Keras训练后load_model无法工作

3
我使用了这里的示例代码: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并分别保存权重,然后进行加载,但即使这样也会失败。

1个回答

2
我能够想出一个解决方法。我可以按照以下步骤加载训练好的模型:
  1. 创建Sequential模型。 例如: model = Sequential() model.add(...) model.add(...) model.compile(...) model.fit(...)

  2. 在训练后只保存模型权重,使用model.save_weights()函数。 例如: model.save_weights(保存位置)

  3. 要加载模型权重,请以与第1步相同的方式编程创建模型,但不要使用model.compile函数。 例如: model = Sequential() model.add(...) model.add(...) model.load_weights(weightFile)

  4. 现在成功加载了权重。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接