(self, z, base_decode_fn)
return recursive_decode(z)
Depth first decoding from `z`, passing final embeddings to base fn.
Depth first decoding from `z`, passing final embeddings to base fn.
Depth first decoding from z passing final embeddings to base fn .
def _hierarchical_decode(self, z, base_decode_fn):
    """Depth first decoding from `z`, passing final embeddings to base fn."""
    batch_size = z.shape[0]
    # Subtract 1 for the core decoder level.
    num_levels = len(self._level_lengths) - 1

    hparams = self.hparams
    batch_size = hparams.batch_size

    def recursive_decode(initial_input, path=None):
      """Recursive hierarchical decode function."""
      path = path or []
      level = len(path)

      if level == num_levels:
        with tf.variable_scope('core_decoder', reuse=tf.AUTO_REUSE):
          return base_decode_fn(initial_input, path)

      scope = tf.VariableScope(
          tf.AUTO_REUSE, 'decoder/hierarchical_level_%d' % level)
      num_steps = self._level_lengths[level]
      with tf.variable_scope(scope):
        state = lstm_utils.initial_cell_state_from_embedding(
            self._hier_cells[level], initial_input, name='initial_state')
      if level not in self._disable_autoregression:
        # The initial input should be the same size as the tensors returned by
        # next level.
        if self._hierarchical_encoder:
          input_size = self._hierarchical_encoder.level(0).output_depth
        elif level == num_levels - 1:
          input_size = sum(tf.nest.flatten(self._core_decoder.state_size))
        else:
          input_size = sum(
              tf.nest.flatten(self._hier_cells[level + 1].state_size))
        next_input = tf.zeros([batch_size, input_size])
      lower_level_embeddings = []
      for i in range(num_steps):
        if level in self._disable_autoregression:
          next_input = tf.zeros([batch_size, 1])
        else:
          next_input = tf.concat([next_input, initial_input], axis=1)
        with tf.variable_scope(scope):
          output, state = self._hier_cells[level](next_input, state, scope)
        next_input = recursive_decode(output, path + [i])
        lower_level_embeddings.append(next_input)
      if self._hierarchical_encoder:
        # Return the encoding of the outputs using the appropriate level of the
        # hierarchical encoder.
        enc_level = num_levels - level
        return self._hierarchical_encoder.level(enc_level).encode(
            sequence=tf.stack(lower_level_embeddings, axis=1),
            sequence_length=tf.fill([batch_size], num_steps))
      else:
        # Return the final state.
        return tf.concat(tf.nest.flatten(state), axis=-1)

    return recursive_decode(z)
def _hierarchical_decode ( self , z , base_decode_fn ) : batch_size = z . shape [ 0 ] # Subtract 1 for the core decoder level. num_levels = len ( self . _level_lengths ) - 1 hparams = self . hparams batch_size = hparams . batch_size def recursive_decode ( initial_input , path = None ) : """Recursive hierarchical decode function.""" path = path or [ ] level = len ( path ) if level == num_levels : with tf . variable_scope ( 'core_decoder' , reuse = tf . AUTO_REUSE ) : return base_decode_fn ( initial_input , path ) scope = tf . VariableScope ( tf . AUTO_REUSE , 'decoder/hierarchical_level_%d' % level ) num_steps = self . _level_lengths [ level ] with tf . variable_scope ( scope ) : state = lstm_utils . initial_cell_state_from_embedding ( self . _hier_cells [ level ] , initial_input , name = 'initial_state' ) if level not in self . _disable_autoregression : # The initial input should be the same size as the tensors returned by # next level. if self . _hierarchical_encoder : input_size = self . _hierarchical_encoder . level ( 0 ) . output_depth elif level == num_levels - 1 : input_size = sum ( tf . nest . flatten ( self . _core_decoder . state_size ) ) else : input_size = sum ( tf . nest . flatten ( self . _hier_cells [ level + 1 ] . state_size ) ) next_input = tf . zeros ( [ batch_size , input_size ] ) lower_level_embeddings = [ ] for i in range ( num_steps ) : if level in self . _disable_autoregression : next_input = tf . zeros ( [ batch_size , 1 ] ) else : next_input = tf . concat ( [ next_input , initial_input ] , axis = 1 ) with tf . variable_scope ( scope ) : output , state = self . _hier_cells [ level ] ( next_input , state , scope ) next_input = recursive_decode ( output , path + [ i ] ) lower_level_embeddings . append ( next_input ) if self . _hierarchical_encoder : # Return the encoding of the outputs using the appropriate level of the # hierarchical encoder. enc_level = num_levels - level return self . _hierarchical_encoder . level ( enc_level ) . encode ( sequence = tf . stack ( lower_level_embeddings , axis = 1 ) , sequence_length = tf . fill ( [ batch_size ] , num_steps ) ) else : # Return the final state. return tf . concat ( tf . nest . flatten ( state ) , axis = - 1 ) return recursive_decode ( z )