[C2W3_Assignment] Components of StyleGAN: UNQ_C4, AdaIN, IndexError: too many indices for tensor of dimension 2

Hi,

I am working on [C2W3_Assignment] Components of StyleGAN: UNQ_C4, AdaIN.

I got an error: in forward(self, image, w)
36 print(w.shape)
37 print(self.style_scale_transform(w).shape)
—> 38 style_scale = self.style_scale_transform(w)[:, :, image.shape[1], w.shape]
39 style_shift = self.style_shift_transform(w)[:, :, None, None]
40

IndexError: too many indices for tensor of dimension 2

My code is:

    self.style_scale_transform = nn.Linear(w_dim, channels)
    self.style_shift_transform = nn.Linear(w_dim, channels)

    style_scale = self.style_scale_transform(w)[:, :, image.shape[1], w.shape]
    style_shift = self.style_shift_transform(w)[:, :, None, None]

What is “style_scale = self.style_scale_transform(w)[:, :, image.shape[1], w.shape]” for? Why there are so many dimensions?

Thanks,

Wei

Hi Wei_Xia!
Hope you are doing well.

The code that you have shared does not belong to the editable part of the notebook. That is, it shouldn’t be edited.
It should be :
style_scale = self.style_scale_transform(w)[:, :, None, None]
style_shift = self.style_shift_transform(w)[:, :, None, None]

Please do not modify the other part of the notebook which doesn’t belong to the editable part.

Moreover, w.shape will be a tuple which cannot be used as an index (the code itself doesn’t give any meaning as such).
Hope this solves your problem. Have a good day!
Regards,
Nithin

Hi Muhammad!
I’m afraid that the solution you have provided might be misleading because of the following reasons

  • This will give the shape of style_scale as (number of samples, image channels) :: ndim 2. Without giving out many details about the code :: the next step in the logic requires us to multiply (element-wise) style_scale with normalized image whose ndim is 4.(I don’t want reveal any more details on this as this belongs to the learner’s implementation part).
  • Plus, this portion of the code does not belong to the editable part. They have given the correct code already in the notebook. (i.e.)
    style_scale = self.style_scale_transform(w)[:, :, None, None]
    style_shift = self.style_shift_transform(w)[:, :, None, None]

Hence I request you, to delete your post so that we don’t confuse Wei.
Thank you.
Regards,
Nithin

1 Like

Thank you for your help! I might have accidentally edit the part I should not edit.

Wei