I'm following the introductory MIT Deep Learning course on Youtube and i've been stuck for a day now on this piece of code:

self.encoder = make_standard_classifier(num_encoder_dims)
self.decoder = make_face_decoder_network()

def encode(self, x):
  # encoder output
  encoder_output = self.encoder(x)

  # classification prediction
  y_logit = tf.expand_dims(encoder_output[:, 0], -1)
  # latent variable distribution parameters
  z_mean = encoder_output[:, 1:self.latent_dim+1] 
  z_logsigma = encoder_output[:, self.latent_dim+1:]

return y_logit, z_mean, z_logsigma

This is the encoder network in a VAE. I don't understand what is exaclty doing tf.expand_dims, is it adding a dimension to the encoder output? Or it also taking the value in encoder_output[:,0] and returning it?

The second question is about z_mean and z_logsigma. Why are they in those positions of the output? Shouldn't they be computed on it? The encoder is a standard one so no particular output, it has as last Layer a Dense one with 2*latent dim + 1 outputs.

This is the url to the code https://github.com/aamini/introtodeeplearning/blob/master/lab2/solutions/Part2_Debiasing_Solution.ipynb

I know it may be a trivial question but at the moment this is the very big difficulty i've found in this course. Thank you very much!

  • 1
    $\begingroup$ Hello. Is this problem due to the fact that 1. you're not familiar with TensorFlow or 2. you're trying to understand this implementation and match it to your theoretical understanding of the VAE? $\endgroup$ – nbro May 14 at 21:38
  • 1
    $\begingroup$ Actually both i guess. I managed to build a binary classification CNN and got no problem with dimensions, but i think this is a more difficult scenario and maybe that's why i'm not really understanding what it's doing. Why in this case the output has it's classification value (y_logit) in that particular position? How is it extracted by the use of expand_dims? The second doubt is on z_mean and z_logsigma. Shouldn't they be computed on all the output values? Instead they are in that particular positions of the output. $\endgroup$ – Lorenzo Lanari May 15 at 8:42