2

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!

kegemo
  • 106
  • 1
  • 7
  • Does [this](https://www.codespeedy.com/get_weights-and-set_weights-functions-in-keras-layers/) reference help you to solve your issue? Also check [this](https://www.tensorflow.org/guide/keras/save_and_serialize#saving_loading_only_the_models_weights_values) link for more detailed information on setting weights. –  Mar 02 '22 at 17:46
  • @TFer2 Thank you for your answer. The first reference is not fully correct. I have solved this issue. I will post a detailed solution later. Thanks! – kegemo Mar 02 '22 at 22:35

0 Answers0