Hello,
I am trying to learn how to use PyTorch. I am having some difficulties using the data loaders.
Can anyone tell me if my code looks ok?
Because during training the output tensors are all zero and loss is always the same.
I was wondering if there is something wrong with the way I am loading the data. Generally, I want to load the data by myself, not via torchvision.datasets. (I am loading the inputs-targets from a file that is meant to be used in torch, that is why I am converting label values from 1-10 to 0-9).
Thank you.
Here is the code:
from future import print_function
import argparse
import torch
import torch.utils.data as utils_data
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
import numpy as np
import torchfile
class MyConvNet(nn.Module):
def __init__(self):
    super(MyConvNet, self).__init__()
    self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=0)
    self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)
    self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=0)
    self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)
    self.fc1 = nn.Linear(1600, 500)
    self.fc2 = nn.Linear(500, 10)

def forward(self, input):
    x = self.pool1(F.relu(self.conv1(input)))
    x = self.pool2(F.relu(self.conv2(x)))
    x = x.view(x.size(0), -1)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    return x

net = MyConvNet()
print(net)
model=MyConvNet()
model.cuda()
patches=torchfile.load(’/data1/04_MP/test_pytorch/cifar-10-batches-t7/train.t7’)
labels=torchfile.load(’/data1/04_MP/test_pytorch/cifar-10-batches-t7/train_t.t7’)
for i in range(labels.shape[0]):
if labels[i]==1:
labels[i]=0
if labels[i]==2:
labels[i]=1
if labels[i]==3:
labels[i]=2
if labels[i]==4:
labels[i]=3
if labels[i]==5:
labels[i]=4
if labels[i]==6:
labels[i]=5
if labels[i]==7:
labels[i]=6
if labels[i]==8:
labels[i]=7
if labels[i]==9:
labels[i]=8#
if labels[i]==10:
labels[i]=9
tensor_input=torch.from_numpy(patches)
tensor_target=torch.from_numpy(labels)
training_samples = utils_data.TensorDataset(tensor_input.float(), tensor_target.long())
data_loader = utils_data.DataLoader(training_samples, batch_size=100, shuffle=True)
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
criterion=nn.CrossEntropyLoss()
for epoch in range(40):
print(’\nEpoch: %d’ % epoch)
model.train()
train_loss=0.0
for i, (inputs, targets) in enumerate(data_loader,0):
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets)
optimizer.zero_grad
outputs=model(inputs)
print (outputs)
loss=criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.data[0]
print(’[%d, %5d] loss: %.3f’ % (epoch+1, i+1, train_loss / 500))
print(‘Finished Training’)