So my label looks like this
_,labels = next(iter(dataloaders['val']))
labels

image803×163 2.64 KB
How I should change the keywords in the training loop to get accuracy ?
for epoch in range(EPOCHS):
        print('Epoch ', epoch,'/',EPOCHS-1)
        print('-'*15)

        for phase in ['train', 'val']:
            if phase == 'train':
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0.0

            # Iterate over data.
            for inputs,labels in dataloaders[phase]:
                inputs = inputs.to(device, dtype=torch.float)
                labels = labels.to(device, dtype=torch.float)

                # zero the parameter gradients
                optimizer.zero_grad()

                with torch.set_grad_enabled(phase == 'train'):

                    outputs = model(inputs)
                    _, preds = torch.max(outputs, 1)

                    loss = loss_fn(outputs,labels)

                    # we backpropagate to set our learning parameters only in training mode
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                running_loss += loss.item() * inputs.size(0)
                
                if phase == 'val':
                    running_corrects += torch.sum(preds == torch.argmax(labels.data, dim=1))

            # scheduler for weight decay
            if phase == 'train':
                scheduler.step()
                epoch_loss = running_loss / float(dataset_sizes[phase])
                xm.master_print('{} Loss: {:.4f}'.format(phase, epoch_loss))
            else:
                epoch_loss = running_loss / float(dataset_sizes[phase])
                epoch_acc = running_corrects / float(dataset_sizes[phase])
                xm.master_print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

link to my notebook