Esempio n. 1
0
  def dynamic_decode(self,
                     embedding,
                     start_tokens,
                     end_token,
                     vocab_size=None,
                     initial_state=None,
                     output_layer=None,
                     maximum_iterations=250,
                     mode=tf.estimator.ModeKeys.PREDICT,
                     memory=None,
                     memory_sequence_length=None,
                     dtype=None,
                     return_alignment_history=False):
    batch_size = tf.shape(start_tokens)[0]

    helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(
        embedding,
        start_tokens,
        end_token)

    cell, initial_state = self._build_cell(
        mode,
        batch_size,
        initial_state=initial_state,
        memory=memory,
        memory_sequence_length=memory_sequence_length,
        dtype=dtype,
        alignment_history=return_alignment_history)

    if output_layer is None:
      output_layer = build_output_layer(self.num_units, vocab_size, dtype=dtype or memory.dtype)

    decoder = tf.contrib.seq2seq.BasicDecoder(
        cell,
        helper,
        initial_state,
        output_layer=output_layer)

    outputs, state, length = tf.contrib.seq2seq.dynamic_decode(
        decoder, maximum_iterations=maximum_iterations)

    predicted_ids = outputs.sample_id
    log_probs = logits_to_cum_log_probs(outputs.rnn_output, length)

    # Make shape consistent with beam search.
    predicted_ids = tf.expand_dims(predicted_ids, 1)
    length = tf.expand_dims(length, 1)
    log_probs = tf.expand_dims(log_probs, 1)

    if return_alignment_history:
      alignment_history = _get_alignment_history(state)
      if alignment_history is not None:
        alignment_history = tf.expand_dims(alignment_history, 1)
      return (predicted_ids, state, length, log_probs, alignment_history)
    return (predicted_ids, state, length, log_probs)
Esempio n. 2
0
  def dynamic_decode(self,
                     embedding,
                     start_tokens,
                     end_token,
                     vocab_size,
                     initial_state=None,
                     maximum_iterations=250,
                     mode=tf.estimator.ModeKeys.PREDICT,
                     memory=None,
                     memory_sequence_length=None,
                     dtype=None):
    batch_size = tf.shape(start_tokens)[0]

    helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(
        embedding,
        start_tokens,
        end_token)

    cell, initial_state = self._build_cell(
        mode,
        batch_size,
        initial_state=initial_state,
        memory=memory,
        memory_sequence_length=memory_sequence_length,
        dtype=dtype)

    output_layer = self._build_output_layer(vocab_size, dtype=dtype or memory.dtype)

    decoder = tf.contrib.seq2seq.BasicDecoder(
        cell,
        helper,
        initial_state,
        output_layer=output_layer)

    outputs, state, length = tf.contrib.seq2seq.dynamic_decode(
        decoder, maximum_iterations=maximum_iterations)

    predicted_ids = outputs.sample_id
    log_probs = logits_to_cum_log_probs(outputs.rnn_output, length)

    # Make shape consistent with beam search.
    predicted_ids = tf.expand_dims(predicted_ids, 1)
    length = tf.expand_dims(length, 1)
    log_probs = tf.expand_dims(log_probs, 1)

    return (predicted_ids, state, length, log_probs)
Esempio n. 3
0
    def dynamic_decode(self,
                       embedding,
                       start_tokens,
                       end_token,
                       vocab_size,
                       initial_state=None,
                       maximum_iterations=250,
                       mode=tf.estimator.ModeKeys.PREDICT,
                       memory=None,
                       memory_sequence_length=None):
        batch_size = tf.shape(start_tokens)[0]

        helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(
            embedding, start_tokens, end_token)

        cell, initial_state = self._build_cell(
            mode,
            batch_size,
            initial_state=initial_state,
            memory=memory,
            memory_sequence_length=memory_sequence_length)

        output_layer = self._build_output_layer(vocab_size)

        decoder = tf.contrib.seq2seq.BasicDecoder(cell,
                                                  helper,
                                                  initial_state,
                                                  output_layer=output_layer)

        outputs, state, length = tf.contrib.seq2seq.dynamic_decode(
            decoder, maximum_iterations=maximum_iterations)

        predicted_ids = outputs.sample_id
        log_probs = logits_to_cum_log_probs(outputs.rnn_output, length)

        # Make shape consistent with beam search.
        predicted_ids = tf.expand_dims(predicted_ids, 1)
        log_probs = tf.expand_dims(log_probs, 1)

        return (predicted_ids, state, length, log_probs)