How to obtain validation loss during training?

Karolina_Syryllo: <p>Hi, I am trying to obtain validation loss alongside training loss, but it only prints training loss and ignores validation loss. It doesn’t show any error when I call the function but it doesn’t print validation loss</p>
<p>Here is the code,</p>
<p>def train_model_epochs2(num_epochs):<br>
for epoch in range(num_epochs):</p>
<pre><code>    running_loss = 0.0
    for i, data in enumerate(train_loader, 0):
        images, labels = data

        # Explicitly specifies that data is to be copied onto the device!
        images = images.to(device)  # 
        labels = labels.to(device)  #

        optimizer.zero_grad()
        outputs = model_gpu(images)

        loss = criterion(outputs, labels)
        loss.backward()

        optimizer.step()
        running_loss += loss.item()
        if i % 100 == 99:    # print every 1000 mini-batches
            print('Epoch / Batch [%d / %d] - Loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 100))
            running_loss = 0.0


    val_loss = 0
    for b, vdata in enumerate(valid_loader, 0):
      with torch.no_grad():

        images, labels = vdata
        images = images.to(device)  # 
        labels = labels.to(device)  # 

        outputs = model_gpu(images)

        # Compute the loss based on the true labels
        loss = criterion(outputs, labels)

        #Print out loss
        val_loss += loss.item()
        if b % 100 == 99:    # print every 100 mini-batches
            print('Epoch / Batch [%d / %d] - Val_Loss: %.3f' %
                  (epoch + 1, b + 1, val_loss / 100))
            val_loss=0.0</code></pre>

jmandivarapu1: <aside class="quote no-group" data-username="Karolina_Syryllo" data-post="1" data-topic="71009">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/karolina_syryllo/48/20959_2.png" class="avatar"> Karolina_Syryllo:</div>
<blockquote>
<p>valid_loader</p>
</blockquote>
</aside>
<p>Can you check the length of the valid_loader? and the same time remove the condition <code>if b % 100 == 99: </code> for testing purpose to see what happens</p>

Karolina_Syryllo: <p>Length of a valid_loader is 75.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/1/21ca59c8e62d8276c5c5e5992957f2236487a0fb.png" data-download-href="https://discuss.pytorch.org/uploads/default/21ca59c8e62d8276c5c5e5992957f2236487a0fb" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/1/21ca59c8e62d8276c5c5e5992957f2236487a0fb_2_690x246.png" alt="image" data-base62-sha1="4OVi2oJUqBTXmEW8jLbtcfGELWP" width="690" height="246" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/1/21ca59c8e62d8276c5c5e5992957f2236487a0fb_2_690x246.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/1/21ca59c8e62d8276c5c5e5992957f2236487a0fb_2_1035x369.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/1/21ca59c8e62d8276c5c5e5992957f2236487a0fb_2_1380x492.png 2x" data-dominant-color="F5F5F5"><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">image</span><span class="informations">1809×645 43 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>When I also removed val_loss=0.0 then I still didn’t get any output for validation loss</p>

jmandivarapu1: <aside class="quote no-group" data-username="Karolina_Syryllo" data-post="1" data-topic="71009">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/karolina_syryllo/48/20959_2.png" class="avatar"> Karolina_Syryllo:</div>
<blockquote>
<p>for b, vdata in enumerate(valid_loader, 0):</p>
</blockquote>
</aside>
<p>Can you print something any constant just after entering the <code>for b, vdata in enumerate(valid_loader, 0):</code> and please change it to ```for b, vdata in enumerate(valid_loader):``</p>
<p>and print either images,labels and outputs</p>