【问题标题】:Issues with input dimension in a simple tensorflow model简单张量流模型中的输入维度问题
【发布时间】:2021-08-03 17:07:17
【问题描述】:

我在使用自定义训练模型进行预测时遇到问题。

输入128维向量,输出两个值。

到目前为止,我的模型如下所示:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_12 (Dense)             (None, 128)               16512
_________________________________________________________________
dense_13 (Dense)             (None, 2)                 258
=================================================================
Total params: 16,770
Trainable params: 16,770
Non-trainable params: 0

因此我尝试输入数据来进行预测,(出于示例目的,它只是一个 np.ones 数组)

my_model = tf.keras.models.load_model('my_saved_model.h5')


probability_model = tf.keras.Sequential([my_model, tf.keras.layers.Softmax()])


simple_data = np.ones((128))

predictions = probability_model.predict(simple_data)
print(predictions)

并输出关于 simple_data 形状为 (32, 1) 的错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 128 but received input with shape (32, 1)

我不知道为什么。

提前致谢

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    也许您应该尝试在输入数据中添加批处理维度,如下所示:

    simple_data = np.ones((1, 128))
    

    而且我认为您必须在 predict 方法中添加 arg batch_size=1

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2019-10-02
      • 1970-01-01
      • 2019-02-10
      • 2018-11-25
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多