我使用新的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集来获得更多的数据点!
发布于 2020-03-23 05:16:27
由于您在预测时看到了问题,因此解决此问题的一种方法是将测试数据填充为批处理大小的倍数。它不应该减慢预测的速度,因为批次的数量没有变化。numpy.pad应该可以做到这一点。
https://stackoverflow.com/questions/60804619
复制相似问题