【Bug】PyTorch DataLoader - “IndexError: too many indices for tensor of dimension 0“

【Bug】PyTorch DataLoader - “IndexError: too many indices for tensor of dimension 0“_第1张图片

import torch
import torchvision
import torchvision.transforms as transforms

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

trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=20, shuffle=True, num_workers=2)

testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=20, shuffle=False, num_workers=2)

for i, data in enumerate(trainloader, 0):
    inputs, labels = data[0], data[1]

需要修改transforms.Normalize((0.5),(0.5)),0.5后面加逗号,
问题分析:可能是pytorch版本导致的。
将代码改为:

img_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])
type((0.5))   # 
type((0.5,))  # 

参考:

python - PyTorch DataLoader - "IndexError: too many indices for tensor of dimension 0" - Stack Overflow

(24条消息) Pytorch-IndexError: too many indices for tensor of dimension 0_Virogous的博客-CSDN博客

你可能感兴趣的:(debug,pytorch,深度学习,机器学习)