code: def prefix_accuracy(
      predictions, labels, weights_fn=common_layers.weights_nonzero
  ):
      """Average # of correct tokens at start of sequences, ignoring padding 0s.
  
      See section 4.3 of Learning to Transduce with Unbounded Memory,
      Grefenstette et al., 2015.
  
      Args:
        predictions: Tensor of shape [`batch_size`, `length`, 1, `num_classes`] and
            type tf.float32 representing the logits, 0-padded.
        labels: Tensor of shape [`batch_size`, `length`, 1, 1] and type tf.int32
            representing the labels of same length as logits and 0-padded.
        weights_fn: ignored. The weights returned are the total length of the ground
            truth labels, excluding 0-paddings.
  
      Returns:
        (prefix accuracy, 1.0)
  
      Raises:
        ValueError: if weights_fn is not common_layers.weights_nonzero.
      """
      if weights_fn is not common_layers.weights_nonzero:
          raise ValueError(
              "Only weights_nonzero can be used for this metric."
          )
  
      predictions = tf.to_int32(
          tf.squeeze(tf.argmax(predictions, axis=-1), axis=2)
      )
      labels = tf.squeeze(labels, axis=(2, 3))
      seq_len = tf.reduce_sum(
          tf.cast(
              tf.not_equal(labels, tf.constant(0)), dtype=tf.float32
          ),
          axis=1,
      )
      matching_elements = tf.equal(labels, predictions)
      prefix_len = tf.reduce_sum(
          tf.cumprod(tf.cast(matching_elements, tf.float32), axis=1),
          axis=1,
      )
      return tf.reduce_mean(prefix_len / seq_len), tf.constant(1.0)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import six
  from tensor2tensor.layers import common_layers
  from tensor2tensor.layers import modalities
  from tensor2tensor.utils import bleu_hook
  from tensor2tensor.utils import contrib
  from tensor2tensor.utils import rouge
  from tensor2tensor.utils import sari_hook
  import tensorflow.compat.v1
  from tensorflow.python.util import tf_inspect
label_desc: Use image node_id 13 for calling a global function with example usage: prefix_accuracy(predictions, labels, weights_fn) and returns: tf,  tf