docstr: Does a forward pass on the given batch and returns the ``loss`` value in the result.
  If ``for_training`` is `True` also applies regularization penalty.
docstr_tok: ["Does","a","forward","pass","on","the","given","batch","and","returns","the","``","loss","``","value","in","the","result",".","If","``","for_training","``","is","`","True","`","also","applies","regularization","penalty","."]
code: def _batch_loss(self, batch, for_training):
      """
      Does a forward pass on the given batch and returns the ``loss`` value in the result.
      If ``for_training`` is `True` also applies regularization penalty.
      """
      if self._multiple_gpu:
          output_dict = self._data_parallel(batch)
      else:
          output_dict = self._model(**batch)
  
      try:
          loss = output_dict["loss"]
          if for_training:
              loss += self._model.get_regularization_penalty()
      except KeyError:
          if for_training:
              raise RuntimeError(
                  "The model you are trying to optimize does not contain a"
                  " 'loss' key in the output of model.forward(inputs)."
              )
          loss = None
  
      return loss
  
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 6 for calling the Trainer obj's underlying member method code with example usage: obj._batch_loss(batch, for_training) and returns: loss