import tensorflow as tf

                    wordidx_t = tf.multinomial(log_score_t, 1) # [batch_size, 1]
                    wordidx_t = tf.reshape(wordidx_t, [-1]) # [batch_size]
                elif mode_gen in ('ce_train', 'loss',):
                    wordidx_t = answer_batch_unstack[i]
                else:
                    assert False, 'unknown generating mode %s' % mode_gen
                sampled_words.append(wordidx_t)

        if len(sampled_words)!=0:
            sampled_words = tf.stack(sampled_words, axis=1) # [batch_size, max_dec_steps]

        vocab_scores = tf.stack(vocab_scores, axis=1) # [batch_size, max_dec_steps, vocab]
        # calculating loss
        self._loss = None
        if mode_gen in ('ce_train', 'loss', ):
            xent = CE_loss(vocab_scores, answer_batch, loss_weights) # [batch_size]
            if mode_gen == 'loss': xent *= self.placeholders.reward # multiply with rewards
            self._loss = tf.reduce_mean(xent)
