Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize layers by numpy array in keras

I want to convert a pre-trained caffe model to keras, then i need to initialize the layers , layer by layer. I saved the weights and biases in a mat file and I loaded them to python workspace. I know "weights" parameter get the numpy array but not how? Thanks

like image 523
Soodabe Zarezade Avatar asked Feb 28 '17 14:02

Soodabe Zarezade


Video Answer


1 Answers

You can get more information about how to set the weight of a model in the Keras Layers Documentation. Basically you use :

layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of get_weights).

Or you can directly initialize them when you create the layer. Every layer has a parameter weights that you can set with a numpy array. Read each layer's documentation to feed the right weights format. For example, Dense() layers accept this format for the parameter weights :

List of Numpy arrays to set as initial weights. The list should have 2 elements, of shape (input_dim, output_dim) and (output_dim,) for weights and biases respectively. source

like image 165
Nassim Ben Avatar answered Nov 09 '22 01:11

Nassim Ben