code: def model_with_buckets(
      encoder_inputs_tensor,
      decoder_inputs,
      targets,
      weights,
      buckets,
      seq2seq,
      softmax_loss_function=None,
      per_example_loss=False,
      name=None,
  ):
      """Create a sequence-to-sequence model with support for bucketing.
  
      The seq2seq argument is a function that defines a sequence-to-sequence model,
      e.g., seq2seq = lambda x, y: basic_rnn_seq2seq(x, y, rnn_cell.GRUCell(24))
  
      Args:
          encoder_inputs: A list of Tensors to feed the encoder; first seq2seq input.
          decoder_inputs: A list of Tensors to feed the decoder; second seq2seq input.
          targets: A list of 1D batch-sized int32 Tensors (desired output sequence).
          weights: List of 1D batch-sized float-Tensors to weight the targets.
          buckets: A list of pairs of (input size, output size) for each bucket.
          seq2seq: A sequence-to-sequence model function; it takes 2 input that
              agree with encoder_inputs and decoder_inputs, and returns a pair
              consisting of outputs and states (as, e.g., basic_rnn_seq2seq).
          softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch
              to be used instead of the standard softmax (the default if this is None).
          per_example_loss: Boolean. If set, the returned loss will be a batch-sized
              tensor of losses for each sequence in the batch. If unset, it will be
              a scalar with the averaged loss from all examples.
          name: Optional name for this operation, defaults to "model_with_buckets".
  
      Returns:
          A tuple of the form (outputs, losses), where:
              outputs: The outputs for each bucket. Its j'th element consists of a list
                  of 2D Tensors of shape [batch_size x num_decoder_symbols] (jth outputs).
              losses: List of scalar Tensors, representing losses for each bucket, or,
                  if per_example_loss is set, a list of 1D batch-sized float Tensors.
  
      Raises:
          ValueError: If length of encoder_inputsut, targets, or weights is smaller
              than the largest (last) bucket.
      """
      if len(targets) < buckets[-1][1]:
          raise ValueError(
              "Length of targets (%d) must be at least that of last"
              "bucket (%d)." % (len(targets), buckets[-1][1])
          )
      if len(weights) < buckets[-1][1]:
          raise ValueError(
              "Length of weights (%d) must be at least that of last"
              "bucket (%d)." % (len(weights), buckets[-1][1])
          )
  
      all_inputs = (
          [encoder_inputs_tensor] + decoder_inputs + targets + weights
      )
      with tf.compat.v1.name_scope(
          name, "model_with_buckets", all_inputs
      ):
          with tf.compat.v1.variable_scope(
              tf.compat.v1.get_variable_scope(), reuse=None
          ):
              bucket = buckets[0]
              encoder_inputs = tf.split(
                  encoder_inputs_tensor, bucket[0], 0
              )
              encoder_inputs = [
                  tf.squeeze(inp, axis=[0]) for inp in encoder_inputs
              ]
              bucket_outputs, attention_weights_history = seq2seq(
                  encoder_inputs[: int(bucket[0])],
                  decoder_inputs[: int(bucket[1])],
                  int(bucket[0]),
              )
              if per_example_loss:
                  loss = sequence_loss_by_example(
                      bucket_outputs,
                      targets[: int(bucket[1])],
                      weights[: int(bucket[1])],
                      average_across_timesteps=True,
                      softmax_loss_function=softmax_loss_function,
                  )
              else:
                  loss = sequence_loss(
                      bucket_outputs,
                      targets[: int(bucket[1])],
                      weights[: int(bucket[1])],
                      average_across_timesteps=True,
                      softmax_loss_function=softmax_loss_function,
                  )
  
      return bucket_outputs, loss, attention_weights_history
  
file_path: attention-ocr/aocr/model/seq2seq.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from six.moves import xrange
  from six.moves import zip
  import tensorflow
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import init_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import nn_ops
  from tensorflow.python.ops import variable_scope
  from tensorflow.python.util import nest
label_desc: Use image node_id 7 for calling a global function with example usage: model_with_buckets(encoder_inputs_tensor, decoder_inputs, targets, weights, buckets, seq2seq, softmax_loss_function, per_example_loss, name) and returns: bucket_outputs,  loss,  attention_weights_history