Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set weights in Keras with a numpy array?

I am having trouble with the Keras backend functions for setting values. I am trying to convert a model from PyTorch to Keras and am trying to set the weights of the Keras model, but the weights do not appear to be getting set. Note: I am not actually setting with np.ones just using that for an example.

I have tried...

Loading an existing model

import keras from keras.models import load_model, Model model = load_model(model_dir+file_name) keras_layer = [layer for layer in model.layers if layer.name=='conv2d_1'][0] 

Creating a simple model

img_input = keras.layers.Input(shape=(3,3,3)) x = keras.layers.Conv2D(1, kernel_size=1, strides=1, padding="valid",  use_bias=False, name='conv1')(img_input) model = Model(img_input, x) keras_layer = [layer for layer in model.layers if layer.name=='conv1'][0] 

Then using set_weights or set_value

keras_layer.set_weights([np.ones((1, 1, 3, 1))]) 

or...

K.batch_set_value([(weight,np.ones((1, 1, 3, 1))) for weight in keras_layer.weights]) 

afterwards I call either one of the following:

K.batch_get_value([weight for weight in keras_layer.weights]) keras_layer.get_weights() 

And None of the weights appear to have been set. The same values as before are returned.

[array([[[[  1.61547325e-06],       [  2.97779252e-06],       [  1.50160542e-06]]]], dtype=float32)] 

How do I set the weights of a layer in Keras with a numpy array of values?

like image 660
DeltaLee Avatar asked Nov 08 '17 15:11

DeltaLee


People also ask

How do I assign weights in keras?

Use the get_weights() function to get the weights and biases of the layers before training the model. These are the weights and biases with which the layers will be initialized.

Can I use Numpy in keras?

Using the TensorFlow NumPy API alongside Keras allows you to easily leverage TensorBoard. The TensorBoard monitor metrics and examine the training curve. The TensorBoard also allows you to explore the computation graph used in your models.

What are weights in keras?

1 Answer. Show activity on this post. Model weights are all the parameters (including trainable and non-trainable) of the model which are in turn all the parameters used in the layers of the model. And yes, for a convolution layer that would be the filter weights as well as the biases.

How do I save and load weights in keras?

Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.


2 Answers

What is keras_layer in your code?

You can set weights these ways:

model.layers[i].set_weights(listOfNumpyArrays)     model.get_layer(layerName).set_weights(...) model.set_weights(listOfNumpyArrays) 

Where model is an instance of an existing model. You can see the expected length of the list and its array shapes using the method get_weights() from the same instances above.

like image 158
Daniel Möller Avatar answered Sep 20 '22 07:09

Daniel Möller


The set_weights() method of keras accepts a list of numpy arrays, what you have passed to the method seems like a single array. The shape of this should be the same as the shape of the output of get_weights() on the same layer. Here's the code:

l=[] x=np.array() #weights y=np.array() #array of biases l.append(x) l.append(y) loaded_model.layers[0].set_weights(l) #loaded_model.layer[0] being the layer 

This worked for me and it returns the updated weights on calling get_weights().

like image 26
Palak Bansal Avatar answered Sep 21 '22 07:09

Palak Bansal