DANN: RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

When I run your code, I have met a error when executing data_source = data_source_iter.next():

RuntimeError: output with shape [1, 28, 28] doesn’t match the broadcast shape [3, 28, 28].

I have no idea why this error occurs. Could you please give some suggestions. Besides, I’m using Python3.6 and Pytorch 1.0. My operating system is Ubuntu 16.04. Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I’m working on Ubuntu 16.04 version and using torch version 1.3.1 It’s that MNIST data set consists of grey images.

transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)), ])

can be changed to

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

Let me clarify, if the img has three channels, you should have three number for mean, for example, img is RGB, mean is [0.5, 0.5, 0.5], the normalize result is Rx0.5, Gx0.5, Bx0.5. If img is grey type that only one channel, so mean should be [0.5], the normalize result is R*0.5

Downgrading torch and torchvision to 0.2.0 and 0.2.1 solved this issue for me.

Ooooooh, finally, I run the code successfully. I add a new transform like this

img_transform1 = transforms.Compose([
    transforms.Resize(image_size),
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x.repeat(3,1,1)),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])

And I replace the original transform of source data mnist with img_transform1. By do so, the mnist images can be converted to [3, *, *] tensors. I am curious about why the code is OK under your settings. In my opinion, is there a difference about torchvision.datasets.MNIST() function between torch 0.4 and torch 1.0???