class_docstr: Mixin utility for training loss methods for models that do **not** own their own data.
  It provides
  
    - a uniform API for the training loss :meth:`training_loss`
    - a convenience method :meth:`training_loss_closure` for constructing the closure expected by
      various optimizers, namely :class:`gpflow.optimizers.Scipy` and subclasses of
      `tf.optimizers.Optimizer`.
  
  See :class:`InternalDataTrainingLossMixin` for an equivalent mixin for models that **do** own
  their own data.
class_docstr_tok: ["Mixin","utility","for","training","loss","methods","for","models","that","do","*","*","not","*","*","own","their","own","data",".","It","provides","-","a","uniform","API","for","the","training","loss",":","meth",":","`","training_loss","`","-","a","convenience","method",":","meth",":","`","training_loss_closure","`","for","constructing","the","closure","expected","by","various","optimizers",",","namely",":","class",":","`","gpflow.optimizers.Scipy","`","and","subclasses","of","`","tf.optimizers.Optimizer","`",".","See",":","class",":","`","InternalDataTrainingLossMixin","`","for","an","equivalent","mixin","for","models","that","*","*","do","*","*","own","their","own","data","."]
docstr: Returns a closure that computes the training loss, which by default is
  wrapped in tf.function(). This can be disabled by passing `compile=False`.
  
  :param data: the data to be used by the closure for computing the model
      objective. Can be the full dataset or an iterator, e.g.
      `iter(dataset.batch(batch_size))`, where dataset is an instance of
      tf.data.Dataset.
  :param compile: if True, wrap training loss in tf.function()
docstr_tok: ["Returns","a","closure","that","computes","the","training","loss",",","which","by","default","is","wrapped","in","tf.function","(",")",".","This","can","be","disabled","by","passing","`","compile=False","`",".",":","param","data",":","the","data","to","be","used","by","the","closure","for","computing","the","model","objective",".","Can","be","the","full","dataset","or","an","iterator",",","e.g",".","`","iter","(","dataset.batch","(","batch_size",")",")","`",",","where","dataset","is","an","instance","of","tf.data.Dataset",".",":","param","compile",":","if","True",",","wrap","training","loss","in","tf.function","(",")"]
code: def training_loss_closure(
      self,
      data: Union[Data, DatasetOwnedIterator],
      *,
      compile: bool = True,
  ) -> Callable[[], tf.Tensor]:
      """
      Returns a closure that computes the training loss, which by default is
      wrapped in tf.function(). This can be disabled by passing `compile=False`.
  
      :param data: the data to be used by the closure for computing the model
          objective. Can be the full dataset or an iterator, e.g.
          `iter(dataset.batch(batch_size))`, where dataset is an instance of
          tf.data.Dataset.
      :param compile: if True, wrap training loss in tf.function()
      """
      training_loss = self.training_loss
  
      if isinstance(data, DatasetOwnedIterator):
          if compile:
              # lambda because: https://github.com/GPflow/GPflow/issues/1929
              training_loss_lambda = lambda d: self.training_loss(d)
              input_signature = [data.element_spec]
              training_loss = tf.function(
                  training_loss_lambda, input_signature=input_signature
              )
  
          def closure() -> tf.Tensor:
              assert isinstance(
                  data, DatasetOwnedIterator
              )  # Hint for mypy.
              batch = next(data)
              return training_loss(batch)
  
      else:
  
          def closure() -> tf.Tensor:
              return training_loss(data)
  
          if compile:
              closure = tf.function(closure)
  
      return closure
  
file_path: GPflow/gpflow/models/training_mixins.py
imports: from typing import Callable, TypeVar, Union
  import tensorflow
  from check_shapes import check_shapes
  from tensorflow.python.data.ops.iterator_ops import OwnedIterator
  from ..base import InputData, OutputData, RegressionData
label_desc: Use image node_id 2 for calling the ExternalDataTrainingLossMixin obj's underlying member method code with example usage: obj.training_loss_closure(data) and returns: closure, training_loss, training_loss