'ValueError in Unsupervised learning

I'm trying to bulid an unsupervised learning model with Autoencoders and I get a ValueError after this code:

from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Reshape
from tensorflow.keras.optimizers import SGD

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

(X_train,y_train),(X_test,y_test) = mnist.load_data()
X_train = X_train/255
X_test = X_test/255

encoder = Sequential()
encoder.add(Flatten(input_shape=[28,28]))

encoder.add(Dense(400,activation='relu'))
encoder.add(Dense(200,activation='relu'))
encoder.add(Dense(100,activation='relu'))
encoder.add(Dense(50,activation='relu'))
encoder.add(Dense(25,activation='relu'))

decoder = Sequential()

decoder.add(Dense(50,activation='relu'))
decoder.add(Dense(100,activation='relu'))
decoder.add(Dense(200,activation='relu'))
decoder.add(Dense(400,activation='relu'))
decoder.add(Dense(784,activation='sigmoid'))

decoder.add(Reshape([28,28]))

autoencoder = Sequential([encoder,decoder])

autoencoder.compile(loss='binary_crossentropy',
                    optimizer=SGD(learning_rate=1.5),     
                    metrics=['accuracy'])

autoencoder.fit(X_train, X_train, epochs=10, validation_data=[X_test, X_test])

image = autoencoder.predict(X_test[0])

I get this Error:

WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='sequential_13_input'), name='sequential_13_input', description="created by layer 'sequential_13_input'"), but it was called on an input with incompatible shape (None, 28). WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='flatten_3_input'), name='flatten_3_input', description="created by layer 'flatten_3_input'"), but it was called on an input with incompatible shape (None, 28).

ValueError Traceback (most recent call last) in () ----> 1 image = autoencoder.predict(X_test[0])

1 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1127 except Exception as e: # pylint:disable=broad-except 1128 if hasattr(e, "ag_error_metadata"): -> 1129 raise e.ag_error_metadata.to_exception(e) 1130 else: 1131 raise

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1621, in predict_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1611, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1604, in run_step  **
    outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1572, in predict_step
    return self(x, training=False)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 248, in assert_input_compatibility
    f'Input {input_index} of layer "{layer_name}" is '

ValueError: Exception encountered when calling layer "sequential_13" (type Sequential).

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

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 28), dtype=float32)
  • training=False
  • mask=None


Solution 1:[1]

When you trained your model, you used:

encoder.add(Flatten(input_shape=[28,28]))

but you should do:

encoder.add(tf.keras.Input(shape=(28,28)))
encoder.add(Flatten())

This is because on your code you are flattening inside the input layer, so it expects your data to be already flattened, but you want your model to flatten the data after input.

As the error message suggests:

expected axis -1of input shape to have value 784, but received input with shape (None, 28)

Also, it is better to define the Input class as your first layer.

Please read documentation in Flatten Layer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nferreira78