nn.Upsample not working right

I am trying to use nn.Upsample to make image tensor twice bigger.
But it seems only upsample the width.
Below is the code I use:

u = nn.Upsample(scale_factor=2)
t = torch.randn([1, 28, 28])
print(u(t).shape) # output shape is [1, 28, 56], I want it to be [1, 56, 56]


nn.Upsample will consider the channel dimension, which in pytorch is the dimension in second index.
The channel dimension will not be doubled, but the dimension after this will be doubled, which are height and weight dim. This is based on the following notation

(Batch_dim, Channel_dim, Height_dim, Width_dim)

In your problem, adding the channel dimension would be enough

u = nn.Upsample(scale_factor=2)
t = torch.randn([1, 1, 28, 28])
print(u(t).shape) # output shape is [1, 1, 56, 56]

If you dont want channel dimension, you can get away with using view to add channel dim

1 Like

Got it. Thank you so much!