【问题标题】:Load weights for last layer (output layer) to a new model from trained network将最后一层(输出层)的权重从经过训练的网络加载到新模型
【发布时间】:2020-12-07 17:43:50
【问题描述】:

是否可以通过使用 set_weights 和 get_weights 方案从训练有素的网络将权重加载到我的新模型的最后一层? 关键是,我将每一层的权重保存为一个 mat 文件(训练后),以便在 Matlab 中进行一些计算,我只想将最后一层的修改后的权重加载到我的新模型和其他层的最后一层获得与训练模型相同的权重。这有点棘手,因为保存的格式是mat。

weights1 = lstm_model1.layers[0].get_weights()[0]
biases1 = lstm_model1.layers[0].get_weights()[1]
weights2 = lstm_model1.layers[2].get_weights()[0]
biases2 = lstm_model1.layers[2].get_weights()[1]
weights3 = lstm_model1.layers[4].get_weights()[0]
biases3 = lstm_model1.layers[4].get_weights()[1]
# Save the weights and biases for adaptation algorithm 
savemat("weights1.mat", mdict={'weights1': weights1})  
savemat("biases1.mat", mdict={'biases1': biases1})      
savemat("weights2.mat", mdict={'weights2': weights2})   
savemat("biases2.mat", mdict={'biases2': biases2})      
savemat("weights3.mat", mdict={'weights3': weights3}) 
savemat("biases3.mat", mdict={'biases3': biases3})  

如何将其他层的旧权重加载到新模型(没有最后一层),并将最后一层的修改权重加载到新模型的最后一层?

【问题讨论】:

    标签: python python-3.x python-2.7 machine-learning keras


    【解决方案1】:

    如果将其保存为 .h5 文件格式,则可以。但是,我不确定 .mat:

    简单来说,您只需在所需层上调用get_weights,类似地,在另一个模型的相应层上调用set_weights

    last_layer_weights = old_model.layers[-1].get_weights()
    new_model.layers[-1].set_weights(last_layer_weights)
    

    如需更完整的代码示例,请点击此处:

    # Create an arbitrary model with some weights, for example
    model = Sequential(layers = [
        Dense(70, input_shape = (100,)),
        Dense(60),
        Dense(50),
        Dense(5)])
    
    # Save the weights of the model
    model.save_weights(“model.h5”)
    
    # Later, load in the model (we only really need the layer in question)
    old_model = Sequential(layers = [
        Dense(70, input_shape = (100,)),
        Dense(60),
        Dense(50),
        Dense(5)])
    
    old_model.load_weights(“model.h5”)
    
    # Create a new model with slightly different architecture (except for the layer in question, at least)
    new_model = Sequential(layers = [
        Dense(80, input_shape = (100,)),
        Dense(60),
        Dense(50),
        Dense(5)])
    
    # Set the weights of the final layer of the new model to the weights of the final layer of the old model, but leaving other layers unchanged.
    new_model.layers[-1].set_weights(old_model.layers[-1].get_weights())
    
    # Assert that the weights of the final layer is the same, but other are not.
    print (np.all(new_model.layers[-1].get_weights()[0] == old_model.layers[-1].get_weights()[0]))
    >> True
    
    print (np.all(new_model.layers[-2].get_weights()[0] == old_model.layers[-2].get_weights()[0]))
    >> False
    

    【讨论】:

    • 请解释您的解决方案
    猜你喜欢
    • 2018-10-08
    • 2017-02-25
    • 2017-09-08
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多