Python tensorflow.contrib.seq2seq.dynamic_decode() Examples

The following are 15 code examples of tensorflow.contrib.seq2seq.dynamic_decode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.contrib.seq2seq , or try the search function .
Example #1
Source File: seq2seq_decoder_estimator.py    From icecaps with MIT License 6 votes vote down vote up
def build_train_decoder(self):
        with tf.name_scope('train_decoder'):
            training_helper = TrainingHelper(inputs=self.inputs_dense,
                                             sequence_length=self.inputs_length,
                                             time_major=False,
                                             name='training_helper')
            with tf.name_scope('basic_decoder'):
                training_decoder = BasicDecoder(cell=self.cell,
                                                helper=training_helper,
                                                initial_state=self.initial_state,
                                                output_layer=self.output_layer)
            with tf.name_scope('dynamic_decode'):
                (outputs, self.last_state, self.outputs_length) = (seq2seq.dynamic_decode(
                    decoder=training_decoder,
                    output_time_major=False,
                    impute_finished=True,
                    maximum_iterations=self.inputs_max_length))
                self.logits = tf.identity(outputs.rnn_output)
                self.log_probs = tf.nn.log_softmax(self.logits)
                self.gs_hypotheses = tf.argmax(self.log_probs, -1) 
Example #2
Source File: seq2seq.py    From retrosynthesis_planner with GNU General Public License v3.0 5 votes vote down vote up
def _make_predict(self, decoder_cell, decoder_initial_state):
        # Access embeddings directly
        with tf.variable_scope('embed', reuse=True):
            embeddings = tf.get_variable('embeddings')

        # Assume 0 is the START token
        start_tokens = tf.zeros((self.batch_size,), dtype=tf.int32)

        # For predictions, we use beam search to return multiple results
        with tf.variable_scope('decode', reuse=True):
            # Project to correct dimensions
            out_proj = tf.layers.Dense(self.vocab_size, name='output_proj')
            embeddings = tf.layers.dense(embeddings, self.hidden_size, name='input_proj')

            decoder = seq2seq.BeamSearchDecoder(
                cell=decoder_cell,
                embedding=embeddings,
                start_tokens=start_tokens,
                end_token=END,
                initial_state=decoder_initial_state,
                beam_width=self.beam_width,
                output_layer=out_proj
            )

            final_outputs, final_state, final_sequence_lengths = seq2seq.dynamic_decode(
                decoder=decoder, impute_finished=False, maximum_iterations=self.max_decode_iter)

        # Swap axes for an order that makes more sense (to me)
        # such that we have [batch_size, beam_width, T], i.e.
        # each row is a output sequence
        return tf.transpose(final_outputs.predicted_ids, [0,2,1]) 
Example #3
Source File: lm.py    From avsr-tf1 with GNU General Public License v3.0 5 votes vote down vote up
def _build_decoder_train(self):
        self._decoder_train_inputs = tf.nn.embedding_lookup(self._embedding_matrix, self._labels_padded_GO)

        if self._mode == 'train':
            sampler = seq2seq.ScheduledEmbeddingTrainingHelper(
                inputs=self._decoder_train_inputs,
                sequence_length=self._labels_length,
                embedding=self._embedding_matrix,
                sampling_probability=self._sampling_probability_outputs,
            )
        else:
            sampler = seq2seq.TrainingHelper(
                inputs=self._decoder_train_inputs,
                sequence_length=self._labels_length,
            )

        cells = self._decoder_cells

        decoder_train = seq2seq.BasicDecoder(
            cell=cells,
            helper=sampler,
            initial_state=self._decoder_initial_state,
            output_layer=self._dense_layer,
        )

        outputs, _, _ = seq2seq.dynamic_decode(
            decoder_train,
            output_time_major=False,
            impute_finished=True,
            swap_memory=False,
        )

        logits = outputs.rnn_output
        self.decoder_train_outputs = logits
        self.average_log_likelihoods = self._compute_likelihood(logits)
        print('') 
