Pytorch Classifier model doesn't converge

k_ryna: <p>Hello dear community. I’m trying to write a simple classifier that is predicting the letter based on input but it doesn’t see to converge. The loss is still 2.5 after all. I tried changing batch size, number of layers and other parameters but it didn’t change. I’m new to NN and can’t figure out what exactly is wrong. Hope you have some tips for me!</p>
<p><strong>process_data.py</strong></p>
<pre><code class="lang-auto">import csv
from torch.utils.data import Dataset, DataLoader
from typing import List
import numpy as np

class MyData(Dataset):
    def __init__(self, path: str = "data_ac_full.csv"):
        labels, samples = MyData.read_label_samples_from_csv(path)
        self.np_samples = np.array(samples, dtype=np.float32)
        self._labels = np.array(labels, dtype=np.uint8)

    def __len__(self):
        return len(self._labels)

    def __getitem__(self, idx):
        return {"points": self.np_samples[idx], "labels": self._labels[idx]}
    
    @staticmethod
    def read_label_samples_from_csv( path: str):
        mapping = {"A": 1, "B":2, "C": 3} # all of the letters except for 'J' and 'Z'

        labels, samples = [], []
        with open(path) as f:
            _ = next(f) # skip header
            for line in csv.reader(f):
                label = line[0]
                labels.append(mapping[label])
                splitted = [MyData.split_samples(x) for x in line[2:]]
                splitted = list(np.concatenate(splitted).flat)
                samples.append(splitted)
        return labels, samples
    
    @staticmethod
    def split_samples(sample):
        y_pos = sample.find("y:")
        z_pos = sample.find("z:")
        x = float(sample[3:y_pos])
        y = float(sample[y_pos + 3 : z_pos])

        return [x, y]


def read_dataset(batch_size=32, ):
    trainset = MyData()
    trainloader = DataLoader(
        trainset, batch_size=batch_size, shuffle=True
    )

    testset = MyData()
    testloader = DataLoader(
        testset, batch_size=batch_size, shuffle=False
    )
    return trainloader, testloader


if __name__ == "__main__":
    data = read_dataset()
</code></pre>
<p><strong>train.py</strong></p>
<pre><code class="lang-auto">import torch.nn as nn
import torch
import torch.optim as optim
from torch.autograd import Variable

from process_data import read_dataset

input_dim = 42 
hidden_layers = 100 
output_dim = 25 

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Linear(input_dim, hidden_layers)
        self.act1 = nn.ReLU() 
        self.layer2 = nn.Linear(hidden_layers, hidden_layers) 
        self.act2 = nn.ReLU()
     
        self.output = nn.Linear(hidden_layers, output_dim) 
        self.softmax = nn.Softmax(dim=1)
 
    def forward(self, x):
        m = self.layer1(x) 
        x = self.act1(m)
        x = self.act2(self.layer2(x))

        x = self.softmax(self.output(x))
        return x


def train(net, criterion, optimizer, trainloader, epoch):
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs = Variable(data["points"].float())
        labels = Variable(data['labels'])
        # remove previous epoch gradients
        optimizer.zero_grad()
        # forward propagation
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        # backward propagation
        loss.backward()
        # optimize
        optimizer.step()

        running_loss += loss.item()
        print('[epoch: %d, batch: %5d] loss: %.6f' % (epoch, i, running_loss / (i + 1)))



def main():
    trainloader, testloader = read_dataset(batch_size=300)

    net = Net().float()
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)

    for epoch in range(10):  # loop over the dataset
        train(net, criterion, optimizer, trainloader, epoch)
        scheduler.step()

    torch.save(net.state_dict(), "saved.pth")


if __name__ == '__main__':
    main()

