diegoalejogm / gans

Generative Adversarial Networks implemented in PyTorch and Tensorflow
MIT License
824 stars 352 forks source link

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

Closed mswiniar closed 3 years ago

mswiniar commented 5 years ago

Hi, I'm getting an error while running your Vanilla GAN PyTorch.ipynb notebook.

RuntimeError Traceback (most recent call last)

in 2 3 for epoch in range(num_epochs): ----> 4 for n_batch, (real_batch,_) in enumerate(data_loader): 5 6 # 1. Train Discriminator C:\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __next__(self) 613 if self.num_workers == 0: # same-process loading 614 indices = next(self.sample_iter) # may raise StopIteration --> 615 batch = self.collate_fn([self.dataset[i] for i in indices]) 616 if self.pin_memory: 617 batch = pin_memory_batch(batch) C:\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in (.0) 613 if self.num_workers == 0: # same-process loading 614 indices = next(self.sample_iter) # may raise StopIteration --> 615 batch = self.collate_fn([self.dataset[i] for i in indices]) 616 if self.pin_memory: 617 batch = pin_memory_batch(batch) C:\anaconda3\lib\site-packages\torchvision\datasets\mnist.py in __getitem__(self, index) 93 94 if self.transform is not None: ---> 95 img = self.transform(img) 96 97 if self.target_transform is not None: C:\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img) 58 def __call__(self, img): 59 for t in self.transforms: ---> 60 img = t(img) 61 return img 62 C:\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, tensor) 161 Tensor: Normalized Tensor image. 162 """ --> 163 return F.normalize(tensor, self.mean, self.std, self.inplace) 164 165 def __repr__(self): C:\anaconda3\lib\site-packages\torchvision\transforms\functional.py in normalize(tensor, mean, std, inplace) 206 mean = torch.tensor(mean, dtype=torch.float32) 207 std = torch.tensor(std, dtype=torch.float32) --> 208 tensor.sub_(mean[:, None, None]).div_(std[:, None, None]) 209 return tensor 210 RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28] Packages installed: torch == 1.0.1 tensorflow == 1.13.1 tensorboardX == 1.6 numpy == 1.16.2 matplotlib == 3.0.3 jupyter == 1.0.0 Is it about running a script on newer version of packages? Sorry, I'm quite new in those techniques. Packages in requirements.txt are not anymore available. Thanks very much, your time is appreciated!
daiiyu commented 5 years ago

change the funtion mnist_data to this:

def mnist_data():
    compose = transforms.Compose([
        transforms.ToTensor(), transforms.Normalize([0.5], [0.5])])
    out_dir = '{}/dataset'.format(DATA_FOLDER)
    return datasets.MNIST(root=out_dir, train=True, transform=compose, download=True)
diegoalejogm commented 3 years ago

Thanks for fixing this. I've updated the repo