How to load only specific weights on Keras

前端 未结 2 1147
忘了有多久
忘了有多久 2020-12-13 09:28

I have a trained model that I\'ve exported the weights and want to partially load into another model. My model is built in Keras using TensorFlow as backend.

Right n

2条回答
  •  爱一瞬间的悲伤
    2020-12-13 10:26

    This call:

    weights_list = model.get_weights()
    

    will return a list of all weight tensors in the model, as Numpy arrays.

    All what you have to do next is to iterate over this list and apply:

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

    where model.layers is a flattened list of the layers comprising the model. In this case, you reload the weights of the first 9 layers.

    More information is available here:

    https://keras.io/layers/about-keras-layers/

    https://keras.io/models/about-keras-models/

提交回复
热议问题