Example #4
Source File: agreement.py    From language with Apache License 2.0 5 votes vote down vote up
def get_decode_func(self, embeddings,
                      inputs, inputs_length,
                      hiddens, hiddens_length,
                      enc_state,
                      mode=None,
                      decoder_hparams=None,
                      impute_finished=False,
                      decoder_iterations=None):
    """Return a closure for decoding."""

    def decode_func():
      """A closure that builds decoder outputs."""
      dec_outputs, _, dec_lengths = contrib_seq2seq.dynamic_decode(
          decoder=self.decoder(
              embeddings=embeddings,
              inputs=inputs,
              inputs_length=inputs_length,
              hiddens=hiddens,
              hiddens_length=hiddens_length,
              enc_state=enc_state,
              mode=mode,
              hparams=self._hparams,
              decoder_hparams=decoder_hparams,
              reuse=tf.AUTO_REUSE),
          impute_finished=impute_finished,
          maximum_iterations=decoder_iterations)
      return {
          "rnn_output": dec_outputs.rnn_output,
          "sample_id": dec_outputs.sample_id,
          "length": dec_lengths}
    return decode_func 
Example #5
Source File: basic.py    From language with Apache License 2.0 5 votes vote down vote up
def body(self, features):
    """Process features and produce outputs."""
    # Preprocess features.
    inputs, inputs_length, targets, targets_length = self._preprocess(features)

    # Encode.
    encoder = encoders.get(self._hparams.encoder_type)
    enc_outputs = encoder(
        inputs=inputs,
        inputs_length=inputs_length,
        mode=self._hparams.mode,
        hparams=self._hparams)

    # Get target embeddings.
    target_modality = self._problem_hparams.modality["targets"]
    target_modality_scope = self._variable_scopes[target_modality.name]
    target_embeddings = model_utils.get_embeddings(
        modality=target_modality,
        outer_scope=target_modality_scope,
        inner_scope="shared")

    # Decode.
    decoder = decoders.get(self._hparams.decoder_type)
    decoder = decoder(
        embeddings=target_embeddings,
        inputs=targets,
        inputs_length=targets_length,
        hiddens=enc_outputs.outputs,
        hiddens_length=inputs_length,
        enc_state=enc_outputs.final_state,
        mode=self._hparams.mode,
        hparams=self._hparams)
    dec_outputs, _, _ = contrib_seq2seq.dynamic_decode(decoder=decoder)

    return tf.expand_dims(dec_outputs.rnn_output, axis=2)


# ------------------------------------------------------------------------------
# Hparams
# ------------------------------------------------------------------------------ 
Example #6
Source File: seq2seq_decoder_estimator.py    From icecaps with MIT License 5 votes vote down vote up
def build_rt_decoder(self):
        self.build_embeddings()
        start_tokens_sparse = tf.ones(shape=[self.batch_size], dtype=tf.int32) * self.vocab.start_token_id
        with tf.name_scope('beamsearch_decoder'):
            rt_decoder = BeamSearchDecoder(cell=self.cell,
                                           embedding=self.embed_sparse_to_dense,
                                           start_tokens=start_tokens_sparse,
                                           end_token=self.vocab.end_token_id,
                                           initial_state=self.initial_state,
                                           beam_width=self.hparams.beam_width,
                                           output_layer=self.output_layer,
                                           skip_tokens_decoding=self.vocab.skip_tokens,
                                           shrink_vocab=self.hparams.shrink_vocab)
            (hypotheses, input_query_ids, scores) = dynamic_decode(
                decoder=rt_decoder,
                output_time_major=False,
                maximum_iterations=self.hparams.max_length,
                repetition=self.hparams.repetition_penalty)

            sort_ids = tf.argsort(
                scores, direction="DESCENDING", stable=True, axis=0)
            scores = tf.gather_nd(scores, sort_ids)
            hypotheses = tf.gather_nd(hypotheses, sort_ids)
            input_query_ids = tf.gather_nd(input_query_ids, sort_ids)

            sort_ids = tf.argsort(
                input_query_ids, direction="ASCENDING", stable=True, axis=0)
            scores = tf.gather_nd(scores, sort_ids)
            hypotheses = tf.gather_nd(hypotheses, sort_ids)
            input_query_ids = tf.gather_nd(input_query_ids, sort_ids)

            input_queries = tf.gather_nd(tf.convert_to_tensor(
                self.features["original_inputs"]), input_query_ids)
            self.rt_hypotheses = tf.identity(hypotheses)
            self.inputs_pred = tf.identity(input_queries)
            self.scores = tf.identity(scores) 
