Plot training and validation accuracy and losses

username: RAFAIL_MAHAMMADLI
cooked: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I got following error when i plot train and validation acc.  Could yu please help me to solve this error?</p>
<p>Thank you</p>
<p><span class="hashtag-raw">#save</span> the losses for further visualization</p>
<p>losses = {‘train’:<span class="chcklst-box fa fa-square-o fa-fw"></span>, ‘validation’:<span class="chcklst-box fa fa-square-o fa-fw"></span>}</p>
<p>accuracies = {‘train’:<span class="chcklst-box fa fa-square-o fa-fw"></span>, ‘validation’:<span class="chcklst-box fa fa-square-o fa-fw"></span>}</p>
<p>def train_model(model, criterion, optimizer, scheduler, num_epochs=25):<br>
since = time.time()</p>
<pre><code>best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0

for epoch in range(num_epochs):
    print(f'Epoch {epoch}/{num_epochs - 1}')
    print('-' * 10)

    # Each epoch has a training and validation phase
    for phase in ['train', 'validation']:
        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)
            labels = labels.to(device)

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward
            # track history if only in train
            with torch.set_grad_enabled(phase == 'train'):
                outputs = model(inputs)
                _, preds = torch.max(outputs, 1)
                loss = criterion(outputs, labels)

                # backward + optimize only if in training phase
                if phase == 'train':
                    loss.backward()
                    optimizer.step()

            # statistics
            running_loss += loss.item() * inputs.size(0)
            running_corrects += torch.sum(preds == labels.data)
        if phase == 'train':
            scheduler.step()

        epoch_loss = running_loss / dataset_sizes[phase]
        epoch_acc = running_corrects.double() / dataset_sizes[phase]
        losses[phase].append(epoch_loss)
        accuracies[phase].append(epoch_acc)

        print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')

        # deep copy the model
        if phase == 'validation' and epoch_acc &gt; best_acc:
            best_acc = epoch_acc
            best_model_wts = copy.deepcopy(model.state_dict())

    print()

time_elapsed = time.time() - since
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
print(f'Best val Acc: {best_acc:4f}')

# load best model weights
model.load_state_dict(best_model_wts)
return model
</code></pre>
<p>num_epochs=12<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/a/aa2bdd4d3b2aca257d32245839351a59f22a5257.png" data-download-href="https://discuss.pytorch.org/uploads/default/aa2bdd4d3b2aca257d32245839351a59f22a5257" title="Screenshot 2022-09-24 at 14.42.38"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/a/aa2bdd4d3b2aca257d32245839351a59f22a5257_2_690x455.png" alt="Screenshot 2022-09-24 at 14.42.38" data-base62-sha1="ohp9fk6oqeppDNUmp6ceWLXU4bd" width="690" height="455" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/a/aa2bdd4d3b2aca257d32245839351a59f22a5257_2_690x455.png, https://discuss.pytorch.org/uploads/default/original/3X/a/a/aa2bdd4d3b2aca257d32245839351a59f22a5257.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/a/aa2bdd4d3b2aca257d32245839351a59f22a5257.png 2x" data-dominant-color="717172"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2022-09-24 at 14.42.38</span><span class="informations">856×565 69.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))</p>
<p>t = f.suptitle(‘Performance’, fontsize=12)</p>
<p>f.subplots_adjust(top=0.85, wspace=0.3)</p>
<p>epoch_list = list(range(1,num_epochs*2+1))</p>
<p>ax1.plot(epoch_list, accuracies[‘train’], label=‘Train Accuracy’)</p>
<p>ax1.plot(epoch_list, accuracies[‘validation’], label=‘Validation Accuracy’)</p>
<p>ax1.set_xticks(np.arange(0, num_epochs*2+1, 5))</p>
<p>ax1.set_ylabel(‘Accuracy Value’)</p>
<p>ax1.set_xlabel(‘Epoch’)</p>
<p>ax1.set_title(‘Accuracy’)</p>
<p>l1 = ax1.legend(loc=“best”)</p>
<p>ax2.plot(epoch_list, losses[‘train’], label=‘Train Loss’)</p>
<p>ax2.plot(epoch_list, losses[‘validation’], label=‘Validation Loss’)</p>
<p>ax2.set_xticks(np.arange(0, epochs*2+1, 5))</p>
<p>ax2.set_ylabel(‘Loss Value’)</p>
<p>ax2.set_xlabel(‘Epoch’)</p>
<p>ax2.set_title(‘Loss’)</p>
<p>l2 = ax2.legend(loc=“best”)</p>

username: ptrblck
cooked: <p>Based on the error message it seems the inputs to <code>plot</code> don’t have the same shape.<br>
In particular it seems <code>accuracies['train']</code> should have <code>num_epochs</code> entries while <code>epoch_list</code> is created via: <code>epoch_list = list(range(1,num_epochs*2+1))</code>. Could you check the shape and see how long each array/list is?</p>