Reputation: 422
I am looking at how to set custom weights into the layers.
Below is the code I work with
batch_size = 64
input_dim = 12
units = 64
output_size = 1 # labels are from 0 to 9
# Build the RNN model
def build_model(allow_cudnn_kernel=True):
lstm_layer = keras.layers.RNN(
keras.layers.LSTMCell(units), input_shape=(None, input_dim))
model = keras.models.Sequential(
[
lstm_layer,
keras.layers.BatchNormalization(),
keras.layers.Dense(output_size),
]
)
return model
model = build_model()
model.compile(
loss=keras.losses.MeanSquaredError(),
optimizer="Adam",
metrics=["accuracy"],
)
model.fit(
x_train, y_train, validation_data=(x_val, y_val), batch_size=batch_size, epochs=15
)
Modle Summary
Can anyone help me how to set_weights in above code? Thanks in advance.
Upvotes: 6
Views: 3684
Reputation:
You can do it using set_weights
method.
For example, if you want to set the weights of your LSTM Layer
, it can be accessed using model.layers[0]
and if your Custom Weights
are, say in an array, named, my_weights_matrix
, then you can set your Custom Weights
to First Layer (LSTM) using the code shown below:
model.layers[0].set_weights([my_weights_matrix])
If you don't want your weights to be modified during Training, then you have to Freeze that Layer using the code, model.layers[0].trainable = False
.
Please let me know if you face any other issue and I will be Happy to Help you.
Hope this helps. Happy Learning!
Upvotes: 8