How to set weights in Keras with a numpy array?

51,081

Solution 1

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.

Solution 2

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().

Solution 3

If you are trying to convert Pytorch model to Keras model, you can also try a Pytorch2Keras converter.

It supports base layers like Conv2d, Linear, Activations, some element-wise operations etc. You can follow pytorch2keras/layers.py for layer convertion functions.

Share:
51,081
DeltaLee
Author by

DeltaLee

Updated on March 06, 2020

Comments

  • DeltaLee
    DeltaLee about 4 years

    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?