To visualize the training and test loss inside the same figure, you could add the loss calculation to the mentioned code snippet and store the training and test losses in separate lists.
Note that you should detach the (training) loss, if you are storing its value in a list to avoid an increasing memory usage (which might create an OOM) via:
train_losses.append(loss.detach().cpu().item())

Once you have both lists, you can convert them to numpy arrays and plot via matplotlib.