(self, n, max_length=None, z=None, c_input=None,
             **core_sampler_kwargs)
return all_samples, self._merge_decode_results(all_decode_results)
Sample from decoder with an optional conditional latent vector `z`.

    Args:
      n: Scalar number of samples to return.
      max_length: (Optional) maximum total length of samples. If given, must
        match `hparams.max_seq_len`.
      z: (Optional) Latent vectors to sample from. Required if model is
        conditional. Sized `[n, z_size]`.
      c_input: (Optional) Control sequence, sized `[max_length, control_depth]`.
      **core_sampler_kwargs: (Optional) Additional keyword arguments to pass to
        core sampler.
    Returns:
      samples: Sampled sequences with concenated, possibly padded segments.
         Sized `[n, max_length, output_depth]`.
      decoder_results: The merged LstmDecodeResults from sampling.
    Raises:
      ValueError: If `z` is provided and its first dimension does not equal `n`,
        or if `c_input` is provided in re-encoder mode.
Sample from decoder with an optional conditional latent vector `z`.
Sample from decoder with an optional conditional latent vector z .
def sample(self, n, max_length=None, z=None, c_input=None,
             **core_sampler_kwargs):
    """Sample from decoder with an optional conditional latent vector `z`.

    Args:
      n: Scalar number of samples to return.
      max_length: (Optional) maximum total length of samples. If given, must
        match `hparams.max_seq_len`.
      z: (Optional) Latent vectors to sample from. Required if model is
        conditional. Sized `[n, z_size]`.
      c_input: (Optional) Control sequence, sized `[max_length, control_depth]`.
      **core_sampler_kwargs: (Optional) Additional keyword arguments to pass to
        core sampler.
    Returns:
      samples: Sampled sequences with concenated, possibly padded segments.
         Sized `[n, max_length, output_depth]`.
      decoder_results: The merged LstmDecodeResults from sampling.
    Raises:
      ValueError: If `z` is provided and its first dimension does not equal `n`,
        or if `c_input` is provided in re-encoder mode.
    """
    if z is not None and int(z.shape[0]) != n:
      raise ValueError(
          '`z` must have a first dimension that equals `n` when given. '
          'Got: %d vs %d' % (z.shape[0], n))
    z = tf.zeros([n, 0]) if z is None else z

    if self._hierarchical_encoder and c_input is not None:
      raise ValueError(
          'Re-encoder mode unsupported when conditioning on controls.')

    if max_length is not None:
      with tf.control_dependencies([
          tf.assert_equal(
              max_length, self._total_length,
              message='`max_length` must equal `hparams.max_seq_len` if given.')
      ]):
        max_length = tf.identity(max_length)

    if c_input is not None:
      # Reshape control sequence to hierarchy.
      c_input = tf.squeeze(
          self._reshape_to_hierarchy(tf.expand_dims(c_input, 0)),
          axis=len(self._level_lengths) - 1)

    core_max_length = self._level_lengths[-1]
    all_samples = []
    all_decode_results = []

    def base_sample_fn(embedding, hier_index):
      """Base function for sampling hierarchical decoder."""
      samples, decode_results = self._core_decoder.sample(
          n,
          max_length=core_max_length,
          z=embedding,
          c_input=c_input[hier_index] if c_input is not None else None,
          start_inputs=all_samples[-1][:, -1] if all_samples else None,
          **core_sampler_kwargs)
      all_samples.append(samples)
      all_decode_results.append(decode_results)
      if self._hierarchical_encoder:
        return self._hierarchical_encoder.level(0).encode(
            samples,
            decode_results.final_sequence_lengths)
      else:
        return tf.concat(tf.nest.flatten(decode_results.final_state), axis=-1)

    # Populate `all_sample_ids`.
    self._hierarchical_decode(z, base_sample_fn)

    all_samples = tf.concat(
        [tf.pad(s, [(0, 0), (0, core_max_length - tf.shape(s)[1]), (0, 0)])
         for s in all_samples],
        axis=1)
    return all_samples, self._merge_decode_results(all_decode_results)
def sample ( self , n , max_length = None , z = None , c_input = None , * * core_sampler_kwargs ) : if z is not None and int ( z . shape [ 0 ] ) != n : raise ValueError ( '`z` must have a first dimension that equals `n` when given. ' 'Got: %d vs %d' % ( z . shape [ 0 ] , n ) ) z = tf . zeros ( [ n , 0 ] ) if z is None else z if self . _hierarchical_encoder and c_input is not None : raise ValueError ( 'Re-encoder mode unsupported when conditioning on controls.' ) if max_length is not None : with tf . control_dependencies ( [ tf . assert_equal ( max_length , self . _total_length , message = '`max_length` must equal `hparams.max_seq_len` if given.' ) ] ) : max_length = tf . identity ( max_length ) if c_input is not None : # Reshape control sequence to hierarchy. c_input = tf . squeeze ( self . _reshape_to_hierarchy ( tf . expand_dims ( c_input , 0 ) ) , axis = len ( self . _level_lengths ) - 1 ) core_max_length = self . _level_lengths [ - 1 ] all_samples = [ ] all_decode_results = [ ] def base_sample_fn ( embedding , hier_index ) : """Base function for sampling hierarchical decoder.""" samples , decode_results = self . _core_decoder . sample ( n , max_length = core_max_length , z = embedding , c_input = c_input [ hier_index ] if c_input is not None else None , start_inputs = all_samples [ - 1 ] [ : , - 1 ] if all_samples else None , * * core_sampler_kwargs ) all_samples . append ( samples ) all_decode_results . append ( decode_results ) if self . _hierarchical_encoder : return self . _hierarchical_encoder . level ( 0 ) . encode ( samples , decode_results . final_sequence_lengths ) else : return tf . concat ( tf . nest . flatten ( decode_results . final_state ) , axis = - 1 ) # Populate `all_sample_ids`. self . _hierarchical_decode ( z , base_sample_fn ) all_samples = tf . concat ( [ tf . pad ( s , [ ( 0 , 0 ) , ( 0 , core_max_length - tf . shape ( s ) [ 1 ] ) , ( 0 , 0 ) ] ) for s in all_samples ] , axis = 1 ) return all_samples , self . _merge_decode_results ( all_decode_results )