Hi everyone! I am having problems in reading MNIST training csv file (size: 60000* 785, the first column is the label, where I downloaded through the link: https://pjreddie.com/projects/mnist-in-csv/ ) from my local computer.
The following is the detail information, could somebody help me to identify what is the cause, thanks in advance!
- I am running Pytorch in Win10 with pytorch-0.4.1, python 3.6.6;
- My source codes:
''' -*- coding: utf-8 -*-'''
import torch
import torch.nn as nn
from skimage import transform
import torchvision.transforms as transforms
from torch.autograd import Variable
import numpy as np
from torch.utils.data import Dataset
''' Ignore warnings'''
import warnings
warnings.filterwarnings("ignore")
''' Hyper Parameters'''
input_size = 784
hidden_size1 = 500
hidden_size2 = 300
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
class NosiyMNISTDataset(Dataset):
def __init__(self):
xy = np.loadtxt('./data_MNIST/mnist_train.csv',
delimiter=',', dtype=np.float32)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 1:])
self.y_data = torch.from_numpy(xy[:, [0]])
def __getitem__(self, index):
return (self.x_data[index], self.y_data[index])
def __len__(self):
return self.len
''' MNIST Dataset '''
train_dataset = NosiyMNISTDataset()
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers = 2)
''' Neural Network Model (2 hidden layer)'''
class Net(nn.Module):
def __init__(self, input_size, hidden_size1, hidden_size2, num_classes):
super(Net, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size1)
self.fc2 = nn.Linear(hidden_size1, hidden_size2)
self.fc3 = nn.Linear(hidden_size2, num_classes)
self.relu = nn.ReLU()
# self.dropout = nn.Dropout()
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
# out = self.dropout(out)
out = self.fc2(out)
out = self.relu(out)
# out = self.dropout(out)
out = self.fc3(out)
return out
if __name__ == '__main__':
net = Net(input_size, hidden_size1, hidden_size2, num_classes)
net.cuda()
''' Loss and Optimizer'''
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr = learning_rate)
'''Train the Model'''
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader,0):
# Convert torch tensor to Variable
images = Variable(images.view(-1, 28*28).cuda())
labels = Variable(labels.cuda())
# Forward + Backward + Optimize
optimizer.zero_grad() # zero the gradient buffer
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
- The error I got is:
"Traceback (most recent call last):
File "Local_MNIST.py", line 92, in <module>
loss = criterion(outputs, labels)
................
RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.FloatTensor for argument #2 'target'"
I have tried
<1> . change the ādtypeā in my NosiyMNISTDataset class to ānp.floatā, ānp.longā, and ānp.int64ā
<2>. add long() to torch.from_numpy(xy[:, [0]]) as torch.from_numpy(xy[:, [0]]).long()
<3> remove cuda() from my codes
All the three ways do not solve my problem.
P.S. I know there is a standard way to laod MNIST dataset, however, I would like to modify dataset thatās why I am using csv file.
Looking forward to you guys comments.
Thanks!