Hello,
I’m getting an error that I’m having trouble solving. It’s a binary classification problem on tabular data. It’s something to do with the dimension(I think ) on the log_softmax
. Below is the error.
Traceback (most recent call last):
File "<ipython-input-4-626b771bc4cb>", line 39, in <module>
outputs = model1(x)
File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "<ipython-input-4-626b771bc4cb>", line 14, in forward
x = F.log_softmax(x,dim=2)
File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\functional.py", line 1591, in log_softmax
ret = input.log_softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
Here is the model:
class model(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(D, 10)
self.fc2 = nn.Linear(10, 5)
self.fc3 = nn.Linear(5, 2)
def forward(self, x_train):
x = self.fc1(x_train)
x = self.fc2(x)
x = self.fc3(x)
x = F.log_softmax(x, dim=2)
return x
model1 = model()
# Loss and optimizer
criterion = nn.NLLLoss()
optimizer = torch.optim.Adam(model1.parameters())
Here is the training loop:
# Train the model
n_epochs = 1000
# Stuff to store
train_losses = np.zeros(n_epochs)
test_losses = np.zeros(n_epochs)
for it in range(n_epochs):
# zero the parameter gradients
optimizer.zero_grad()
# Forward pass
outputs = model1(x)
loss = criterion(outputs, y)
# Backward and optimize
loss.backward()
optimizer.step()
# Get test loss
outputs_test = model1(test_x)
loss_test = criterion(outputs_test, test_y)
# Save losses
train_losses[it] = loss.item()
test_losses[it] = loss_test.item()
if (it + 1) % 50 == 0:
print(f'Epoch {it+1}/{n_epochs}, Train Loss: {loss.item():.4f}, Test Loss: {loss_test.item():.4f}')
Thanks for any help.