是否可以使用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})
如何将其他层的旧权重加载到新模型(没有最后一层),将最后一层的修改权重加载到新模型的最后一层
如果将其保存为.h5文件格式,则此操作有效。但是,我不确定。mat:
简单地说,您只需在所需的层上调用
get_weights
,类似地,在另一个模型的相应层上调用set_weights
:有关更完整的代码示例,请参见:
相关问题 更多 >
编程相关推荐