<s> """<STR_LIT>""" <EOL> from parameter import * <EOL> class Edge ( Parameter ) : <EOL> def __init__ ( self , proto , node1 , node2 , t_op = None , tied_to = None ) : <EOL> super ( Edge , self ) . __init__ ( ) <EOL> self . node1 = node1 <EOL> self . node2 = node2 <EOL> self . tied_to = tied_to <EOL> if proto . tied : <EOL> tied_to . num_shares += <NUM_LIT:1> <EOL> self . transpose = proto . tied_transpose <EOL> proto . CopyFrom ( tied_to . proto ) <EOL> proto . node1 = node1 . name <EOL> proto . node2 = node2 . name <EOL> if self . transpose : <EOL> for param in proto . param : <EOL> if param . dimensions : <EOL> dims = list ( reversed ( param . dimensions ) ) <EOL> del param . dimensions [ : ] <EOL> param . dimensions . extend ( dims ) <EOL> self . proto = proto <EOL> self . params = { } <EOL> self . conv = False <EOL> self . local = False <EOL> self . t_op = t_op <EOL> self . name = '<STR_LIT>' % ( self . node1 . name , self . node2 . name ) <EOL> self . prefix = proto . prefix <EOL> self . hyperparams = None <EOL> if proto . directed : <EOL> node1 . AddOutgoingEdge ( self ) <EOL> node2 . AddIncomingEdge ( self ) <EOL> else : <EOL> node1 . AddOutgoingEdge ( self ) <EOL> node1 . AddIncomingEdge ( self ) <EOL> node2 . AddOutgoingEdge ( self ) <EOL> node2 . AddIncomingEdge ( self ) <EOL> self . LoadParams ( proto , t_op = t_op , tied_to = tied_to ) <EOL> self . marker = <NUM_LIT:0> <EOL> self . fig = visualize . GetFigId ( ) <EOL> self . fig_stats = visualize . GetFigId ( ) <EOL> if self . conv or self . local : <EOL> self . conv_filter_fig = visualize . GetFigId ( ) <EOL> self . AllocateMemory ( ) <EOL> def Show ( self ) : <EOL> if not self . hyperparams . enable_display : <EOL> return <EOL> visualize . show_hist ( self . params [ '<STR_LIT>' ] . asarray ( ) , self . fig ) <EOL> """<STR_LIT>""" <EOL> def AllocateBatchsizeDependentMemory ( self ) : <EOL> for param in self . proto . param : <EOL> if param . conv or param . local : <EOL> self . AllocateMemoryForConvolutions ( param , self . node1 , self . node2 ) <EOL> def AllocateMemory ( self ) : <EOL> if '<STR_LIT>' in self . params : <EOL> edge_shape = self . params [ '<STR_LIT>' ] . shape <EOL> self . temp = cm . CUDAMatrix ( np . zeros ( edge_shape ) ) <EOL> self . gradient = cm . CUDAMatrix ( np . zeros ( edge_shape ) ) <EOL> self . gradient_history = cm . CUDAMatrix ( np . zeros ( edge_shape ) ) <EOL> t_op = self . t_op <EOL> if t_op and ( t_op . optimizer == deepnet_pb2 . Operation . PCD or t_op . optimizer == deepnet_pb2 . Operation . CD ) : <EOL> self . suff_stats = cm . CUDAMatrix ( np . zeros ( edge_shape ) ) <EOL> def AllocateMemoryForConvolutions ( self , param , node1 , node2 ) : <EOL> self . conv = param . conv <EOL> self . local = param . local <EOL> if self . conv : <EOL> assert not self . local <EOL> else : <EOL> assert not self . conv <EOL> self . conv_params = param . conv_params <EOL> num_colors = self . conv_params . num_colors <EOL> num_filters = self . conv_params . num_filters <EOL> size = self . conv_params . size <EOL> padding = self . conv_params . padding <EOL> stride = self . conv_params . stride <EOL> numdims , numimages = node1 . state . shape <EOL> assert numdims % num_colors == <NUM_LIT:0> <EOL> x = int ( np . sqrt ( numdims / num_colors ) ) <EOL> assert x ** <NUM_LIT:2> == numdims / num_colors <EOL> n_locs = ( x + <NUM_LIT:2> * padding - size ) / stride + <NUM_LIT:1> <EOL> input_shape = node1 . state . shape [ : : - <NUM_LIT:1> ] <EOL> output_shape = node2 . state . shape [ : : - <NUM_LIT:1> ] <EOL> self . input_t = cm . empty ( input_shape ) <EOL> self . input_t2 = cm . empty ( input_shape ) <EOL> self . output_t = cm . empty ( output_shape ) <EOL> self . output_t2 = cm . empty ( output_shape ) <EOL> if param . conv_params . max_pool : <EOL> pool_output_size = n_locs ** <NUM_LIT:2> * num_filters <EOL> self . unpooled_layer = cm . empty ( ( numimages , pool_output_size ) ) <EOL> pool_size = param . conv_params . pool_size <EOL> pool_stride = param . conv_params . pool_stride <EOL> n_pool_locs = ( n_locs - pool_size ) / pool_stride + <NUM_LIT:1> <EOL> assert output_shape [ <NUM_LIT:1> ] == n_pool_locs ** <NUM_LIT:2> * num_filters , ( <EOL> "<STR_LIT>" % ( <EOL> self . name , n_pool_locs ** <NUM_LIT:2> * num_filters , output_shape [ <NUM_LIT:1> ] ) ) <EOL> if param . conv_params . prob : <EOL> self . rnd = cm . empty ( self . unpooled_layer . shape ) <EOL> else : <EOL> assert output_shape [ <NUM_LIT:1> ] == n_locs ** <NUM_LIT:2> * num_filters , ( <EOL> "<STR_LIT>" % ( <EOL> self . name , n_locs ** <NUM_LIT:2> * num_filters , output_shape [ <NUM_LIT:1> ] ) ) <EOL> if param . conv_params . rnorm : <EOL> self . unrnormalized_layer = cm . empty ( output_shape ) <EOL> self . denoms = cm . empty ( output_shape ) <EOL> self . rnorm_temp1 = cm . empty ( output_shape ) <EOL> self . rnorm_temp2 = cm . empty ( output_shape ) <EOL> return n_locs <EOL> def LoadParams ( self , proto , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> node1 = self . node1 <EOL> node2 = self . node2 <EOL> self . hyperparams = proto . hyperparams <EOL> param_names = [ param . name for param in proto . param ] <EOL> for param in proto . param : <EOL> if param . conv or param . local : <EOL> n_locs = self . AllocateMemoryForConvolutions ( param , node1 , node2 ) <EOL> if not param . dimensions : <EOL> if param . conv : <EOL> cv = param . conv_params <EOL> dims = [ cv . num_filters , cv . size ** <NUM_LIT:2> * cv . num_colors ] <EOL> elif param . local : <EOL> dims = [ cv . num_filters , n_locs ** <NUM_LIT:2> * cv . size ** <NUM_LIT:2> * cv . num_colors ] <EOL> else : <EOL> dims = [ node1 . numlabels * node1 . dimensions , <EOL> node2 . numlabels * node2 . dimensions ] <EOL> param . dimensions . extend ( dims ) <EOL> super ( Edge , self ) . LoadParams ( proto , ** kwargs ) <EOL> def LoadPretrained ( self , param ) : <EOL> node1_name = param . pretrained_model_node1 <EOL> node2_name = param . pretrained_model_node2 <EOL> if node1_name == '<STR_LIT>' : <EOL> node1_name = self . proto . node1 <EOL> if node2_name == '<STR_LIT>' : <EOL> node2_name = self . proto . node2 <EOL> if param . transpose_pretrained : <EOL> temp = node1_name <EOL> node1_name = node2_name <EOL> node2_name = temp <EOL> mat = None <EOL> for pretrained_model in param . pretrained_model : <EOL> model_file = os . path . join ( self . prefix , pretrained_model ) <EOL> ext = os . path . splitext ( pretrained_model ) [ <NUM_LIT:1> ] <EOL> if ext == '<STR_LIT>' : <EOL> npzfile = np . load ( model_file ) <EOL> if param . name == '<STR_LIT>' : <EOL> this_mat = np . nan_to_num ( npzfile [ '<STR_LIT>' ] / npzfile [ '<STR_LIT>' ] ) <EOL> elif param . name == '<STR_LIT>' : <EOL> this_mat = np . nan_to_num ( <NUM_LIT:1.> / npzfile [ '<STR_LIT>' ] ) <EOL> elif ext == '<STR_LIT>' : <EOL> this_mat = np . load ( model_file ) <EOL> else : <EOL> model = util . ReadModel ( model_file ) <EOL> try : <EOL> edge = next ( e for e in model . edge if e . node1 == node1_name and e . node2 == node2_name ) <EOL> except StopIteration as e : <EOL> print '<STR_LIT>' % ( node1_name , node2_name , model_file ) <EOL> raise e <EOL> pretrained_param = next ( p for p in edge . param if p . name == param . name ) <EOL> assert pretrained_param . mat != '<STR_LIT>' , '<STR_LIT>' % ( <EOL> pretrained_param . name , edge . node1 , edge . node2 , pretrained_model ) <EOL> if param . transpose_pretrained : <EOL> assert param . dimensions == pretrained_param . dimensions [ : : - <NUM_LIT:1> ] , '<STR_LIT>' % ( <EOL> param . dimensions , reversed ( pretrained_param . dimensions ) ) <EOL> else : <EOL> assert param . dimensions == pretrained_param . dimensions , '<STR_LIT>' % ( <EOL> param . dimensions , pretrained_param . dimensions ) <EOL> this_mat = param . mult_factor * util . ParameterAsNumpy ( pretrained_param ) <EOL> if param . transpose_pretrained : <EOL> this_mat = this_mat . T <EOL> if mat is None : <EOL> mat = this_mat <EOL> else : <EOL> mat += this_mat <EOL> return mat / len ( param . pretrained_model ) <EOL> def CollectSufficientStatistics ( self , neg = False ) : <EOL> logging . debug ( '<STR_LIT>' , self . name ) <EOL> if self . node1 . activation == deepnet_pb2 . Hyperparams . REPLICATED_SOFTMAX : <EOL> self . node1 . state . div_by_row ( self . node1 . NN ) <EOL> if self . node2 . activation == deepnet_pb2 . Hyperparams . REPLICATED_SOFTMAX : <EOL> self . node2 . state . div_by_row ( self . node2 . NN ) <EOL> if not neg : <EOL> h1 = self . node1 . hyperparams <EOL> h2 = self . node2 . hyperparams <EOL> if h1 . sparsity : <EOL> self . node1 . state . add_col_mult ( self . node1 . sparsity_gradient , - <NUM_LIT:1> ) <EOL> if h2 . sparsity : <EOL> self . node2 . state . add_col_mult ( self . node2 . sparsity_gradient , - <NUM_LIT:1> ) <EOL> cm . dot ( self . node1 . state , self . node2 . state . T , target = self . suff_stats ) <EOL> if h1 . sparsity : <EOL> self . node1 . state . add_col_vec ( self . node1 . sparsity_gradient ) <EOL> if h2 . sparsity : <EOL> self . node2 . state . add_col_vec ( self . node2 . sparsity_gradient ) <EOL> else : <EOL> self . suff_stats . add_dot ( self . node1 . state , self . node2 . state . T , mult = - <NUM_LIT:1.0> ) <EOL> if self . node1 . activation == deepnet_pb2 . Hyperparams . REPLICATED_SOFTMAX : <EOL> self . node1 . state . mult_by_row ( self . node1 . NN ) <EOL> if self . node2 . activation == deepnet_pb2 . Hyperparams . REPLICATED_SOFTMAX : <EOL> self . node2 . state . mult_by_row ( self . node2 . NN ) </s>