Example #7
Source File: seq2seq_decoder_estimator.py    From icecaps with MIT License 5 votes vote down vote up
def build_mmi_decoder(self):
        with tf.name_scope('mmi_scorer'):
            training_helper = TrainingHelper(inputs=self.inputs_dense,
                                             sequence_length=self.inputs_length,
                                             time_major=False,
                                             name='mmi_training_helper')
            with tf.name_scope('mmi_basic_decoder'):
                training_decoder = MMIDecoder(cell=self.cell,
                                              helper=training_helper,
                                              initial_state=self.initial_state,
                                              output_layer=self.output_layer)
            with tf.name_scope('mmi_dynamic_decoder'):
                (outputs, self.last_state, self.outputs_length) = seq2seq.dynamic_decode(
                    decoder=training_decoder,
                    output_time_major=False,
                    impute_finished=True,
                    maximum_iterations=self.inputs_max_length)

            self.scores_raw = tf.identity(
                tf.transpose(outputs.scores, [1, 2, 0]))
            targets = self.features["targets"]
            targets = tf.cast(targets, dtype=tf.int32)
            target_len = tf.cast(tf.count_nonzero(
                targets - self.vocab.end_token_id, -1), dtype=tf.int32)
            max_target_len = tf.reduce_max(target_len)
            pruned_targets = tf.slice(targets, [0, 0], [-1, max_target_len])

            index = (tf.range(0, max_target_len, 1)) * \
                tf.ones(shape=[self.batch_size, 1], dtype=tf.int32)
            row_no = tf.transpose(tf.range(
                0, self.batch_size, 1) * tf.ones(shape=(max_target_len, 1), dtype=tf.int32))
            indices = tf.stack([index, pruned_targets, row_no], axis=2)

            # Retrieve scores corresponding to indices
            batch_scores = tf.gather_nd(self.scores_raw, indices)
            self.mmi_scores = tf.reduce_sum(batch_scores, axis=1) 
Example #8
Source File: seq2seq.py    From retrosynthesis_planner with GNU General Public License v3.0 4 votes vote down vote up
def _make_train(self, decoder_cell, decoder_initial_state):
        # Assume 0 is the START token
        start_tokens = tf.zeros((self.batch_size,), dtype=tf.int32)
        y = tf.concat([tf.expand_dims(start_tokens, 1), self.y], 1)
        output_lengths = tf.reduce_sum(tf.to_int32(tf.not_equal(y, 1)), 1)

        # Reuse encoding embeddings
        inputs = layers.embed_sequence(
            y,
            vocab_size=self.vocab_size,
            embed_dim=self.embed_dim,
            scope='embed', reuse=True)

        # Prepare the decoder with the attention cell
        with tf.variable_scope('decode'):
            # Project to correct dimensions
            out_proj = tf.layers.Dense(self.vocab_size, name='output_proj')
            inputs = tf.layers.dense(inputs, self.hidden_size, name='input_proj')

            helper = seq2seq.TrainingHelper(inputs, output_lengths)
            decoder = seq2seq.BasicDecoder(
                cell=decoder_cell, helper=helper,
                initial_state=decoder_initial_state,
                output_layer=out_proj)
            max_len = tf.reduce_max(output_lengths)
            final_outputs, final_state, final_sequence_lengths = seq2seq.dynamic_decode(
                decoder=decoder, impute_finished=True, maximum_iterations=max_len)
            logits = final_outputs.rnn_output

        # Set valid timesteps to 1 and padded steps to 0,
        # so we only look at the actual sequence without the padding
        mask = tf.sequence_mask(output_lengths, maxlen=max_len, dtype=tf.float32)

        # Prioritize examples that the model was wrong on,
        # by setting weight=1 to any example where the prediction was not 1,
        # i.e. incorrect
        # weights = tf.to_float(tf.not_equal(y[:, :-1], 1))

        # Training and loss ops,
        # with gradient clipping (see [4])
        loss_op = seq2seq.sequence_loss(logits, self.y, weights=mask)
        optimizer = tf.train.AdamOptimizer(self.learning_rate)
        gradients, variables = zip(*optimizer.compute_gradients(loss_op))
        gradients, _ = tf.clip_by_global_norm(gradients, self.max_grad_norm)
        train_op = optimizer.apply_gradients(zip(gradients, variables))

        # Compute accuracy
        # Use the mask from before so we only compare
        # the relevant sequence lengths for each example
        pred = tf.argmax(logits, axis=2, output_type=tf.int32)
        pred = tf.boolean_mask(pred, mask)
        true = tf.boolean_mask(self.y, mask)
        accs = tf.cast(tf.equal(pred, true), tf.float32)
        accuracy_op = tf.reduce_mean(accs, name='acc')
        return loss_op, train_op, accuracy_op 
