def save_model(self, save_path): ensure_dir(os.path.dirname(save_path)) all_params = get_all_params(self._net['dist']) with open(save_path, 'wb') as f: params = {v.name: v.get_value() for v in all_params} np.savez(f, **params) _logger.info('\nSaved model:\n{}\n'.format(save_path))
def save_model(self, save_model_path): ensure_dir(os.path.dirname(save_model_path)) ordered_params = get_all_param_values(self._net['dist']) with open(save_model_path, 'wb') as f: np.savez(f, *ordered_params) _logger.info('\nSaved model:\n{}\n'.format(save_model_path))
def download(self, remote_file_name, local_file_name): """ Download file from AWS S3 to the local one """ # create dir if not exists for storing file from s3 ensure_dir(os.path.dirname(local_file_name)) self._logger.info('Getting file %s from AWS S3 and saving it as %s' % (remote_file_name, local_file_name)) self._bucket_client.download_file(remote_file_name, local_file_name) self._logger.info('Got file %s from S3' % remote_file_name)
def download(self, remote_file_name, local_file_name): """ Download file from AWS S3 to the local one """ remote_file_name = os.path.normpath(remote_file_name) # create dir if not exists for storing file from s3 ensure_dir(os.path.dirname(local_file_name)) self._logger.info('Getting file %s from AWS S3 and saving it as %s' % (remote_file_name, local_file_name)) self._bucket_client.download_file(remote_file_name, local_file_name) self._logger.info('Got file %s from S3' % remote_file_name)
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_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 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 dump_index_to_item(index_to_item, path): ensure_dir(os.path.dirname(path)) with codecs.open(path, 'w', 'utf-8') as fh: json.dump(index_to_item, fh, ensure_ascii=False)
def save_weights(self, save_path): ensure_dir(os.path.dirname(save_path)) ordered_params = get_all_param_values(self._net['dist']) with open(save_path, 'wb') as f: np.savez(f, *ordered_params)
def _save_model(model, model_path): _logger.info('Saving model to {}'.format(model_path)) ensure_dir(os.path.dirname(model_path)) model.save(model_path, separately=[]) _logger.info('Model has been saved')
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 _save_model(model, model_path): _logger.info('Saving model to %s' % model_path) ensure_dir(os.path.dirname(model_path)) model.save(model_path, separately=[]) _logger.info('Model has been saved')