问题
i am trying to make an ai with the mnist dataset from torchvision and makeing it with pytorch, but when i type some of the code that shuffle the data, and run it, it say:
trainset = torch.utils.data.Dataloader(train, batch_size=10, shuffle=True)
AttributeError: module 'torch.utils.data' has no attribute 'Dataloader'
i tried a difrent method but it still do not work, and it says:
trainset = torch.autograd.Variable.DataLoader(train, batch_size=10, shuffle=True)
AttributeError: type object 'Variable' has no attribute 'DataLoader'
the code i use is:
import torch
import numpy as np
import torchvision
from torchvision import transforms, datasets
train = datasets.MNIST("", train=True, download=True,
transform = transforms.Compose([transforms.ToTensor()]))
test = datasets.MNIST("", train=False, download=True,
transform = transforms.Compose([transforms.ToTensor()]))
trainset = torch.utils.data.Dataloader(train, batch_size=10, shuffle=True)
testset = torch.utils.data.Dataloader(test, batch_size=10, shuffle=True)
for data in trainset:
print(data)
break
the eror from this code:
trainset = torch.utils.data.Dataloader(train, batch_size=10, shuffle=True)
AttributeError: module 'torch.utils.data' has no attribute 'Dataloader'
i tried a new version but it still not working:
import torch
import numpy as np
import torchvision
from torchvision import transforms, datasets
train = datasets.MNIST("", train=True, download=True,
transform = transforms.Compose([transforms.ToTensor()]))
test = datasets.MNIST("", train=False, download=True,
transform = transforms.Compose([transforms.ToTensor()])
trainset = torch.autograd.Variable.DataLoader(train, batch_size=10, shuffle=True)
testset = torch.autograd.Variable.DataLoader(test, batch_size=10, shuffle=True)
for data in trainset:
print(data)
break
the eror from this code:
trainset = torch.autograd.Variable.DataLoader(train, batch_size=10, shuffle=True)
AttributeError: type object 'Variable' has no attribute 'DataLoader'
i am stil confused why it is not working,i was folowing a tutorial but it did not work
回答1:
You have a simple typo: Dataloader
-> DataLoader (capital L
).
Try:
trainset = torch.utils.data.DataLoader(train, batch_size=10, shuffle=True)
来源:https://stackoverflow.com/questions/65181608/pytorch-cant-shuffle-the-dataset