Example #9
Source File: decoder_unimodal.py    From avsr-tf1 with GNU General Public License v3.0 4 votes vote down vote up
def _build_decoder_test_greedy(self):
        r"""
        Builds the greedy test decoder, which feeds the most likely decoded symbol as input for the
        next timestep
        """
        self._helper_greedy = seq2seq.GreedyEmbeddingHelper(
            embedding=self._embedding_matrix,
            start_tokens=tf.tile([self._GO_ID], [self._batch_size]),
            end_token=self._EOS_ID)

        if self._hparams.enable_attention is True:
            cells, initial_state = add_attention(
                cells=self._decoder_cells,
                attention_types=self._hparams.attention_type[1],
                num_units=self._hparams.decoder_units_per_layer[-1],
                memory=self._encoder_memory,
                memory_len=self._encoder_features_len,
                beam_search=False,
                batch_size=self._batch_size,
                initial_state=self._decoder_initial_state,
                mode=self._mode,
                dtype=self._hparams.dtype,
                fusion_type='linear_fusion',
                write_attention_alignment=self._hparams.write_attention_alignment)
        else:
            cells = self._decoder_cells
            initial_state = self._decoder_initial_state

        self._decoder_inference = seq2seq.BasicDecoder(
            cell=cells,
            helper=self._helper_greedy,
            initial_state=initial_state,
            output_layer=self._dense_layer)

        outputs, states, lengths = seq2seq.dynamic_decode(
            self._decoder_inference,
            impute_finished=True,
            swap_memory=False,
            maximum_iterations=self._hparams.max_label_length)

        self.inference_outputs = outputs.rnn_output
        self.inference_predicted_ids = outputs.sample_id

        if self._hparams.write_attention_alignment is True:
            self.attention_summary = self._create_attention_alignments_summary(states) 
Example #10
Source File: decoder_unimodal.py    From avsr-tf1 with GNU General Public License v3.0 4 votes vote down vote up
def _build_decoder_test_beam_search(self):
        r"""
        Builds a beam search test decoder
        """
        if self._hparams.enable_attention is True:
            cells, initial_state = add_attention(
                cells=self._decoder_cells,
                attention_types=self._hparams.attention_type[1],
                num_units=self._hparams.decoder_units_per_layer[-1],
                memory=self._encoder_memory,
                memory_len=self._encoder_features_len,
                beam_search=True,
                batch_size=self._batch_size,
                beam_width=self._hparams.beam_width,
                initial_state=self._decoder_initial_state,
                mode=self._mode,
                dtype=self._hparams.dtype,
                fusion_type='linear_fusion',
                write_attention_alignment=self._hparams.write_attention_alignment)
        else:  # does the non-attentive beam decoder need tile_batch ?
            cells = self._decoder_cells

            decoder_initial_state_tiled = seq2seq.tile_batch(  # guess so ? it compiles without it too
                self._decoder_initial_state, multiplier=self._hparams.beam_width)
            initial_state = decoder_initial_state_tiled

        self._decoder_inference = seq2seq.BeamSearchDecoder(
            cell=cells,
            embedding=self._embedding_matrix,
            start_tokens=array_ops.fill([self._batch_size], self._GO_ID),
            end_token=self._EOS_ID,
            initial_state=initial_state,
            beam_width=self._hparams.beam_width,
            output_layer=self._dense_layer,
            length_penalty_weight=0.6,
        )

        outputs, states, lengths = seq2seq.dynamic_decode(
            self._decoder_inference,
            impute_finished=False,
            maximum_iterations=self._hparams.max_label_length,
            swap_memory=False)

        if self._hparams.write_attention_alignment is True:
            self.attention_summary, self.attention_alignment = self._create_attention_alignments_summary(states)

        self.inference_outputs = outputs.beam_search_decoder_output
        self.inference_predicted_ids = outputs.predicted_ids[:, :, 0]  # return the first beam
        self.inference_predicted_beam = outputs.predicted_ids
        self.beam_search_output = outputs.beam_search_decoder_output 
