#NEWLINE# def gen_backend_tensors( backends , tensor_dims , flags = None , dtype = np . float32 ) : #NEWLINE# #INDENT# "\n    Generates random number for all backends.\n\n    Arguments:\n        backends (list): List of backends, one of (np, NervanaGPU, NervanaCPU)\n        tensor_dims (list): List of dimensions of the tensors, for example\n                            [(1, 2), (3, 4), (5, 6)]\n        dtype (data-type): One of (np.float16, np.float32), must be the same\n                           as backend.dtype if backend is one of the nervana\n                           backends\n        flags (list or str): If list is provided, specifies the flag for each\n                             tensor. If str is provided, will be applied to all\n                             tensors. Flags is one of the following:\n                             ('zeros', 'pos_ones', 'neg_ones', 'pos_rand',\n                              'neg_rand', 'rand', None)\n\n    Returns:\n        List of lists of tensors, corresponding to the backends.\n        For example:\n        [[np.ndarray, np.ndarray, np.ndarray],\n         [GPUTensor, GPUTensor, GPUTensor],\n         [CPUTensor, CPUTensor, CPUTensor]]\n    " #NEWLINE# tensor_num = len ( tensor_dims ) #NEWLINE# if ( flags is not None ) : #NEWLINE# #INDENT# assert ( len ( flags ) == tensor_num ) #NEWLINE# #UNINDENT# backend_tensors = [ [ ] for i in range ( tensor_num ) ] #NEWLINE# idx = 0 #NEWLINE# for ( tensor_dim , flag ) in zip ( tensor_dims , flags ) : #NEWLINE# #INDENT# assert ( flag in ( 'zeros' , 'pos_ones' , 'neg_ones' , 'pos_rand' , 'neg_rand' , 'rand' , None ) ) #NEWLINE# if ( flag == 'zeros' ) : #NEWLINE# #INDENT# tensor = np . zeros ( tensor_dim ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( flag == 'pos_ones' ) : #NEWLINE# #INDENT# tensor = np . ones ( tensor_dim ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( flag == 'neg_ones' ) : #NEWLINE# #INDENT# tensor = ( - np . ones ( tensor_dim ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( flag == 'pos_rand' ) : #NEWLINE# #INDENT# tensor = np . random . rand ( * tensor_dim ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( flag == 'neg_rand' ) : #NEWLINE# #INDENT# tensor = ( - np . random . rand ( * tensor_dim ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( flag == 'rand' ) or ( flag is None ) ) : #NEWLINE# #INDENT# tensor = ( - np . random . randn ( * tensor_dim ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise NotImplementedError #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# tensor = tensor . astype ( dtype ) #NEWLINE# for ( backend , tensors ) in zip ( backends , backend_tensors ) : #NEWLINE# #INDENT# if ( backend == np ) : #NEWLINE# #INDENT# tensors . append ( tensor ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert ( backend . default_dtype == dtype ) #NEWLINE# tensors . append ( backend . array ( tensor , name = ( 'x%s' % idx ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# idx += 1 #NEWLINE# #UNINDENT# return backend_tensors