Hello,
I’m new to the world of pytorch and machine learning.
I code a small project and I made a very simplified version here, if someone wants to help me to understand the problem.
I have tens of thousands of categories.
In this example, I’m trying to get the categories 42, 4300, 55000 for an input of 0, 0, 0.
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
outputExcepted = torch.tensor([[42, 4300, 55000]])
data = torch.tensor([[0, 0, 0]], dtype=torch.float32)
class FCNet(nn.Module):
def __init__(self):
super(FCNet, self).__init__()
self.layer1 = nn.Linear(3, 3)
def forward(self, x):
x = torch.relu(self.layer1(x))
return x
model = FCNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)
num_epochs = 10000
model.train()
for epoch in range(num_epochs):
optimizer.zero_grad()
outputs = model(data)
loss = criterion(outputs, outputExcepted.float())
loss.backward()
optimizer.step()
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
The first concern is that the neural network does not converge:
Epoch [10000/10000], Loss: 20850.6680
I also wonder why I have to pass floats to the criterion function :
loss = criterion(outputs, outputExcepted.float())
If I don’t, I get this error:
RuntimeError: Expected floating point type for target with class probabilities, got Long
But on the documentation of CrossEntropyLoss I understand that you have to send him Long.
I guess it’s not a problem with a very complicated cause but there is surely a point that I haven’t learned and it blocks my little projects.
It would be great if someone could help me!
Thanks a lot,