Example #11
Source File: decoder_unimodal.py    From avsr-tf1 with GNU General Public License v3.0 4 votes vote down vote up
def _basic_decoder_train(self):
        r"""
        Builds the standard teacher-forcing training decoder with sampling from previous predictions.
        """

        helper_train = seq2seq.ScheduledEmbeddingTrainingHelper(
            inputs=self._decoder_train_inputs,
            sequence_length=self._labels_len,
            embedding=self._embedding_matrix,
            sampling_probability=self._sampling_probability_outputs,
        )

        # christian_fun = lambda logits: tf.math.top_k(logits, 3).indices
        #
        # helper_train = seq2seq.ScheduledOutputTrainingHelper(
        #     inputs=self._decoder_train_inputs,
        #     sequence_length=self._labels_len,
        #     sampling_probability=self._sampling_probability_outputs,
        # )

        if self._hparams.enable_attention is True:
            cells, initial_state = add_attention(
                cells=self._decoder_cells,
                attention_types=self._hparams.attention_type[1],
                num_units=self._hparams.decoder_units_per_layer[-1],
                memory=self._encoder_memory,
                memory_len=self._encoder_features_len,
                initial_state=self._decoder_initial_state,
                batch_size=self._batch_size,
                mode=self._mode,
                dtype=self._hparams.dtype,
                fusion_type='linear_fusion',
                write_attention_alignment=False,  # we are in train mode
            )
        else:
            cells = self._decoder_cells
            initial_state = self._decoder_initial_state

        decoder_train = seq2seq.BasicDecoder(
            cell=cells,
            helper=helper_train,
            initial_state=initial_state,
            output_layer=self._dense_layer,
        )

        outputs, fstate, fseqlen = seq2seq.dynamic_decode(
            decoder_train,
            output_time_major=False,
            impute_finished=True,
            swap_memory=False,

        )

        return outputs, fstate, fseqlen 
Example #12
Source File: decoder_bimodal.py    From avsr-tf1 with GNU General Public License v3.0 4 votes vote down vote up
def _build_decoder_train(self):

        self._labels_embedded = tf.nn.embedding_lookup(self._embedding_matrix, self._labels_padded_GO)

        self._helper_train = seq2seq.ScheduledEmbeddingTrainingHelper(
            inputs=self._labels_embedded,
            sequence_length=self._labels_len,
            embedding=self._embedding_matrix,
            sampling_probability=self._sampling_probability_outputs,
        )

        if self._hparams.enable_attention is True:
            attention_mechanisms, layer_sizes = self._create_attention_mechanisms()

            attention_cells = seq2seq.AttentionWrapper(
                cell=self._decoder_cells,
                attention_mechanism=attention_mechanisms,
                attention_layer_size=layer_sizes,
                initial_cell_state=self._decoder_initial_state,
                alignment_history=False,
                output_attention=self._output_attention,
            )
            batch_size, _ = tf.unstack(tf.shape(self._labels))

            attn_zero = attention_cells.zero_state(
                dtype=self._hparams.dtype, batch_size=batch_size
            )
            initial_state = attn_zero.clone(
                cell_state=self._decoder_initial_state
            )

            cells = attention_cells
        else:
            cells = self._decoder_cells
            initial_state = self._decoder_initial_state

        self._decoder_train = seq2seq.BasicDecoder(
            cell=cells,
            helper=self._helper_train,
            initial_state=initial_state,
            output_layer=self._dense_layer,
        )

        self._basic_decoder_train_outputs, self._final_states, self._final_seq_lens = seq2seq.dynamic_decode(
            self._decoder_train,
            output_time_major=False,
            impute_finished=True,
            swap_memory=False,
        )

        self._logits = self._basic_decoder_train_outputs.rnn_output 
