I am currently learning about autoencoders and I follow https://www.tensorflow.org/tutorials/generative/autoencoder

When denoising images, authors of tutorial add an additional axis to the data and I cannot find any explanation why... I would appreciate any answer or suggestion :)

x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

Then the encoder is built from the following layers:

 self.encoder = tf.keras.Sequential([
      layers.Input(shape=(28, 28, 1)), 
      layers.Conv2D(16, (3,3), activation='relu', padding='same', strides=2),
      layers.Conv2D(8, (3,3), activation='relu', padding='same', strides=2)])
 self.decoder = tf.keras.Sequential([
      layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2D(1, kernel_size=(3,3), activation='sigmoid', padding='same')])
  • $\begingroup$ This question is really at the limit of being on-topic, but I will leave it open. $\endgroup$
    – nbro
    Oct 30, 2020 at 19:25

1 Answer 1


I hope this is still relevant. I was also recently taking this tutorial and after some searching it appears that this is required for the Conv2D layers used in the denoising autoencoder (specifically the encoder). This axis represents the number of channels present in the data.

For colour images, this would be 3 representing the red, green and blue channels. However, in this case, the images are black and white and hence only 1 channel is present.

Looking at the documentation we see the following:

When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers or None, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures...

I hope this clears things up a bit. I am new to the world of tensor flow so my knowledge is very limited.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .