PyTorch,AttributeError:模块“形状”没有属性“”

2024-03-29 04:42:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想在GPU上运行MNIST代码。在我的conda环境中,python的版本是3.8,而PyTorch的版本是1.8,因此它们都得到了相当的更新。示例_数据是MNIST数据集,我能够得到torch.Size([1000,1,28,28])作为Jupyter实验室上示例_data.shape的结果

那么,我收到这个错误消息的原因是什么?谢谢

Traceback (most recent call last):
  File "MNIST5.py", line 45, in <module>
    example_data.shape
AttributeError: 'torch.FloatTensor' object has no attribute 'shape'

下面是导致问题的代码部分。我从网上的一个教程中得到了它,它基本上只是下载了数据集,并试图得到它的形状

train_loader = torch.utils.data.DataLoader(
  torchvision.datasets.MNIST('/files/', train=True, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize(
                                 (0.1307,), (0.3081,))
                             ])),
  batch_size=batch_size_train, shuffle=True)

examples = enumerate(test_loader)

batch_idx, (example_data, example_targets) = next(examples)
example_data.shape

Tags: 数据代码版本true示例dataexamplebatch