Example #13
Source File: decoder_bimodal.py    From avsr-tf1 with GNU General Public License v3.0 4 votes vote down vote up
def _build_decoder_greedy(self):

        batch_size, _ = tf.unstack(tf.shape(self._labels))
        self._helper_greedy = seq2seq.GreedyEmbeddingHelper(
            embedding=self._embedding_matrix,
            start_tokens=tf.tile([self._GO_ID], [batch_size]),
            end_token=self._EOS_ID)

        if self._hparams.enable_attention is True:
            attention_mechanisms, layer_sizes = self._create_attention_mechanisms()

            attention_cells = seq2seq.AttentionWrapper(
                cell=self._decoder_cells,
                attention_mechanism=attention_mechanisms,
                attention_layer_size=layer_sizes,
                initial_cell_state=self._decoder_initial_state,
                alignment_history=self._hparams.write_attention_alignment,
                output_attention=self._output_attention
            )
            attn_zero = attention_cells.zero_state(
                dtype=self._hparams.dtype, batch_size=batch_size
            )
            initial_state = attn_zero.clone(
                cell_state=self._decoder_initial_state
            )
            cells = attention_cells
        else:
            cells = self._decoder_cells
            initial_state = self._decoder_initial_state

        self._decoder_inference = seq2seq.BasicDecoder(
            cell=cells,
            helper=self._helper_greedy,
            initial_state=initial_state,
            output_layer=self._dense_layer)

        outputs, states, lengths = seq2seq.dynamic_decode(
            self._decoder_inference,
            impute_finished=True,
            swap_memory=False,
            maximum_iterations=self._hparams.max_label_length)

        # self._result = outputs, states, lengths
        self.inference_outputs = outputs.rnn_output
        self.inference_predicted_ids = outputs.sample_id

        if self._hparams.write_attention_alignment is True:
            self.attention_summary = self._create_attention_alignments_summary(states) 
