I am trying a simple network on MNIST dataset from Tensorflow. I hit this error and am trying to understand where the issue lies.
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)
Which creates the network:
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
_________________________________________________________________
The ultimate error is:
...
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)
Any insights would be helpful.
Thanks in advance.
question from:
https://stackoverflow.com/questions/65866526/tensorflow-mnist-sequential-valueerror-input-0-of-layer-sequential-is-incompa