Tensorflow MNIST Sequential - ValueError: layer Sequential的输



我在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)

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

这一行有问题层。(单位=64,激活='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)

相关内容

  • 没有找到相关文章

最新更新