@@ -15,6 +15,7 @@ def array_2_string(array):
1515def string_2_array (string ):
1616 str_in = StringIO (string )
1717 return np .loadtxt (str_in )
18+
1819
1920def _nnet2file (layers , set_layer_num = - 1 , filename = 'nnet.out' , activation = 'sigmoid' , start_layer = 0 , withfinal = True ,
2021 input_factor = 0.0 , factor = [0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ]):
@@ -64,47 +65,93 @@ def _file2nnet(layers, set_layer_num = -1, filename='nnet.in', activation='sigmo
6465 layers [- 1 ].params [1 ].set_value (np .asarray (string_2_array (nnet_dict [dict_key ]), dtype = theano .config .floatX ))
6566 logger .info ('Loaded the neural_net model in %s' ,(filename ))
6667
67- def _cnn2file (conv_layers , filename = 'nnet.out' , activation = 'sigmoid' , withfinal = True , input_factor = 1.0 , factor = 1.0 ):
68+ def _cnn2file (conv_layers ,mlp_layers ,filename = 'nnet.out' ,
69+ start_layer = 0 ,set_layer_num = - 1 ,withfinal = True , input_factor = 1.0 , factor = 1.0 ):
70+ #Dumping CNN Configuration
6871 n_layers = len (conv_layers )
69- nnet_dict = {}
72+ cnn_dict = {}
7073 for i in xrange (n_layers ):
7174 conv_layer = conv_layers [i ]
7275 filter_shape = conv_layer .filter_shape
73-
7476 for next_X in xrange (filter_shape [0 ]):
7577 for this_X in xrange (filter_shape [1 ]):
7678 dict_a = 'W ' + str (i ) + ' ' + str (next_X ) + ' ' + str (this_X )
7779 if i == 0 :
78- nnet_dict [dict_a ] = array_2_string (input_factor * (conv_layer .W .get_value ())[next_X , this_X ])
80+ cnn_dict [dict_a ] = array_2_string (input_factor * (conv_layer .W .get_value ())[next_X , this_X ])
7981 else :
80- nnet_dict [dict_a ] = array_2_string (factor * (conv_layer .W .get_value ())[next_X , this_X ])
82+ cnn_dict [dict_a ] = array_2_string (factor * (conv_layer .W .get_value ())[next_X , this_X ])
8183
8284 dict_a = 'b ' + str (i )
83- nnet_dict [dict_a ] = array_2_string (conv_layer .b .get_value ())
85+ cnn_dict [dict_a ] = array_2_string (conv_layer .b .get_value ())
86+
87+ #Dumping MLP Configuration
88+ n_layers = len (mlp_layers )
89+ mlp_dict = {}
90+ if set_layer_num == - 1 :
91+ set_layer_num = n_layers - 1
92+ for i in range (start_layer ,set_layer_num ):
93+ dict_a = str (i ) + ' W'
94+ if i == 0 :
95+ mlp_dict [dict_a ] = array_2_string (input_factor * mlp_layers [i ].params [0 ].get_value ())
96+ else :
97+ print mlp_layers [i ].params [0 ]
98+ mlp_dict [dict_a ] = array_2_string (factor * mlp_layers [i ].params [0 ].get_value ())
99+ dict_a = str (i ) + ' b'
100+ mlp_dict [dict_a ] = array_2_string (mlp_layers [i ].params [1 ].get_value ())
101+
102+ if withfinal :
103+ dict_a = 'logreg W'
104+ mlp_dict [dict_a ] = array_2_string (factor * mlp_layers [- 1 ].params [0 ].get_value ())
105+ dict_a = 'logreg b'
106+ mlp_dict [dict_a ] = array_2_string (mlp_layers [- 1 ].params [1 ].get_value ())
84107
108+ nnet_dict = {};
109+ nnet_dict ['cnn' ] = cnn_dict ;
110+ nnet_dict ['mlp' ] = mlp_dict ;
111+
85112 with open (filename , 'wb' ) as fp :
86113 json .dump (nnet_dict , fp , indent = 2 , sort_keys = True )
87114 fp .flush ()
88115 logger .info ('Dumped the conv_net model in %s' ,(filename ))
89116
90- def _file2cnn (conv_layers , filename = 'nnet.in' , activation = 'sigmoid' , withfinal = True , factor = 1.0 ):
91- n_layers = len (conv_layers )
117+ def _file2cnn (conv_layers ,mlp_layers , filename = 'nnet.in' ,set_layer_num = - 1 ,withfinal = True , factor = 1.0 ):
92118 nnet_dict = {}
93-
119+
94120 with open (filename , 'rb' ) as fp :
95121 nnet_dict = json .load (fp )
122+
123+ ##Loading CNN Configuration
124+ n_layers = len (conv_layers )
125+ cnn_dict = nnet_dict ['cnn' ];
96126 for i in xrange (n_layers ):
97127 conv_layer = conv_layers [i ]
98128 filter_shape = conv_layer .filter_shape
99129 W_array = conv_layer .W .get_value ()
100-
130+
101131 for next_X in xrange (filter_shape [0 ]):
102132 for this_X in xrange (filter_shape [1 ]):
103133 dict_a = 'W ' + str (i ) + ' ' + str (next_X ) + ' ' + str (this_X )
104- W_array [next_X , this_X , :, :] = factor * np .asarray (string_2_array (nnet_dict [dict_a ]))
105-
134+ W_array [next_X , this_X , :, :] = factor * np .asarray (string_2_array (cnn_dict [dict_a ]))
135+
106136 conv_layer .W .set_value (W_array )
107-
108137 dict_a = 'b ' + str (i )
109- conv_layer .b .set_value (np .asarray (string_2_array (nnet_dict [dict_a ]), dtype = theano .config .floatX ))
138+ conv_layer .b .set_value (np .asarray (string_2_array (cnn_dict [dict_a ]), dtype = theano .config .floatX ))
139+
140+ ##Loading MLP Configuration
141+ if not mlp_layers is None :
142+ n_layers = len (mlp_layers )
143+ mlp_dict = nnet_dict ['mlp' ];
144+ if set_layer_num == - 1 :
145+ set_layer_num = n_layers - 1
146+ for i in xrange (set_layer_num ):
147+ dict_key = str (i ) + ' W'
148+ mlp_layers [i ].params [0 ].set_value (factor * np .asarray (string_2_array (mlp_dict [dict_key ]), dtype = theano .config .floatX ))
149+ dict_key = str (i ) + ' b'
150+ mlp_layers [i ].params [1 ].set_value (np .asarray (string_2_array (mlp_dict [dict_key ]), dtype = theano .config .floatX ))
151+ if withfinal :
152+ dict_key = 'logreg W'
153+ mlp_layers [- 1 ].params [0 ].set_value (np .asarray (string_2_array (mlp_dict [dict_key ]), dtype = theano .config .floatX ))
154+ dict_key = 'logreg b'
155+ mlp_layers [- 1 ].params [1 ].set_value (np .asarray (string_2_array (mlp_dict [dict_key ]), dtype = theano .config .floatX ))
156+
110157 logger .info ('Loaded the conv_net model in %s' ,(filename ))
0 commit comments