pytorch can't shuffle the dataset [closed]

梦想的初衷 提交于 2020-12-15 03:52:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!