Keras - how to set weights to some layers

Viewed 136

I want to set weights to an existing model, such as VGG 16. However, I only want to set weights to the model excluding the last (fully connected) layer. I have tried to use model.layers[:-1].set_weights(weights[:-1]), it will cause the error of

File "server.py", line 114, in evaluate                                                                                   
model.layers[:-1].set_weights(weights[:-1])          
AttributeError: 'list' object has no attribute 'set_weights'  

There is one solution used the for loop to set the weight, but it is inefficient.

weights_list = model.get_weights()
for i, weights in enumerate(weights_list[0:9]):
    model.layers[i].set_weights(weights)

Is there a way to set the weight to some layers of the model? Thanks!

0 Answers
Related