Tensorflow MNIST Sequential - ValueError:连续层的输入 0 与层不兼容:输入形状的预期轴 -1 具有

时间:2021-01-24 02:06:41

标签: tensorflow keras mnist

我正在 Tensorflow 的 MNIST 数据集上尝试一个简单的网络。我遇到了这个错误,并试图了解问题所在。

mnist_ds, mnist_info = tfds.load(
    'mnist', split='train', as_supervised=True, with_info=True)
def normalize(image, label):
    n = tf.cast(image, tf.float32) / 255.0
    n = tf.reshape(n, [28*28])
    return n, label
mnist_ds_norm = mnist_ds.map(normalize)
mymodel = models.Sequential([
    layers.Dense(units=64, activation='relu', input_shape=[28*28]),
    layers.Dense(units=32, activation='relu'),
    layers.Dense(units=10, activation='softmax')])
mymodel.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'],
)
mymodel.fit(mnist_ds_norm, epochs=3, verbose=1)

创建网络:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense (Dense)                (None, 64)                50240
_________________________________________________________________
dense_1 (Dense)              (None, 32)                2080
_________________________________________________________________
dense_2 (Dense)              (None, 10)                330
=================================================================
Total params: 52,650
Trainable params: 52,650
Non-trainable params: 0
_________________________________________________________________

最终的错误是:

...
ValueError: in user code:

    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /my-path/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /my-path/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /my-path/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:754 train_step
        y_pred = self(x, training=True)
    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /my-path/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
        ' but received input with shape ' + display_shape(x.shape))

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

任何见解都会有所帮助。 提前致谢。

1 个答案:

答案 0 :(得分:0)

问题出在这条线上 layer.Dense(units=64, activation='relu', input_shape=[28*28])

查找示例工作代码

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# train set / data 
x_train = x_train.reshape(-1, 28*28)
x_train = x_train.astype('float32') / 255

# train set / target 
y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)

#model
model = Sequential()
model.add(Dense(units=64, input_dim=784, activation="relu"))
model.Dense(units=32, activation='relu'),
model.add(Dense(units =10, activation="softmax"))

model.compile(
    optimizer='adam',
    loss=categorical_crossentropy,
    metrics=['accuracy'],
)
mymodel.fit(x_train, epochs=3, verbose=1)