コード例 #1
0
    def _compute_next_token_score_batch(self, token_idx, condition_id):
        # Get prediction of the model - p(T|S)
        current_token_id_for_each_candidate = self._cur_candidates[:, token_idx - 1]
        self._hidden_states_batch, next_token_score_batch = \
            get_next_token_log_prob_one_step(self._nn_model, self._thought_batch, self._hidden_states_batch,
                                             current_token_id_for_each_candidate, condition_id)
        # Candidates[:, :token_idx], as usual, means "All tokens preceding token_idx for each candidate"
        # We use all preceding tokens to compute counts and penalize current distribution using these counts.
        next_token_score_batch = self._penalize_by_repetition(next_token_score_batch,
                                                              self._cur_candidates[:, :token_idx])

        # next_token_score_batch here is (num_candidates x vocab_size). For each candidate we find all the
        # banned tokens and kill their scores to -inf.
        next_token_score_batch[:, self._service_tokens_ids.banned_tokens_ids] = -np.inf
        return next_token_score_batch
コード例 #2
0
ファイル: beamsearch.py プロジェクト: Allensmile/cakechat
    def _compute_next_token_score_batch(self, token_idx, condition_id):
        # Get prediction of the model - p(T|S)
        current_token_id_for_each_candidate = self._cur_candidates[:, token_idx - 1]
        self._hidden_states_batch, next_token_score_batch = \
            get_next_token_log_prob_one_step(self._nn_model, self._thought_batch, self._hidden_states_batch,
                                             current_token_id_for_each_candidate, condition_id)
        # Candidates[:, :token_idx], as usual, means "All tokens preceding token_idx for each candidate"
        # We use all preceding tokens to compute counts and penalize current distribution using these counts.
        next_token_score_batch = self._penalize_by_repetition(next_token_score_batch,
                                                              self._cur_candidates[:, :token_idx])

        # next_token_score_batch here is (num_candidates x vocab_size). For each candidate we find all the
        # banned tokens and kill their scores to -inf.
        next_token_score_batch[:, self._service_tokens_ids.banned_tokens_ids] = -np.inf
        return next_token_score_batch
コード例 #3
0
ファイル: predict.py プロジェクト: zhengjunzhao1991/cakechat
    def _predict_log_probabilities_one_step(nn_model, x_batch, y_batch):
        """
        Predict answers for every sequence token by token until EOS_TOKEN occurred in the sequence using sampling with temperature.
        All the rest of the sequence is filled with PAD_TOKENs.
        """
        thought_vectors_batch = nn_model.get_thought_vectors(x_batch)
        hidden_states_batch = np.zeros((x_batch.shape[0], DECODER_DEPTH, HIDDEN_LAYER_DIMENSION), dtype=np.float32)

        total_log_probs = np.zeros((y_batch.shape[0], y_batch.shape[1] - 1, nn_model.vocab_size))
        for token_idx in xrange(1, y_batch.shape[1]):
            hidden_states_batch, next_token_log_probs_batch = \
                get_next_token_log_prob_one_step(nn_model, thought_vectors_batch, hidden_states_batch,
                                                 y_batch[:, token_idx - 1], condition_ids=None)
            # total_log_probs has shape (batch_size x num_tokens x vocab_size)
            total_log_probs[:, token_idx - 1, :] = next_token_log_probs_batch

        return total_log_probs