MNIST dataset is failed to transform as tensor object
How to properly transform the MNIST dataset to tensor type? I tried below but not work. The error message AttributeError: 'int' object has no attribute 'type' indicates it is not tensor type.
The codes below can be tested in Google Colab.
It appears that PyTorch Version 1.3.1 can run this, but not for 1.5.1.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; import torch.nn as nn
&gt;&gt;&gt; import torchvision.transforms as transforms
&gt;&gt;&gt; import torchvision.datasets as dsets
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; torch.__version__
1.5.1+cu101

&gt;&gt;&gt; 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!

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

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



You need to access Ist element (corresponding to image tensors), not the second one (labels) i.e.
&gt;&gt;&gt; print(&quot;Type of data element: &quot;, train_dataset[0][0].type())
Type of data element:  torch.FloatTensor

&gt;&gt;&gt; print(train_dataset[0][0].shape, train_dataset[0][1])
(torch.Size([1, 28, 28]), 5)

