Python MNIST数据集无法转换为张量对象

Python MNIST数据集无法转换为张量对象,python,numpy,machine-learning,pytorch,Python,Numpy,Machine Learning,Pytorch,如何正确地将MNIST数据集转换为张量类型?我在下面试过,但没有成功。错误消息AttributeError:'int'对象没有属性'type'表明它不是张量类型 下面的代码可以在Google Colab中测试 PyTorch 1.3.1版似乎可以运行此功能,但不能运行1.5.1版 >>> import torch >>> import torch.nn as nn >>> import torchvision.transforms as tr

如何正确地将MNIST数据集转换为张量类型?我在下面试过,但没有成功。错误消息
AttributeError:'int'对象没有属性'type'
表明它不是张量类型

下面的代码可以在Google Colab中测试

PyTorch 1.3.1版似乎可以运行此功能,但不能运行1.5.1版

>>> import torch
>>> import torch.nn as nn
>>> import torchvision.transforms as transforms
>>> import torchvision.datasets as dsets
>>> import numpy as np
>>> torch.__version__
1.5.1+cu101

>>> train_dataset = dsets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor())
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./data/MNIST/raw/train-images-idx3-ubyte.gz
100.1%Extracting ./data/MNIST/raw/train-images-idx3-ubyte.gz to ./data/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./data/MNIST/raw/train-labels-idx1-ubyte.gz
113.5%Extracting ./data/MNIST/raw/train-labels-idx1-ubyte.gz to ./data/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw/t10k-images-idx3-ubyte.gz
100.4%Extracting ./data/MNIST/raw/t10k-images-idx3-ubyte.gz to ./data/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz
180.4%Extracting ./data/MNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/MNIST/raw
Processing...
/pytorch/torch/csrc/utils/tensor_numpy.cpp:141: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program.
Done!

>>> print("Print the training dataset:\n ", train_dataset)
Print the training dataset:
  Dataset MNIST
    Number of datapoints: 60000
    Root location: ./data
    Split: Train
    StandardTransform
Transform: ToTensor()

>>> print("Type of data element: ", train_dataset[0][1].type())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'type'

导入火炬 >>>导入torch.nn作为nn >>>导入torchvision.transforms作为变换 >>>将torchvision.dataset导入为数据集 >>>将numpy作为np导入 >>>手电筒__ 1.5.1+cu101 >>>train_dataset=dsets.MNIST(root='./data',train=True,download=True,transform=transforms.ToTensor()) 正在下载http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz 至./data/MNIST/raw/train-images-idx3-ubyte.gz 100.1%提取./data/MNIST/raw/train-images-idx3-ubyte.gz至./data/MNIST/raw 正在下载http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz 至./data/MNIST/raw/train-labels-idx1-ubyte.gz 113.5%提取./data/MNIST/raw/train-labels-idx1-ubyte.gz至./data/MNIST/raw 正在下载http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz 至./data/MNIST/raw/t10k-images-idx3-ubyte.gz 100.4%提取./data/MNIST/raw/t10k-images-idx3-ubyte.gz至./data/MNIST/raw 正在下载http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz 至./data/MNIST/raw/t10k-labels-idx1-ubyte.gz 180.4%提取./data/MNIST/raw/t10k-labels-idx1-ubyte.gz至./data/MNIST/raw 处理。。。 /pytorch/torch/csc/utils/tensor_numpy.cpp:141:UserWarning:给定的numpy数组不可写,pytorch不支持不可写的张量。这意味着您可以使用张量写入底层(假定不可写)NumPy数组。在将数组转换为张量之前,可能需要复制数组以保护其数据或使其可写。在本程序的其余部分,此类警告将被抑制。 完成! >>>打印(“打印训练数据集:\n”,训练数据集) 打印培训数据集: 数据集MNIST 数据点数量:60000 根位置:./data 分开:火车 标准变换 Transform:ToTensor() >>>打印(“数据元素类型:”,列数据集[0][1]。类型() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 AttributeError:“int”对象没有属性“type”
您需要访问Ist元素(对应于图像张量),而不是第二个元素(标签),即


谢谢,但是为什么标签不是1.5.1版中的
LongTensor
>>> print("Type of data element: ", train_dataset[0][0].type())
Type of data element:  torch.FloatTensor

>>> print(train_dataset[0][0].shape, train_dataset[0][1])
(torch.Size([1, 28, 28]), 5)