Example #14
Source File: seq2seq_model.py    From AmusingPythonCodes with MIT License 4 votes vote down vote up
def _build_model(self):
        with tf.variable_scope("embeddings"):
            self.source_embs = tf.get_variable(name="source_embs", shape=[self.cfg.source_vocab_size, self.cfg.emb_dim],
                                               dtype=tf.float32, trainable=True)
            self.target_embs = tf.get_variable(name="embeddings", shape=[self.cfg.vocab_size, self.cfg.emb_dim],
                                               dtype=tf.float32, trainable=True)
            source_emb = tf.nn.embedding_lookup(self.source_embs, self.enc_source)
            target_emb = tf.nn.embedding_lookup(self.target_embs, self.dec_target_in)
            print("source embedding shape: {}".format(source_emb.get_shape().as_list()))
            print("target input embedding shape: {}".format(target_emb.get_shape().as_list()))

        with tf.variable_scope("encoder"):
            if self.cfg.use_bi_rnn:
                with tf.variable_scope("bi-directional_rnn"):
                    cell_fw = GRUCell(self.cfg.num_units) if self.cfg.cell_type == "gru" else \
                        LSTMCell(self.cfg.num_units)
                    cell_bw = GRUCell(self.cfg.num_units) if self.cfg.cell_type == "gru" else \
                        LSTMCell(self.cfg.num_units)
                    bi_outputs, _ = bidirectional_dynamic_rnn(cell_fw, cell_bw, source_emb, dtype=tf.float32,
                                                              sequence_length=self.enc_seq_len)
                    source_emb = tf.concat(bi_outputs, axis=-1)
                    print("bi-directional rnn output shape: {}".format(source_emb.get_shape().as_list()))
            input_project = tf.layers.Dense(units=self.cfg.num_units, dtype=tf.float32, name="input_projection")
            source_emb = input_project(source_emb)
            print("encoder input projection shape: {}".format(source_emb.get_shape().as_list()))
            enc_cells = self._create_encoder_cell()
            self.enc_outputs, self.enc_states = dynamic_rnn(enc_cells, source_emb, sequence_length=self.enc_seq_len,
                                                            dtype=tf.float32)
            print("encoder output shape: {}".format(self.enc_outputs.get_shape().as_list()))

        with tf.variable_scope("decoder"):
            self.max_dec_seq_len = tf.reduce_max(self.dec_seq_len, name="max_dec_seq_len")
            self.dec_cells, self.dec_init_states = self._create_decoder_cell()
            # define input and output projection layer
            input_project = tf.layers.Dense(units=self.cfg.num_units, name="input_projection")
            self.dense_layer = tf.layers.Dense(units=self.cfg.vocab_size, name="output_projection")
            if self.mode == "train":  # either "train" or "decode"
                # for training
                target_emb = input_project(target_emb)
                train_helper = TrainingHelper(target_emb, sequence_length=self.dec_seq_len, name="train_helper")
                train_decoder = BasicDecoder(self.dec_cells, helper=train_helper, output_layer=self.dense_layer,
                                             initial_state=self.dec_init_states)
                self.dec_output, _, _ = dynamic_decode(train_decoder, impute_finished=True,
                                                       maximum_iterations=self.max_dec_seq_len)
                print("decoder output shape: {} (vocab size)".format(self.dec_output.rnn_output.get_shape().as_list()))

                # for decode
                start_token = tf.ones(shape=[self.batch_size, ], dtype=tf.int32) * self.cfg.target_dict[GO]
                end_token = self.cfg.target_dict[EOS]

                def inputs_project(inputs):
                    return input_project(tf.nn.embedding_lookup(self.target_embs, inputs))

                dec_helper = GreedyEmbeddingHelper(embedding=inputs_project, start_tokens=start_token,
                                                   end_token=end_token)
                infer_decoder = BasicDecoder(self.dec_cells, helper=dec_helper, initial_state=self.dec_init_states,
                                             output_layer=self.dense_layer)
                infer_dec_output, _, _ = dynamic_decode(infer_decoder, maximum_iterations=self.cfg.maximum_iterations)
                self.dec_predicts = infer_dec_output.sample_id 
Example #15
Source File: attention_predictor.py    From aster with MIT License 4 votes vote down vote up
def predict(self, feature_maps, scope=None):
    if not isinstance(feature_maps, (list, tuple)):
      raise ValueError('`feature_maps` must be list of tuple')

    with tf.variable_scope(scope, 'Predict', feature_maps):
      batch_size = shape_utils.combined_static_and_dynamic_shape(feature_maps[0])[0]      
      decoder_cell = self._build_decoder_cell(feature_maps)
      decoder = self._build_decoder(decoder_cell, batch_size)

      outputs, _, output_lengths = seq2seq.dynamic_decode(
        decoder=decoder,
        output_time_major=False,
        impute_finished=False,
        maximum_iterations=self._max_num_steps
      )
      # apply regularizer
      filter_weights = lambda vars : [x for x in vars if x.op.name.endswith('kernel')]
      tf.contrib.layers.apply_regularization(
        self._rnn_regularizer,
        filter_weights(decoder_cell.trainable_weights))

      outputs_dict = None
      if self._is_training:
        assert isinstance(outputs, seq2seq.BasicDecoderOutput)
        outputs_dict = {
          'labels': outputs.sample_id,
          'logits': outputs.rnn_output,
        }
      else:
        assert isinstance(outputs, seq2seq.FinalBeamSearchDecoderOutput)
        prediction_labels = outputs.predicted_ids[:,:,0]
        prediction_lengths = output_lengths[:,0]
        prediction_scores = tf.gather_nd(
          outputs.beam_search_decoder_output.scores[:,:,0],
          tf.stack([tf.range(batch_size), prediction_lengths-1], axis=1)
        )
        outputs_dict = {
          'labels': prediction_labels,
          'scores': prediction_scores,
          'lengths': prediction_lengths
        }
    return outputs_dict