【问题标题】:model.prediction() fails due to mismatch of shapesmodel.prediction() 由于形状不匹配而失败
【发布时间】:2020-03-22 20:30:21
【问题描述】:

我使用新的tf.keras 版本2.2.4-tf 训练了一个简单的 MLP 模型。下面是模型的样子:

input_layer = Input(batch_shape=(138, 28))
first_layer = Dense(30, activation=activation, name="first_dense_layer_1")(input_layer)
first_layer = Dropout(0.1)(first_layer, training=True)
second_layer = Dense(15, activation=activation, name="second_dense_layer")(first_layer)
out = Dense(1, name='output_layer')(second_layer)
model = Model(input_layer, out)

我在尝试进行预测时遇到错误prediction_result = model.predict(test_data, batch_size=138)test_data 的形状为 (69, 28),因此它小于 batch_size 的 138。这是错误,似乎问题来自第一个 dropout 层:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [138,30] vs. [69,30]
     [[node model/dropout/dropout/mul_1 (defined at ./mlp_new_tf.py:471) ]] [Op:__inference_distributed_function_1700]

相同的解决方案在旧版本的 keras (2.2.4) 和 tensorflow (1.12.0) 中没有问题。我该如何解决这个问题?我没有更多数据进行测试,所以我无法更改 test_data 集以拥有更多数据点!

【问题讨论】:

  • 只是不要在您的模型中强制使用 138 的固定批量大小吗?例如。 Input(shape=(28,))

标签: tensorflow keras deep-learning mlp


【解决方案1】:

由于您在预测时看到问题,解决此问题的一种方法是将测试数据填充为批量大小的倍数。它不应该减慢预测速度,因为批次的数量不会改变。 numpy.pad 应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2012-08-10
    相关资源
    最近更新 更多