“密集”层的输入 0 与该层不兼容:输入形状的预期轴 -1 的值为 784,但收到的输入形状为 (784, 1)

问题描述 投票:0回答:0

我尝试在 mnist 数据集上训练模型。在这里,

features
targets
是从 csv 文件中提取的两个 pandas Dataframe。 我使用 tensorflow 启动数据集:

dataset = tf.data.Dataset.from_tensor_slices((features, targets))

每一行是一组 784 个整数,图像的每个单元格一个值。 在这种情况下,tf 数据集如下所示:

tf.Tensor(
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]], shape=(6271, 784), dtype=int64)

那我尝试使用下面的模型

input_layer = layers.Input(shape=(784, 1))
input_flat = layers.Flatten()(input_layer)
middle_layer = layers.Dense(128, activation='relu')(input_flat)
output_layer = layers.Dense(10)(middle_layer)
model = Model(input_layer, output_layer)

******************************************
Model: "model"
_________________________________________________________________
Layer (type)                Output Shape              Param #   
=================================================================
input_1 (InputLayer)        [(None, 784, 1)]          0         
                                                               
flatten (Flatten)           (None, 784)               0         
                                                               
dense (Dense)               (None, 128)               100480    
                                                               
dense_1 (Dense)             (None, 10)                1290      
                                                               
=================================================================

我得到这个错误:

ValueError: Exception encountered when calling layer 'model' (type Functional).

Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 784, but received input with shape (784, 1)

Call arguments received by layer 'model' (type Functional):
  • inputs=tf.Tensor(shape=(784,), dtype=int64)
  • training=True
  • mask=None

我无法弄清楚模型的哪一部分有错误的尺寸。知道如何解决吗?

python tensorflow mnist
© www.soinside.com 2019 - 2024. All rights reserved.