将训练网络的最后一层(输出层)权重加载到新模型中

是否可以使用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

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 examplemodel = Sequential(layers = [    Dense(70, input_shape = (100,)),    Dense(60),    Dense(50),    Dense(5)])# Save the weights of the modelmodel.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]))>> Trueprint (np.all(new_model.layers[-2].get_weights()[0] == old_model.layers[-2].get_weights()[0]))>> False

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注