Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Keras model after training

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?

like image 568
smw Avatar asked May 01 '17 05:05

smw


1 Answers

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])
like image 184
Igor Poletaev Avatar answered Sep 23 '22 00:09

Igor Poletaev