Well, I am sorry I deleted that code…
But I tried to change the data loading method to the tutorial style and use it on my training code. Unsurprisingly, the NN still cannot learn…
Well I suppose my training code must have some problems…
But I cannot find it…
Could you please help me to check the code?
def train_model(model, learning_rate, nsamples, load_train_dataset):
model.train(True)
epoch = 0
print_loss = 0
criterion = nn.CrossEntropyLoss()
optimizer_module = optim.SGD(model.parameters(), lr=learning_rate)
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)
model = model.to(device)
for i, (data, target) in enumerate(load_train_dataset):
data = data.to(device)
target = target.to(device)
if i > nsamples:
break
else:
# train network
optimizer_module.zero_grad()
out = model(data)
loss = criterion(out, target)
print_loss = loss.data.item()
        loss.backward()
        optimizer_module.step()
        epoch += 1
        if epoch % 100 == 0:
            print('\n Train epoch: {}, loss: {:.4}'.format(epoch, loss.data.item()))
return print_loss

And the training result is this:
Train epoch: 100, loss: 2.277
Train epoch: 200, loss: 2.28
Train epoch: 300, loss: 2.306
Train epoch: 400, loss: 2.331
Train epoch: 500, loss: 2.263
Train epoch: 600, loss: 2.284
Train epoch: 700, loss: 2.255