docstr: Computes the validation loss. Returns it and the number of batches.
docstr_tok: ["Computes","the","validation","loss",".","Returns","it","and","the","number","of","batches","."]
code: def _validation_loss(self):
      """
      Computes the validation loss. Returns it and the number of batches.
      """
      logger.info("Validating")
  
      self._model.eval()
  
      if self._validation_iterator is not None:
          val_iterator = self._validation_iterator
      else:
          val_iterator = self._iterator
  
      val_generator = val_iterator(
          self._validation_data,
          num_epochs=1,
          shuffle=False,
          cuda_device=self._iterator_device,
      )
      num_validation_batches = val_iterator.get_num_batches(
          self._validation_data
      )
      val_generator_tqdm = Tqdm.tqdm(
          val_generator, total=num_validation_batches
      )
      batches_this_epoch = 0
      val_loss = 0
      for batch in val_generator_tqdm:
          loss = self._batch_loss(batch, for_training=False)
          if loss is not None:
              # You shouldn't necessarily have to compute a loss for validation, so we allow for
              # `loss` to be None.  We need to be careful, though - `batches_this_epoch` is
              # currently only used as the divisor for the loss function, so we can safely only
              # count those batches for which we actually have a loss.  If this variable ever
              # gets used for something else, we might need to change things around a bit.
              batches_this_epoch += 1
              val_loss += loss.detach().cpu().numpy()
  
          # Update the description with the latest metrics
          val_metrics = self._get_metrics(val_loss, batches_this_epoch)
          description = self._description_from_metrics(val_metrics)
          val_generator_tqdm.set_description(description, refresh=False)
  
      return val_loss, batches_this_epoch
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import logging
  import os
  import shutil
  import time
  import re
  import datetime
  import traceback
  import torch
  import torch.optim.lr_scheduler
  from torch.nn.parallel import replicate, parallel_apply
  from torch.nn.parallel.scatter_gather import scatter_kwargs, gather
  from allennlp.common import Params
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.util import peak_memory_mb, gpu_memory_mb
  from allennlp.common.tqdm import Tqdm
  from allennlp.data.instance import Instance
  from allennlp.data.iterators.data_iterator import DataIterator
  from allennlp.models.model import Model
  from allennlp.nn import util
  from allennlp.training.learning_rate_schedulers import LearningRateScheduler
  from allennlp.training.optimizers import Optimizer
label_desc: Use image node_id 14 for calling the Trainer obj's underlying member method code with example usage: obj._validation_loss() and returns: val_loss,  batches_this_epoch