Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow model.prediction()由于形状不匹配而失败_Tensorflow_Keras_Deep Learning_Mlp - Fatal编程技术网

Tensorflow model.prediction()由于形状不匹配而失败

Tensorflow model.prediction()由于形状不匹配而失败,tensorflow,keras,deep-learning,mlp,Tensorflow,Keras,Deep Learning,Mlp,我使用新的tf.kerasversion2.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

我使用新的
tf.keras
version
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\u result=model.predict(test\u data,batch\u size=138)
测试数据
的形状为
(69,28)
,因此它小于
批量大小
,即138。下面是错误,问题似乎来自第一个退出层:

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)中不存在任何问题。我如何解决这个问题?我没有更多的测试数据,因此无法更改测试数据集以获得更多的数据点

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

在您的模型中,不要强制将批量大小固定为138?例如,
Input(shape=(28,)