How to plot train and validation accuracy graph?

Abebe_Zerihun: <p>Easy way to plot train and val accuracy<br>
train loss and val loss graph.</p>

RaLo4: <p>One simple way to plot your losses after the training would be using matplotlib:</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt

val_losses = []
train_losses = []
training loop
train_losses.append(loss_train.item())

testing
val_losses.append(loss_val.item())

plt.figure(figsize=(10,5))
plt.title("Training and Validation Loss")
plt.plot(val_losses,label="val")
plt.plot(train_losses,label="train")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
plt.show()
</code></pre>
<p>The more elegant way, which lets you check your progress during training as well is tensorboard:</p>
<pre><code class="lang-auto">from torch.utils.tensorboard import SummaryWriter

# Writer will output to ./runs/ directory by default
writer = SummaryWriter(logdir)

training loop
train_loss += loss_train.item()
writer.add_scalar('Loss/train', training_loss, global_step)

testing
val_loss += loss_val.item()
writer.add_scalar('Loss/val', val_loss, global_step)

writer.close()

</code></pre>
<p>Accuracy works the same</p>

Abebe_Zerihun: <p>thank u for your reply</p>

Mercy: <p>train_loss.append(train_loss)</p>
<pre><code>valid_loss.append(test_loss)

train_acc.append(train_acc)

valid_acc.append(test_acc)
</code></pre>
<p>train_loss = np.array(train_loss)</p>
<p>valid_loss = np.array(valid_loss)</p>
<p>train_acc = np.array(train_acc)</p>
<p>valid_acc = np.array(valid_acc)</p>
<p>data =np.array([train_loss, train_acc,valid_acc,valid_loss])</p>
<p>np.savetxt(‘path’, data, fmt=’%.9f’)//if u wanna plot all</p>
<p><span class="hashtag">#plot</span>…</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_acc)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_acc)</p>
<p>plt.title(“Accuray”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([“Train Acc”,“Test Acc”],loc = “lower right”)</p>
<p>plt.savefig("./CNN_accuracy.png",dpi = 600)</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_loss)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_loss)</p>
<p>plt.title(“Losss”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([‘train loss’, ‘valid loss’], loc=“upper right”)</p>
<p>plt.savefig("./loss.png",dpi = 600)`*[quote=“Mercy, post:4, topic:105524, full:true”]<br>
train_loss.append(train_loss)</p>
<pre><code>valid_loss.append(test_loss)

train_acc.append(train_acc)

valid_acc.append(test_acc)
</code></pre>
<p>train_loss = np.array(train_loss)</p>
<p>valid_loss = np.array(valid_loss)</p>
<p>train_acc = np.array(train_acc)</p>
<p>valid_acc = np.array(valid_acc)</p>
<p>data =np.array([train_loss, train_acc,valid_acc,valid_loss])</p>
<p>np.savetxt(‘path’, data, fmt=’%.9f’)//if u wanna plot all</p>
<p><span class="hashtag">#plot</span>…</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_acc)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_acc)</p>
<p>plt.title(“Accuray”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([“Train Acc”,“Test Acc”],loc = “lower right”)</p>
<p>plt.savefig("./CNN_accuracy.png",dpi = 600)</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_loss)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_loss)</p>
<p>plt.title(“Losss”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([‘train loss’, ‘valid loss’], loc=“upper right”)</p>
<p>plt.savefig("./loss.png",dpi = 600)<br>
[/quote]</p>
<p>[quote=“Mercy, post:4, topic:105524, full:true”]<br>
train_loss.append(train_loss)</p>
<pre><code>valid_loss.append(test_loss)

train_acc.append(train_acc)

valid_acc.append(test_acc)
</code></pre>
<p>train_loss = np.array(train_loss)</p>
<p>valid_loss = np.array(valid_loss)</p>
<p>train_acc = np.array(train_acc)</p>
<p>valid_acc = np.array(valid_acc)</p>
<p>data =np.array([train_loss, train_acc,valid_acc,valid_loss])</p>
<p>np.savetxt(‘path’, data, fmt=’%.9f’)//if u wanna plot all</p>
<p><span class="hashtag">#plot</span>…</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_acc)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_acc)</p>
<p>plt.title(“Accuray”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([“Train Acc”,“Test Acc”],loc = “lower right”)</p>
<p>plt.savefig("./CNN_accuracy.png",dpi = 600)</p>
<p>fig = plt.figure()</p>
<p>plt.subplot(111)</p>
<p>plt.plot(np.arange(1,EPOCH+1),train_loss)</p>
<p>plt.plot(np.arange(1,EPOCH+1),valid_loss)</p>
<p>plt.title(“Losss”)</p>
<p>plt.xlabel(“Epochs”)</p>
<p>plt.ylabel(“Acc”)</p>
<p>plt.legend([‘train loss’, ‘valid loss’], loc=“upper right”)</p>
<p>plt.savefig("./loss.png",dpi = 600)</p>

Lane: <p>What is loss_train ?</p>

RaLo4: <p>Are you asking why the name (1) or what loss is (2)?</p>
<ul>
<li>(1) Your normal loss during training as opposed to your loss during validation.</li>
<li>(2) Neural Networks use a loss function as an objective function. The goal during training of a Neural Network is the minimization of the loss functions output, called loss.</li>
</ul>

Lane: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/6/36a59d55ecabe0c482bb8f7ae945e53f5d303020.png" alt="image" data-base62-sha1="7NqBSO0WKesdStLGRlZs1uqkda0" width="496" height="392"></p>
<p>From where is this variable?</p>

RaLo4: <p>That’s the current output from your loss function.<br>
The original question was how loss and accuracy can be plotted on a graph.<br>
So the answer just shows losses being added up and plotted.<br>
The above code excludes your training loop, it would go where it says <code>training loop</code>.<br>
Let me add an example training loop. Maybe that clears up the confusion.</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt

val_losses = []
train_losses = []
for epoch in range(epochs):
        for i, data in enumerate(trainloader, 0):
                inputs, labels = data
                optimizer.zero_grad()
                outputs = net(inputs)
                loss = loss_function(outputs, labels)
                loss.backward()
                optimizer.step()
                train_losses.append(loss_train.item())

#testing loop goes here
val_losses.append(loss_val.item())

plt.figure(figsize=(10,5))
plt.title("Training and Validation Loss")
plt.plot(val_losses,label="val")
plt.plot(train_losses,label="train")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
plt.show()
</code></pre>

Lane: <p>Okay, thank you, understand.</p>