class_docstr: Transformer decoder that applies multi-head self-attention for
  sequence decoding.
  
  It is a stack of :class:`~texar.tf.modules.encoders.MultiheadAttentionEncoder`,
  :class:`~texar.tf.modules.FeedForwardNetwork` and residual connections.
  
  Args:
      vocab_size (int, optional): Vocabulary size. Required if
          :attr:`output_layer` is `None`.
      output_layer (optional): An output layer that transforms cell output
          to logits. This can be:
  
          - A callable layer, e.g., an instance             of :tf_main:`tf.layers.Layer <layers/Layer>`.
          - A tensor. A dense layer will be created using the tensor             as the kernel weights. The bias of the dense layer is determined by            `hparams.output_layer_bias`. This can be used to tie the output             layer with the input embedding matrix, as proposed in             https://arxiv.org/pdf/1608.05859.pdf
          - `None`. A dense layer will be created based on attr:`vocab_size`            and `hparams.output_layer_bias`.
          - If no output layer in the end is needed, set             `(vocab_size=None, output_layer=tf.identity)`.
  
  .. document private functions
  .. automethod:: _build
class_docstr_tok: ["Transformer","decoder","that","applies","multi-head","self-attention","for","sequence","decoding",".","It","is","a","stack","of",":","class",":","`","~texar.tf.modules.encoders.MultiheadAttentionEncoder","`",",",":","class",":","`","~texar.tf.modules.FeedForwardNetwork","`","and","residual","connections",".","Args",":","vocab_size","(","int",",","optional",")",":","Vocabulary","size",".","Required","if",":","attr",":","`","output_layer","`","is","`","None","`",".","output_layer","(","optional",")",":","An","output","layer","that","transforms","cell","output","to","logits",".","This","can","be",":","-","A","callable","layer",",","e.g.",",","an","instance","of",":","tf_main",":","`","tf.layers.Layer","<","layers\/Layer",">","`",".","-","A","tensor",".","A","dense","layer","will","be","created","using","the","tensor","as","the","kernel","weights",".","The","bias","of","the","dense","layer","is","determined","by","`","hparams.output_layer_bias","`",".","This","can","be","used","to","tie","the","output","layer","with","the","input","embedding","matrix",",","as","proposed","in","https",":","\/\/arxiv.org\/pdf\/1608.05859.pdf","-","`","None","`",".","A","dense","layer","will","be","created","based","on","attr",":","`","vocab_size","`","and","`","hparams.output_layer_bias","`",".","-","If","no","output","layer","in","the","end","is","needed",",","set","`","(","vocab_size=None",",","output_layer=tf.identity",")","`",".","..","document","private","functions","..","automethod",":",":","_build"]
docstr: Returns an initialized cache.
  
  In order to support both inference-like decoding and beam-search
  decoding, the elements of each layer must be initialized and extended
  as different structure respectively. Specifically, when inference-like
  decoding, tf.TensorArray is used, which satisfies the shape consistency
  check in the while-loop in tf.contrib.seq2seq.dynamic_decode. When
  beam-search decoding, a tf.Tensor of shape
  `[batch_size, current_steps, num_units]` is maintained, where
  `current_steps` is the number of steps currently decoded.
docstr_tok: ["Returns","an","initialized","cache",".","In","order","to","support","both","inference-like","decoding","and","beam-search","decoding",",","the","elements","of","each","layer","must","be","initialized","and","extended","as","different","structure","respectively",".","Specifically",",","when","inference-like","decoding",",","tf.TensorArray","is","used",",","which","satisfies","the","shape","consistency","check","in","the","while-loop","in","tf.contrib.seq2seq.dynamic_decode",".","When","beam-search","decoding",",","a","tf.Tensor","of","shape","`","[","batch_size",",","current_steps",",","num_units","]","`","is","maintained",",","where","`","current_steps","`","is","the","number","of","steps","currently","decoded","."]
code: def _init_cache(
      self,
      memory,
      memory_attention_bias,
      beam_search_decoding,
      batch_size=None,
  ):
      """Returns an initialized cache.
  
      In order to support both inference-like decoding and beam-search
      decoding, the elements of each layer must be initialized and extended
      as different structure respectively. Specifically, when inference-like
      decoding, tf.TensorArray is used, which satisfies the shape consistency
      check in the while-loop in tf.contrib.seq2seq.dynamic_decode. When
      beam-search decoding, a tf.Tensor of shape
      `[batch_size, current_steps, num_units]` is maintained, where
      `current_steps` is the number of steps currently decoded.
      """
      if batch_size is None:
          batch_size = self.batch_size
  
      def _shape(batch_size, from_shape):
          if (
              not isinstance(from_shape, tf.TensorShape)
              or from_shape.ndims == 0
          ):
              return tf.TensorShape(None)
  
          batch_size = tf.contrib.util.constant_value(
              tf.convert_to_tensor(batch_size, name="batch_size")
          )
          return tf.TensorShape([batch_size]).concatenate(from_shape)
  
      def _create_ta(s, d):
          return tf.TensorArray(
              dtype=d,
              size=0,
              dynamic_size=True,
              clear_after_read=False,
              element_shape=_shape(batch_size, s),
          )
  
      def _create_empty_tensor(s, d):
          return tf.zeros([batch_size, 0] + s.as_list(), dtype=d)
  
      _create_fn = (
          _create_empty_tensor if beam_search_decoding else _create_ta
      )
  
      s = tf.TensorShape([self._hparams.multihead_attention.num_units])
  
      if memory is not None:
          cache = {
              "memory": memory,
              "memory_attention_bias": memory_attention_bias,
          }
          for l in range(self._hparams.num_blocks):
              cache["layer_{}".format(l)] = {
                  "self_keys": _create_fn(s, tf.float32),
                  "self_values": _create_fn(s, tf.float32),
                  "memory_keys": _create_fn(s, tf.float32),
                  "memory_values": _create_fn(s, tf.float32),
              }
      else:
          cache = {}
          for l in range(self._hparams.num_blocks):
              cache["layer_{}".format(l)] = {
                  "self_keys": _create_fn(s, tf.float32),
                  "self_values": _create_fn(s, tf.float32),
              }
  
      return cache
  
imports: import collections
  import tensorflow
  from tensorflow.contrib.seq2seq import Decoder
  from texar.tf.core import layers
  from texar.tf.module_base import ModuleBase
  from texar.tf.modules.networks.networks import FeedForwardNetwork
  from texar.tf.modules.encoders.transformer_encoders import default_transformer_poswise_net_hparams
  from texar.tf.modules.encoders.multihead_attention import MultiheadAttentionEncoder
  from texar.tf.modules.decoders.rnn_decoder_base import _make_output_layer
  from texar.tf.modules.decoders import tf_helpers
  from texar.tf.utils import beam_search, transformer_attentions
  from texar.tf.utils.shapes import shape_list
  from texar.tf.utils.mode import is_train_mode
  from texar.tf.modules.decoders.dynamic_decode import dynamic_decode
label_desc: Use image node_id 7 for calling the TransformerDecoder obj's underlying member method code with example usage: obj._init_cache(memory, memory_attention_bias, beam_search_decoding, batch_size) and returns: cache, tf, tf, tf, tf