Hello,
I’m trying to figure out the best way to pull accuracy out for future plotting. Right now, I get a tensor of 10 predictions. How do i pull each one out of the tensor to store in a list to calculate accuracy for later plotting? Code below:
train_losses, test_losses = [], []
train_accuracy, test_accuracy = [], []
def train_model(model, criterion, optimizer, num_epochs=5, test_epochs = 5):
liveloss = PlotLosses(series_fmt={'training': '{}', 'test':'test_{}'})
max_accuracy = 0
running_loss = 0.0
running_corrects = 0
for epoch in range(num_epochs):
for b, (image, label, policy, categorical_data, numerical_data) in enumerate(train_loader):
image = image.cuda()
label = label.cuda()
numerical_data = numerical_data.cuda()
categorical_data = categorical_data.cuda()
outputs = model(image, categorical_data, numerical_data)
loss = criterion(outputs, label)
print(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
_, preds = torch.max(outputs, 1)
print(preds)
running_loss += loss.item()
running_corrects += torch.sum(preds == label.data)
b += 1
if b % print_interval == 0:
print(f"train_epoch: {epoch},train_batch: {b}")
if b == max_trn_batch:
train_losses.append(running_loss / max_trn_batch)
train_accuracy.append(running_corrects/ max_trn_batch)
print(f"epoch: {epoch}, loss: {train_losses}, accuracy: {train_accuracy}")
break