Hi, I am new to deeplearning and pytorch, I write a very simple demo, but the loss can’t decreasing when training.
Any comments are highly appreciated!
I want to use one hot to represent group and resource, there are 2 group and 4 resouces in training data:
group1 (1, 0) can access resource 1(1, 0, 0, 0) and resource2(0, 1, 0, 0)
group2(0, 1) can access resource3(0, 0, 1, 0) and resource4(0, 0, 0, 1)
result is true(0) and false(1)
So the first line in input [1, 0, 1, 0, 0, 0] mean group1 can acess resource1, the first value in label is 0, mean true.
All the code is list below:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
from torch.autograd import Variable
import numpy as np
import random
from torch.utils.data import DataLoader, Dataset
input_size = 6
hidden_size = 6
num_classes = 2
num_epochs = 300
batch_size = 1
learning_rate = 0.01
training_data = torch.FloatTensor([[1, 0, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 1]])
training_label = torch.LongTensor([0, 0, 1, 1, 1, 1, 0, 0])
class MyDataset(Dataset):
def __init__(self, datas, labels):
self.datas = datas
self.labels = labels
def __getitem__(self, index):
data, target = self.datas[index], self.labels[index]
return data, target
def __len__(self):
return len(self.datas)
train_dataloader = DataLoader(MyDataset(training_data, training_label), batch_size=batch_size)
class Net(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(Net, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.fc2(out)
out = self.fc3(out)
return F.log_softmax(out, dim=1)
net = Net(input_size, hidden_size, num_classes)
criterion = nn.NLLLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)
# Train the Model
for epoch in range(num_epochs):
for data, labels in train_dataloader:
# Convert torch tensor to Variable
data = Variable(data)
labels = Variable(labels)
# Forward + Backward + Optimize
optimizer.zero_grad() # zero the gradient buffer
outputs = net(data)
loss = criterion(outputs, labels.long())
loss.backward()
optimizer.step()
print ('Epoch [%d/%d], Loss: %.4f'
%(epoch+1, num_epochs, loss.data[0]))