【问题标题】:Keras load_model not working after trainingKeras load_model 训练后不工作
【发布时间】:2017-02-19 13:51:27
【问题描述】:

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

【问题讨论】:

    标签: python deep-learning keras


    【解决方案1】:

    我能够设计出一种解决方法。我能够使用以下步骤加载经过训练的模型:

    1. 创建顺序模型。 例子: model = Sequential() model.add(...) model.add(...) model.compile(...) model.fit(...)

    2. 训练后使用 model.save_weights() 仅保存模型权重 例子: model.save_weights(SaveLocation)

    3. 要加载模型权重,请按照步骤 1 以编程方式创建模型,但不要使用 model.compile 函数。 例子: model = Sequential() model.add(...) model.add(...) model.load_weights(weightFile)

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

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      相关资源
      最近更新 更多