Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

keras - Tensorflow MNIST Sequential - ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your input data shape is different from the shape of the input layer. Modify your code as follows

def normalize(image, label):
    n = image/255.0
    #following line can be taken care by flattening
    #n = tf.reshape(n, [28*28])
    return n, label
...
mymodel = models.Sequential([layers.Flatten(),
layers.Dense(units=64, activation='relu'),
layers.Dense(units=32, activation='relu'),
layers.Dense(units=10, activation='softmax')],
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...