#NEWLINE# def random_matrix( rows , cols , mean = 0 , std = 1 , sparsity = 0 , radius = 0 , diagonal = 0 , rng = None ) : #NEWLINE# #INDENT# 'Create a matrix of randomly-initialized weights.\n\n    Parameters\n    ----------\n    rows : int\n        Number of rows of the weight matrix -- equivalently, the number of\n        "input" units that the weight matrix connects.\n    cols : int\n        Number of columns of the weight matrix -- equivalently, the number\n        of "output" units that the weight matrix connects.\n    mean : float, optional\n        Draw initial weight values from a normal with this mean. Defaults to 0.\n    std : float, optional\n        Draw initial weight values from a normal with this standard deviation.\n        Defaults to 1.\n    sparsity : float in (0, 1), optional\n        If given, ensure that the given fraction of the weight matrix is\n        set to zero. Defaults to 0, meaning all weights are nonzero.\n    radius : float, optional\n        If given, rescale the initial weights to have this spectral radius.\n        No scaling is performed by default.\n    diagonal : float, optional\n        If nonzero, create a matrix containing all zeros except for this value\n        along the diagonal. If nonzero, other arguments (except for rows and\n        cols) will be ignored.\n    rng : :class:`numpy.random.RandomState` or int, optional\n        A random number generator, or an integer seed for a random number\n        generator. If not provided, the random number generator will be created\n        with an automatically chosen seed.\n\n    Returns\n    -------\n    matrix : numpy array\n        An array containing random values. These often represent the weights\n        connecting each "input" unit to each "output" unit in a layer.\n    ' #NEWLINE# if ( ( rng is None ) or isinstance ( rng , int ) ) : #NEWLINE# #INDENT# rng = np . random . RandomState ( rng ) #NEWLINE# #UNINDENT# arr = ( mean + ( std * rng . randn ( rows , cols ) ) ) #NEWLINE# if ( 1 > sparsity > 0 ) : #NEWLINE# #INDENT# k = min ( rows , cols ) #NEWLINE# mask = rng . binomial ( n = 1 , p = ( 1 - sparsity ) , size = ( rows , cols ) ) . astype ( bool ) #NEWLINE# mask [ : k , : k ] |= np . eye ( k ) . astype ( bool ) #NEWLINE# arr *= mask #NEWLINE# #UNINDENT# if ( radius > 0 ) : #NEWLINE# #INDENT# ( u , s , vT ) = np . linalg . svd ( arr , full_matrices = False ) #NEWLINE# arr = np . dot ( np . dot ( u , np . diag ( ( ( radius * s ) / abs ( s [ 0 ] ) ) ) ) , s ) #NEWLINE# #UNINDENT# if ( diagonal != 0 ) : #NEWLINE# #INDENT# arr = ( diagonal * np . eye ( max ( rows , cols ) ) [ : rows , : cols ] ) #NEWLINE# #UNINDENT# return arr . astype ( FLOAT )