</code></pre>
<p><strong>if batch size = 32</strong><br>
Labels  = [1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 3, 1, 1, 3, 3, 1, 1, 3, 1]<br>
Inputs size = torch.Size([32, 63])</p>
<p><strong>Output of train.py:</strong><br>
[epoch: 0, batch:     0] loss: 3.213218<br>
[epoch: 0, batch:     1] loss: 3.213202<br>
[epoch: 0, batch:     2] loss: 3.213065<br>
[epoch: 0, batch:     3] loss: 3.212979<br>
[epoch: 0, batch:     4] loss: 3.212907<br>
[epoch: 0, batch:     5] loss: 3.212822<br>
[epoch: 0, batch:     6] loss: 3.212732<br>
[epoch: 0, batch:     7] loss: 3.212626<br>
[epoch: 0, batch:     8] loss: 3.212534<br>
[epoch: 1, batch:     0] loss: 3.211746<br>
[epoch: 1, batch:     1] loss: 3.211465<br>
[epoch: 1, batch:     2] loss: 3.211172<br>
[epoch: 1, batch:     3] loss: 3.211001<br>
[epoch: 1, batch:     4] loss: 3.210788<br>
[epoch: 1, batch:     5] loss: 3.210574<br>
[epoch: 1, batch:     6] loss: 3.210420<br>
[epoch: 1, batch:     7] loss: 3.210246<br>
[epoch: 1, batch:     8] loss: 3.210095<br>
[epoch: 2, batch:     0] loss: 3.208078<br>
[epoch: 2, batch:     1] loss: 3.207820<br>
[epoch: 2, batch:     2] loss: 3.207630<br>
[epoch: 2, batch:     3] loss: 3.207447<br>
[epoch: 2, batch:     4] loss: 3.207174<br>
[epoch: 2, batch:     5] loss: 3.206867<br>
[epoch: 2, batch:     6] loss: 3.206626<br>
[epoch: 2, batch:     7] loss: 3.206323<br>
[epoch: 2, batch:     8] loss: 3.206030<br>
[epoch: 3, batch:     0] loss: 3.203678<br>
[epoch: 3, batch:     1] loss: 3.202639<br>
[epoch: 3, batch:     2] loss: 3.202355<br>
[epoch: 3, batch:     3] loss: 3.201975<br>
[epoch: 3, batch:     4] loss: 3.201828<br>
[epoch: 3, batch:     5] loss: 3.201533<br>
[epoch: 3, batch:     6] loss: 3.201332<br>
[epoch: 3, batch:     7] loss: 3.200817<br>
[epoch: 3, batch:     8] loss: 3.200394<br>
[epoch: 4, batch:     0] loss: 3.196789<br>
[epoch: 4, batch:     1] loss: 3.196557<br>
[epoch: 4, batch:     2] loss: 3.195881<br>
[epoch: 4, batch:     3] loss: 3.195361<br>
[epoch: 4, batch:     4] loss: 3.194702<br>
[epoch: 4, batch:     5] loss: 3.194068<br>
[epoch: 4, batch:     6] loss: 3.193679<br>
[epoch: 4, batch:     7] loss: 3.192990<br>
[epoch: 4, batch:     8] loss: 3.192376<br>
[epoch: 5, batch:     0] loss: 3.186233<br>
[epoch: 5, batch:     1] loss: 3.186157<br>
[epoch: 5, batch:     2] loss: 3.186067<br>
[epoch: 5, batch:     3] loss: 3.184781<br>
[epoch: 5, batch:     4] loss: 3.183736<br>
[epoch: 5, batch:     5] loss: 3.182839<br>
[epoch: 5, batch:     6] loss: 3.181987<br>
[epoch: 5, batch:     7] loss: 3.181091<br>
[epoch: 5, batch:     8] loss: 3.179891<br>
[epoch: 6, batch:     0] loss: 3.169108<br>
[epoch: 6, batch:     1] loss: 3.168651<br>
[epoch: 6, batch:     2] loss: 3.167292<br>
[epoch: 6, batch:     3] loss: 3.166000<br>
[epoch: 6, batch:     4] loss: 3.164232<br>
[epoch: 6, batch:     5] loss: 3.162400<br>
[epoch: 6, batch:     6] loss: 3.160793<br>
[epoch: 6, batch:     7] loss: 3.158822<br>
[epoch: 6, batch:     8] loss: 3.157000<br>
[epoch: 7, batch:     0] loss: 3.131654<br>
[epoch: 7, batch:     1] loss: 3.130803<br>
[epoch: 7, batch:     2] loss: 3.128822<br>
[epoch: 7, batch:     3] loss: 3.124050<br>
[epoch: 7, batch:     4] loss: 3.120708<br>
[epoch: 7, batch:     5] loss: 3.115770<br>
[epoch: 7, batch:     6] loss: 3.110153<br>
[epoch: 7, batch:     7] loss: 3.104420<br>
[epoch: 7, batch:     8] loss: 3.099207<br>
[epoch: 8, batch:     0] loss: 3.012532<br>
[epoch: 8, batch:     1] loss: 3.004640<br>
[epoch: 8, batch:     2] loss: 2.989662<br>
[epoch: 8, batch:     3] loss: 2.979682<br>
[epoch: 8, batch:     4] loss: 2.961897<br>
[epoch: 8, batch:     5] loss: 2.942658<br>
[epoch: 8, batch:     6] loss: 2.922926<br>
[epoch: 8, batch:     7] loss: 2.898688<br>
[epoch: 8, batch:     8] loss: 2.875924<br>
[epoch: 9, batch:     0] loss: 2.632436<br>
[epoch: 9, batch:     1] loss: 2.636438<br>
[epoch: 9, batch:     2] loss: 2.621789<br>
[epoch: 9, batch:     3] loss: 2.601568<br>
[epoch: 9, batch:     4] loss: 2.600631<br>
[epoch: 9, batch:     5] loss: 2.586250<br>
[epoch: 9, batch:     6] loss: 2.581876<br>
[epoch: 9, batch:     7] loss: 2.572590<br>
[epoch: 9, batch:     8] loss: 2.566086</p>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects raw logits so remove the <code>nn.Softmax</code> from your model.</p>

k_ryna: <p>Thank you, that helped!</p>