I have a simple trained keras model:
model = Sequential()
model.add(Dense(output_dim, activation='relu',input_shape(input_dim,)))
model.add(Dense(output_dim, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train)
After I train the model I want to add a layer where I choose the weights. I have tried
model.layers.append(Dense(output_dim, activation='sigmoid', input_shape=(input_dim,), kernel_initializer='ones'))
But it is not working... I feel like I might need to compile the model again, but its not working. Any ideas?
If I understood your question right then all you need is just to save weights for all of the model's layers. You can do it in different ways. The simplest one is the following:
# training ...
temp_weights = [layer.get_weights() for layer in model.layers]
After your model was trained - it's necessary to build model again with one more layer as you wanted:
model = Sequential()
model.add(Dense(output_dim, activation='relu',input_shape(input_dim,)))
model.add(Dense(output_dim, activation='sigmoid'))
model.add(Dense(output_dim, activation='sigmoid', kernel_initializer='ones')) # new layer
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
And load previously saved weights into the respective layers:
for i in range(len(temp_weights)):
model.layers[i].set_weights(temp_weights[i])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With