Answers.Body: <p>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.</p>
  <p>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.</p>
  <p>Looking at the <a href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D" rel="nofollow noreferrer">documentation</a> we see the following:</p>
  <blockquote>
  <p>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...</p>
  </blockquote>
  <p>I hope this clears things up a bit. I am new to the world of tensor flow so my knowledge is very limited.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-21T18:57:29.657Z
Answers.LastActivityDate: 2022-02-21T18:57:29.657Z
Answers.UserDisplayName: Guy
Body: <p>I am currently learning about autoencoders and I follow <a href="https://www.tensorflow.org/tutorials/generative/autoencoder" rel="nofollow noreferrer">https://www.tensorflow.org/tutorials/generative/autoencoder</a></p>
  <p>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 :)</p>
  <pre><code>x_train = x_train[..., tf.newaxis]
  x_test = x_test[..., tf.newaxis]
  </code></pre>
  <p>Then the encoder is built from the following layers:</p>
  <pre><code> 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')])
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-30T19:25:45.263Z
Comments.Text: This question is really at the limit of being on-topic, but I will leave it open.
Comments.UserDisplayName: nbro
Tags: <convolutional-neural-networks><tensorflow><autoencoders>
Title: Why do we add additional axis in CNN autoencoder while denoising?
UserDisplayName: Maciek Woźniak
ThreadText: # Why do we add additional axis in CNN autoencoder while denoising?
  
  (Asked by: username_0 on 2020-10-29)
  
  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')])
  
  ```
  
  **Tags:** convolutional-neural-networks, tensorflow, autoencoders
  
  > **Comments:**
  > **username_1**: This question is really at the limit of being on-topic, but I will leave it open.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-02-21. Score: 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](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D) 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.
  
  
  ---
  
  