Johann Verlohren
Johann Verlohren

Reputation: 35

Pytorch getting accuracy of train_loop doesn't work

I want to get the accuracy of my train section of my neuronal network But i get this error: correct += (prediction.argmax(1) == y).type(torch.float).item() ValueError: only one element tensors can be converted to Python scalars With this code :

def train_loop(dataloader, model, optimizer):
    model.train()
    size = len(dataloader.dataset)
    correct = 0, 0
    l_loss = 0
    for batch, (X, y) in enumerate(dataloader):
        prediction = model(X)
        loss = cross_entropy(prediction, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        correct += (prediction.argmax(1) == y).type(torch.float).sum().item()
        loss, current = loss.item(), batch * len(X)
        l_loss = loss
        print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")
    correct /= size
    accu = 100 * correct

    train_loss.append(l_loss)
    train_accu.append(accu)
    print(f"Accuracy: {accu:>0.1f}%")

I don't understand why it is not working becaus in my test section it work perfektly fine with execly the same code line.

Upvotes: 1

Views: 315

Answers (1)

Hamzah Al-Qadasi
Hamzah Al-Qadasi

Reputation: 9786

item function is used to convert a one-element tensor to a standard python number as stated in the here. Please try to make sure that the result of the sum() is only a one-element tensor before using item().

x = torch.tensor([1.0,2.0]) # a tensor contains 2 elements
x.item()

error message: ValueError: only one element tensors can be converted to Python scalars

Try to use this:

prediction = prediction.argmax(1)
correct = prediction.eq(y)
correct = correct.sum() 
print(correct) # to check if it is a one value tensor
correct_sum += correct.item() 

Upvotes: 1

Related Questions