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

RuntimeError: output with shape [1, 28, 28] doesn’t match the broadcast shape [3, 28, 28]
在用pytorch导入mnist数据时出现了上述错误,
报错程序:

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

报错原因:这是因为mnist图像都是灰度图像,只有一个通道,而上面的transforms.Normalize 却对三个通道都归一化了,这肯定会报错,所以只要像下面修改即可:

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

你可能感兴趣的:(21个项目玩转深度学习,笔记,python程序报错问题集)