def predict_for_condition_id(nn_model, contexts, condition_id, prediction_mode=PREDICTION_MODE_FOR_TESTS): condition_ids = np.array([condition_id] * contexts.shape[0], dtype=INTX) responses = get_nn_responses(contexts, nn_model, mode=prediction_mode, output_candidates_num=1, condition_ids=condition_ids) return [candidates[0] for candidates in responses]
def _get_non_offensive_response_using_fast_sampling(context_tokens_ids, condition_id): for _ in range(SAMPLING_ATTEMPTS_NUM): response = get_nn_responses(context_tokens_ids, _cakechat_model, PREDICTION_MODES.sampling, condition_ids=condition_id)[0][0] if _is_appropriate_response(response): return prettify_response(response) return DEFAULT_RESPONSE
def _get_non_offensive_response_using_fast_sampling(context_tokens_ids, condition_id): for _ in xrange(SAMPLING_ATTEMPTS_NUM): response = get_nn_responses(context_tokens_ids, _cakechat_model, PREDICTION_MODES.sampling, condition_ids=condition_id)[0][0] tokenized_response = get_tokens_sequence(response) if not _offense_detector.has_offensive_ngrams(tokenized_response): return get_pretty_str_from_tokens_sequence(tokenized_response) return DEFAULT_RESPONSE
def _log_sample_answers(x_test, nn_model, mode, is_reverse_model): _logger.info('Model: {}'.format(get_model_full_path(is_reverse_model))) _logger.info('Start predicting responses of length {out_len} for {n_samples} samples with mode {mode}'.format( out_len=MAX_PREDICTIONS_LENGTH, n_samples=x_test.shape[0], mode=mode)) questions = transform_context_token_ids_to_sentences(x_test, nn_model.index_to_token) responses = get_nn_responses(x_test, nn_model, mode, output_candidates_num=LOG_CANDIDATES_NUM) _logger.info('Finished predicting! Logging...') for i, (question_ids, question) in enumerate(zip(x_test, questions)): laconic_logger.info('') # for better readability for j, response in enumerate(responses[i]): laconic_logger.info('%-35s\t --#=%02d--> \t%s' % (question, j + 1, response))
def _get_non_offensive_response(context_tokens_ids, condition_id): responses = get_nn_responses( context_tokens_ids, _cakechat_model, PREDICTION_MODE, output_candidates_num=NUM_BEST_CANDIDATES_TO_PICK_FROM, condition_ids=condition_id)[0] responses = list(filter(_is_appropriate_response, responses)) if responses: selected_response = random.choice(responses) return prettify_response(selected_response) return DEFAULT_RESPONSE
def log_predictions(predictions_path, x_test, nn_model, mode, candidates_num=None, stats_info=None, cur_perplexity=None, output_seq_len=MAX_PREDICTIONS_LENGTH, **kwargs): _logger.info('Logging responses to test lines') # Create all the directories for the prediction path in case they don't exist prediction_dir = os.path.dirname(predictions_path) if prediction_dir: ensure_dir(prediction_dir) with open(predictions_path, 'w') as test_res_fh: csv_writer = init_csv_writer(test_res_fh, mode, output_seq_len) if cur_perplexity: csv_writer.writerow(['Current perplexity: %.2f' % cur_perplexity]) if stats_info: csv_writer.writerow([_get_iteration_stats(stats_info)]) csv_writer.writerow( ['input sentence'] + ['candidate #{}'.format(v + 1) for v in xrange(candidates_num)]) questions = transform_context_token_ids_to_sentences( x_test, nn_model.index_to_token) _logger.info( 'Start predicting responses of length {out_len} for {n_samples} samples with mode {mode}' .format(out_len=output_seq_len, n_samples=x_test.shape[0], mode=mode)) nn_responses = get_nn_responses(x_test, nn_model, mode, candidates_num, output_seq_len, **kwargs) _logger.info('Logging generated predictions...') for idx, (question, responses) in enumerate(zip(questions, nn_responses)): csv_writer.writerow([question] + responses) _logger.info( 'Succesfully dumped {n_resp} of {n_resp} responses'.format( n_resp=len(questions))) _logger.info('Here they are: {}'.format(predictions_path))
def _log_sample_answers(self): self._logger.info('Sample responses for {} mode:'.format(self._prediction_mode_for_tests)) responses = get_nn_responses( self._val_contexts_tokens_ids, self._model, self._prediction_mode_for_tests, output_candidates_num=self._log_candidates_num) for context, response_candidates in zip(self._val_contexts, responses): laconic_logger.info('') # for better readability for i, response in enumerate(response_candidates): laconic_logger.info('{0: <35}\t #{1: <2d} --> \t{2}'.format(context, i + 1, response)) laconic_logger.info('') # for better readability
def log_predictions(predictions_path, contexts_token_ids, nn_model, prediction_modes, output_seq_len=MAX_PREDICTIONS_LENGTH, **kwargs): """ Generate responses for provided contexts and save the results on the disk. For a given context several responses will be generated - one for each mode from the prediction_modes list. :param predictions_path: Generated responses will be saved to this file :param contexts_token_ids: contexts token ids, numpy array of shape (batch_size, context_len, INPUT_SEQUENCE_LENGTH) :param nn_model: instance of CakeChatModel class :param prediction_modes: See PREDICTION_MODES for available options :param output_seq_len: Max number of tokens in generated responses """ _logger.info('Logging responses for test set') # Create all the directories for the prediction path in case they don't exist ensure_dir(os.path.dirname(predictions_path)) _init_csv_writer(predictions_path, output_seq_len, nn_model.model_name) contexts = transform_context_token_ids_to_sentences( contexts_token_ids, nn_model.index_to_token) predictions_data = pd.DataFrame() predictions_data['contexts'] = contexts for prediction_mode in prediction_modes: predicted_responses = get_nn_responses(contexts_token_ids, nn_model, prediction_mode, **kwargs) # list of lists of strings, shape (contexts_num, 1) predicted_responses = [response[0] for response in predicted_responses] # list of strings, shape (contexts_num) predictions_data[prediction_mode] = predicted_responses predictions_data.to_csv(predictions_path, sep='\t', index=False, encoding='utf-8', mode='a', float_format='%.2f') _logger.info('Dumped {} predicted responses to {}'.format( len(contexts), predictions_path))
def _get_non_offensive_response(context_tokens_ids, condition_id): responses = get_nn_responses( context_tokens_ids, _cakechat_model, PREDICTION_MODE, output_candidates_num=NUM_BEST_CANDIDATES_TO_PICK_FROM, condition_ids=condition_id)[0] tokenized_responses = [ get_tokens_sequence(response) for response in responses ] non_offensive_tokenized_responses = [ r for r in tokenized_responses if not _offense_detector.has_offensive_ngrams(r) ] if non_offensive_tokenized_responses: tokenized_response = random.choice(non_offensive_tokenized_responses) return get_pretty_str_from_tokens_sequence(tokenized_response) return DEFAULT_RESPONSE
def log_predictions(predictions_path, x_test, nn_model, mode, candidates_num=None, stats_info=None, cur_perplexity=None, output_seq_len=MAX_PREDICTIONS_LENGTH, **kwargs): _logger.info('Logging responses to test lines') # Create all the directories for the prediction path in case they don't exist prediction_dir = os.path.dirname(predictions_path) if prediction_dir: ensure_dir(prediction_dir) with open(predictions_path, 'w') as test_res_fh: csv_writer = init_csv_writer(test_res_fh, mode, output_seq_len) if cur_perplexity: csv_writer.writerow(['Current perplexity: %.2f' % cur_perplexity]) if stats_info: csv_writer.writerow([_get_iteration_stats(stats_info)]) csv_writer.writerow(['input sentence'] + ['candidate #{}'.format(v + 1) for v in xrange(candidates_num)]) questions = transform_context_token_ids_to_sentences(x_test, nn_model.index_to_token) _logger.info('Start predicting responses of length {out_len} for {n_samples} samples with mode {mode}'.format( out_len=output_seq_len, n_samples=x_test.shape[0], mode=mode)) nn_responses = get_nn_responses(x_test, nn_model, mode, candidates_num, output_seq_len, **kwargs) _logger.info('Logging generated predictions...') for idx, (question, responses) in enumerate(zip(questions, nn_responses)): csv_writer.writerow([question] + responses) _logger.info('Succesfully dumped {n_resp} of {n_resp} responses'.format(n_resp=len(questions))) _logger.info('Here they are: {}'.format(predictions_path))
def predict_for_condition_id(nn_model, x_val, condition_id=None): responses = get_nn_responses(x_val, nn_model, mode=PREDICTION_MODE_FOR_TESTS, condition_ids=condition_id) return [candidates[0] for candidates in responses]
def log_predictions(predictions_path, x_test, nn_model, prediction_modes=(PREDICTION_MODE_FOR_TESTS, ), stats_info=None, cur_perplexity=None, output_seq_len=MAX_PREDICTIONS_LENGTH, **kwargs): """ Generate responses for provided contexts and save the results on the disk. For a given context several responses will be generated - one for each mode from the prediction_modes list. :param predictions_path: Generated responses will be saved to this file :param x_test: context token ids, numpy array of shape (batch_size, context_len, INPUT_SEQUENCE_LENGTH) :param nn_model: instance of CakeChatModel class :param prediction_modes: Iterable of modes to be used for responses generation. See PREDICTION_MODES for available options :param stats_info: Info about current training status: total time passed, time spent on training, processed batches number and estimated time for one epoch :param cur_perplexity: Addition to stats_info - current perplexity metric calculated on validation dataset :param output_seq_len: Max number of tokens in generated responses """ _logger.info('Logging responses to test lines') # Create all the directories for the prediction path in case they don't exist prediction_dir = os.path.dirname(predictions_path) if prediction_dir: ensure_dir(prediction_dir) with open(predictions_path, 'w') as test_res_fh: csv_writer = init_csv_writer(test_res_fh, output_seq_len, nn_model.model_name) if cur_perplexity: csv_writer.writerow(['Current perplexity: %.2f' % cur_perplexity]) if stats_info: csv_writer.writerow([_get_iteration_stats(stats_info)]) contexts = transform_context_token_ids_to_sentences( x_test, nn_model.index_to_token) predictions_data = pd.DataFrame() predictions_data['contexts'] = contexts for pred_mode in prediction_modes: responses_batch = get_nn_responses(x_test, nn_model, pred_mode, **kwargs) # list of lists of strings, shape (contexts_num, 1) first_responses_batch = [response[0] for response in responses_batch] # list of strings, shape (contexts_num) predictions_data[pred_mode] = first_responses_batch predictions_data.to_csv(predictions_path, sep='\t', index=False, encoding='utf-8', mode='a', float_format='%.2f') message = '\nSuccesfully dumped {} responses.'.format(len(contexts)) message += '\nHere they are:\n{}\n'.format(predictions_path) _logger.info(message)
def predict_for_condition_id(nn_model, contexts, condition_id, prediction_mode=PREDICTION_MODE_FOR_TESTS): condition_ids = np.array([condition_id] * contexts.shape[0], dtype=np.int32) responses = get_nn_responses( contexts, nn_model, mode=prediction_mode, output_candidates_num=1, condition_ids=condition_ids) return [candidates[0] for candidates in responses]