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?
See Question&Answers more detail:
os