(self, n, max_length=None, z=None, c_input=None, temperature=1.0,
             start_inputs=None, end_fn=None)
return decode_results.samples, decode_results
Sample from decoder with an optional conditional latent vector `z`.

    Args:
      n: Scalar number of samples to return.
      max_length: (Optional) Scalar maximum sample length to return. Required if
        data representation does not include end tokens.
      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]`.
      temperature: (Optional) The softmax temperature to use when sampling, if
        applicable.
      start_inputs: (Optional) Initial inputs to use for batch.
        Sized `[n, output_depth]`.
      end_fn: (Optional) A callable that takes a batch of samples (sized
        `[n, output_depth]` and emits a `bool` vector
        shaped `[batch_size]` indicating whether each sample is an end token.
    Returns:
      samples: Sampled sequences. Sized `[n, max_length, output_depth]`.
      final_state: The final states of the decoder.
    Raises:
      ValueError: If `z` is provided and its first dimension does not equal `n`.
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, temperature=1.0,
             start_inputs=None, end_fn=None):
    """Sample from decoder with an optional conditional latent vector `z`.

    Args:
      n: Scalar number of samples to return.
      max_length: (Optional) Scalar maximum sample length to return. Required if
        data representation does not include end tokens.
      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]`.
      temperature: (Optional) The softmax temperature to use when sampling, if
        applicable.
      start_inputs: (Optional) Initial inputs to use for batch.
        Sized `[n, output_depth]`.
      end_fn: (Optional) A callable that takes a batch of samples (sized
        `[n, output_depth]` and emits a `bool` vector
        shaped `[batch_size]` indicating whether each sample is an end token.
    Returns:
      samples: Sampled sequences. Sized `[n, max_length, output_depth]`.
      final_state: The final states of the decoder.
    Raises:
      ValueError: If `z` is provided and its first dimension does not equal `n`.
    """
    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))

    # Use a dummy Z in unconditional case.
    z = tf.zeros((n, 0), tf.float32) if z is None else z

    if c_input is not None:
      # Tile control sequence across samples.
      c_input = tf.tile(tf.expand_dims(c_input, 1), [1, n, 1])

    # If not given, start with zeros.
    if start_inputs is None:
      start_inputs = tf.zeros([n, self._output_depth], dtype=tf.float32)
    # In the conditional case, also concatenate the Z.
    start_inputs = tf.concat([start_inputs, z], axis=-1)
    if c_input is not None:
      start_inputs = tf.concat([start_inputs, c_input[0]], axis=-1)
    initialize_fn = lambda: (tf.zeros([n], tf.bool), start_inputs)

    sample_fn = lambda time, outputs, state: self._sample(outputs, temperature)
    end_fn = end_fn or (lambda x: False)

    def next_inputs_fn(time, outputs, state, sample_ids):
      del outputs
      finished = end_fn(sample_ids)
      next_inputs = tf.concat([sample_ids, z], axis=-1)
      if c_input is not None:
        # We need to stop if we've run out of control input.
        finished = tf.cond(tf.less(time, tf.shape(c_input)[0] - 1),
                           lambda: finished,
                           lambda: True)
        next_inputs = tf.concat([
            next_inputs,
            tf.cond(tf.less(time, tf.shape(c_input)[0] - 1),
                    lambda: c_input[time + 1],
                    lambda: tf.zeros_like(c_input[0]))  # should be unused
        ], axis=-1)
      return (finished, next_inputs, state)

    sampler = contrib_seq2seq.CustomHelper(
        initialize_fn=initialize_fn, sample_fn=sample_fn,
        next_inputs_fn=next_inputs_fn, sample_ids_shape=[self._output_depth],
        sample_ids_dtype=tf.float32)

    decode_results = self._decode(
        z, helper=sampler, input_shape=start_inputs.shape[1:],
        max_length=max_length)

    return decode_results.samples, decode_results
def sample ( self , n , max_length = None , z = None , c_input = None , temperature = 1.0 , start_inputs = None , end_fn = None ) : 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 ) ) # Use a dummy Z in unconditional case. z = tf . zeros ( ( n , 0 ) , tf . float32 ) if z is None else z if c_input is not None : # Tile control sequence across samples. c_input = tf . tile ( tf . expand_dims ( c_input , 1 ) , [ 1 , n , 1 ] ) # If not given, start with zeros. if start_inputs is None : start_inputs = tf . zeros ( [ n , self . _output_depth ] , dtype = tf . float32 ) # In the conditional case, also concatenate the Z. start_inputs = tf . concat ( [ start_inputs , z ] , axis = - 1 ) if c_input is not None : start_inputs = tf . concat ( [ start_inputs , c_input [ 0 ] ] , axis = - 1 ) initialize_fn = lambda : ( tf . zeros ( [ n ] , tf . bool ) , start_inputs ) sample_fn = lambda time , outputs , state : self . _sample ( outputs , temperature ) end_fn = end_fn or ( lambda x : False ) def next_inputs_fn ( time , outputs , state , sample_ids ) : del outputs finished = end_fn ( sample_ids ) next_inputs = tf . concat ( [ sample_ids , z ] , axis = - 1 ) if c_input is not None : # We need to stop if we've run out of control input. finished = tf . cond ( tf . less ( time , tf . shape ( c_input ) [ 0 ] - 1 ) , lambda : finished , lambda : True ) next_inputs = tf . concat ( [ next_inputs , tf . cond ( tf . less ( time , tf . shape ( c_input ) [ 0 ] - 1 ) , lambda : c_input [ time + 1 ] , lambda : tf . zeros_like ( c_input [ 0 ] ) ) # should be unused ] , axis = - 1 ) return ( finished , next_inputs , state ) sampler = contrib_seq2seq . CustomHelper ( initialize_fn = initialize_fn , sample_fn = sample_fn , next_inputs_fn = next_inputs_fn , sample_ids_shape = [ self . _output_depth ] , sample_ids_dtype = tf . float32 ) decode_results = self . _decode ( z , helper = sampler , input_shape = start_inputs . shape [ 1 : ] , max_length = max_length ) return decode_results . samples , decode_results