我在这里使用了示例代码:https://github.com/fchollet/keras/issues/2295
我无法在keras中训练后加载我的模型。我收到以下错误:
ValueError: Optimizer weight shape (3, 3, 512, 512) not compatible with provided weight shape (256, 43)
我尝试使用HDFView并删除优化器权重并重新加载。但后来我得到了错误:
ValueError: ('shapes (10,4224) and (1128,256) not aligned: 4224 (dim 1) != 1128 (dim 0)', (10, 4224), (1128, 256))
Apply node that caused the error: Dot22(Reshape{2}.0, lstm_2_W_i)
Toposort index: 249
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(10, 4224), (1128, 256)]
Inputs strides: [(16896, 4), (1024, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Elemwise{Add}[(0, 0)](Dot22.0, InplaceDimShuffle{x,0}.0)]]
我还尝试将架构保存到JSON并单独保存权重,然后加载,但即使失败也是如此。
答案 0 :(得分:1)
我能够设计出一种解决方法。我能够使用以下步骤加载训练过的模型:
创建顺序模型。
例:
model = Sequential()
model.add(...)
model.add(...)
model.compile(...)
model.fit(...)
训练后使用model.save_weights()仅保存模型权重
例:
model.save_weights(SaveLocation)
要加载模型权重,请按照步骤1中的编程方式创建模型,但不要使用model.compile函数。
例:
model = Sequential()
model.add(...)
model.add(...)
model.load_weights(weightFile)
现在已成功加载权重。