0
votes

i'm trying to train an autoencoder in the following code:

encoder_input = keras.layers.Input(shape=(x_Train.shape[1]), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)

encoder = keras.Model(encoder_input, encoder_out, name="encoder")

decoder_input = keras.layers.Dense(602896, activation = "relu")(encoder_out)
decoder_output = keras.layers.Reshape((769, 28, 28))(decoder_input)

opt = keras.optimizers.RMSprop(learning_rate=1e-3)

autoencoder = keras.Model(encoder_input, decoder_output, name = "autoencoder")
autoencoder.summary()

autoencoder.compile(opt, loss='mse')
autoencoder.fit(x_Train, x_Train, epochs=10, batch_size=64, validation_split = 0.1)

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

I don't know how to deal with that or to resize my input. My x_train is a vector with size [769,28,28]

Could someone help me to handle the error?

That's the summary

Thanks

2

2 Answers

0
votes

Your input shape for your autoencoder is a little weird, your training data has a shaped of 28x28, with 769 as your batch, so the fix should be like this:

encoder_input = keras.layer.Input(shape=(28, 28), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)

# For ur decoder, you need to change a bit as well
decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out) # Flatten until 28x28 =784
decoder_output = keras.layers.Reshape((28, 28))(decoder_input) # From there reshape back to 28x28
0
votes

I corrected as you suggested, but it still returns the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-48-78f8cac6c724> in <module>()
      6 
      7 decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out)
----> 8 decoder_output = keras.layers.Reshape((28, 28))(decoder_input)
      9 
     10 opt = keras.optimizers.RMSprop(learning_rate=1e-3)

6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py in _fix_unknown_dimension(self, input_shape, output_shape)
    534       output_shape[unknown] = original // known
    535     elif original != known:
--> 536       raise ValueError(msg)
    537     return output_shape
    538 

ValueError: total size of new array must be unchanged, input_shape = [28, 784], output_shape = [28, 28]

Code:

encoder_input = keras.layers.Input(shape=(28,28), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)

encoder = keras.Model(encoder_input, encoder_out, name="encoder")

decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out)
decoder_output = keras.layers.Reshape((28, 28))(decoder_input)

opt = keras.optimizers.RMSprop(learning_rate=1e-3)

autoencoder = keras.Model(encoder_input, decoder_output, name = "autoencoder")
autoencoder.summary()