class_docstr: Model for predicting autofills given context.
class_docstr_tok: ["Model","for","predicting","autofills","given","context","."]
docstr: Computes scaled loss based on mask out size.
docstr_tok: ["Computes","scaled","loss","based","on","mask","out","size","."]
code: def compute_loss(self, unreduced_loss):
      """Computes scaled loss based on mask out size."""
      # construct mask to identify zero padding that was introduced to
      # make the batch rectangular
      batch_duration = tf.shape(self.pianorolls)[1]
      indices = tf.to_float(tf.range(batch_duration))
      pad_mask = tf.to_float(
          indices[None, :, None, None]
          < self.lengths[:, None, None, None]
      )
  
      # construct mask and its complement, respecting pad mask
      mask = pad_mask * self.masks
      unmask = pad_mask * (1.0 - self.masks)
  
      # Compute numbers of variables
      # #timesteps * #variables per timestep
      variable_axis = 3 if self.hparams.use_softmax_loss else 2
      dd = self.lengths[:, None, None, None] * tf.to_float(
          tf.shape(self.pianorolls)[variable_axis]
      )
      reduced_dd = tf.reduce_sum(dd)
  
      # Compute numbers of variables to be predicted/conditioned on
      mask_size = tf.reduce_sum(
          mask, axis=[1, variable_axis], keep_dims=True
      )
      unmask_size = tf.reduce_sum(
          unmask, axis=[1, variable_axis], keep_dims=True
      )
  
      unreduced_loss *= pad_mask
      if self.hparams.rescale_loss:
          unreduced_loss *= dd / mask_size
  
      # Compute average loss over entire set of variables
      self.loss_total = tf.reduce_sum(unreduced_loss) / reduced_dd
  
      # Compute separate losses for masked/unmasked variables
      # NOTE: indexing the pitch dimension with 0 because the mask is constant
      # across pitch. Except in the sigmoid case, but then the pitch dimension
      # will have been reduced over.
      self.reduced_mask_size = tf.reduce_sum(mask_size[:, :, 0, :])
      self.reduced_unmask_size = tf.reduce_sum(unmask_size[:, :, 0, :])
  
      assert_partition_op = tf.group(
          tf.assert_equal(tf.reduce_sum(mask * unmask), 0.0),
          tf.assert_equal(
              self.reduced_mask_size + self.reduced_unmask_size,
              reduced_dd,
          ),
      )
      with tf.control_dependencies([assert_partition_op]):
          self.loss_mask = (
              tf.reduce_sum(mask * unreduced_loss)
              / self.reduced_mask_size
          )
          self.loss_unmask = (
              tf.reduce_sum(unmask * unreduced_loss)
              / self.reduced_unmask_size
          )
  
      # Check which loss to use as objective function.
      self.loss = (
          self.loss_mask
          if self.hparams.optimize_mask_only
          else self.loss_total
      )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import os
  from magenta.models.coconet import lib_hparams
  from magenta.models.coconet import lib_tfutil
  import tensorflow.compat.v1
label_desc: Use image node_id 15 for calling the CoconetGraph obj's underlying member method code with example usage: obj.compute_loss(unreduced_loss) without return types