我使用 Keras 训练了一个模型,但忘记保存模型。该模型是开发了许多其他模型的项目的一部分,但现在我无法继续该项目。幸运的是,我保存了初始和最终训练重量。现在,我正在尝试创建一个具有相同最终权重的模型来获得预测。我正在编译 keras 模型并使用函数 model.set_weights 将丢失模型的最终训练权重设置为新模型。这是代码。
model = Sequential()
model.add(Dense(1,input_dim = 1, activation = 'relu'))
model.add(Dense(1, activation = 'relu'))
model.compile(loss = 'mean_squared_error', optimizer = 'Adam', metrics = ['mse'])
listOfNumpyArrays = [np.array([0.2]),np.array([0.2])]
listOfNumpyArrays1 = listOfNumpyArrays
model.layers[0].set_weights(listOfNumpyArrays)
model.layers[1].set_weights(listOfNumpyArrays1)
追溯
ValueError Traceback (most recent call last)
<ipython-input-31-e63437554e30> in <module>()
----> 1 model.layers[0].set_weights(listOfNumpyArrays)
2 model.layers[1].set_weights(listOfNumpyArrays1)
1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in set_weights(self, weights)
1124 str(pv.shape) +
1125 ' not compatible with '
-> 1126 'provided weight shape ' + str(w.shape))
1127 weight_value_tuples.append((p, w))
1128 K.batch_set_value(weight_value_tuples)
ValueError: Layer weight shape (1, 1) not compatible with provided weight shape (1,)
慕的地6264312
相关分类