The expanded size of the tensor (1) must match the existing size (3) at non-singleton dimension 1

Hi,
I encountered the following error. How should I modify the code?

Traceback (most recent call last):
  File "E:\Wx\pixelda\pixelda2.py", line 261, in <module>
    imgs_A = Variable(imgs_A.type(FloatTensor).expand(batch_size, 1, opt.img_size, opt.img_size))
RuntimeError: The expanded size of the tensor (1) must match the existing size (3) at non-singleton dimension 1
[Finished in 4.3s with exit code 1]

This is a code snippet:

 # Configure input
        imgs_A = Variable(imgs_A.type(FloatTensor).expand(batch_size, 1, opt.img_size, opt.img_size))
        labels_A = Variable(labels_A.type(LongTensor))
        imgs_B = Variable(imgs_B.type(FloatTensor))

tensor.expand returns a new view of the tensor with singleton dimensions expanded to a larger size.
Based on your code snippet I assume you would like to expand the batch dimension.
If that’s the case, you would need to keep the channel, height, and width dimension equal, which is not the case in your code snippet.
imgs_A seems to have 3 channels, while you are passing 1 to the expand operation in dim1.

PS: Variables are deprecated since PyTorch 0.4, so you can use tensors in newer versions. :wink: