How to set weights in Keras with a numpy array?
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().
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.