pytorch 无法打乱数据集

pytorch can't shuffle the dataset

我正在尝试使用来自 torchvision 的 mnist 数据集制作一个 ai,并使用 pytorch 制作它,但是当我键入一些打乱数据的代码时,运行 它说:

    trainset = torch.utils.data.Dataloader(train, batch_size=10, shuffle=True)
AttributeError: module 'torch.utils.data' has no attribute 'Dataloader'

我尝试了一种不同的方法,但它仍然不起作用,它说:

    trainset = torch.autograd.Variable.DataLoader(train, batch_size=10, shuffle=True) 
AttributeError: type object 'Variable' has no attribute 'DataLoader'

我使用的代码是:

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

此代码的错误:

    trainset = torch.utils.data.Dataloader(train, batch_size=10, shuffle=True)
AttributeError: module 'torch.utils.data' has no attribute 'Dataloader'

我试了一个新版本,但还是不行:

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

此代码的错误:

    trainset = torch.autograd.Variable.DataLoader(train, batch_size=10, shuffle=True) 
AttributeError: type object 'Variable' has no attribute 'DataLoader'

我仍然很困惑为什么它不起作用,我正在学习教程但它不起作用

您有一个简单的错字:Dataloader -> DataLoader(大写 L)。

尝试:

 trainset = torch.utils.data.DataLoader(train, batch_size=10, shuffle=True)