Example #1
0
    if step != 0 and step % args.training_steps == 0:
        writer.add_scalar('Loss/train', running_loss / 100, step)
        print(f"Training Loss: {running_loss/100}")
        print("Getting Final Evaluation Results")
        print("--------------------")
        sorted_preds, indices = eval_util.calculate_predictions(
            eval_loader,
            model,
            device,
            args.target_publication,
            version="Evaluation",
            step=step,
            writer=writer,
            check_recall=True)
        ranked_df = eval_util.create_ranked_results_list(
            final_word_ids, args.word_embedding_type, sorted_preds, indices,
            eval_data)
        eval_util.save_ranked_df(output_path, "evaluation", ranked_df,
                                 args.word_embedding_type)
        print(
            f"Ranked Data Saved to {output_path / 'results' / 'evaluation'}!")
        check = False
        break

# save model for easy future reloading
model_path = output_path / "model"
if not model_path.is_dir():
    model_path.mkdir()
model_string = args.word_embedding_type + "-inner-product-model.pt"
model_path = model_path / model_string
torch.save(model.state_dict(), model_path)
abs_model_path = Path(args.model_path)
config_file = abs_model_path.parent / "config.json"
config = BertConfig.from_json_file(config_file)
model = BertForSequenceClassification(config)
model.load_state_dict(torch.load(abs_model_path))
model.to(device)
model.eval()
torch.no_grad()
print("Model Loaded")
print(model)
print("-------------------")

data_logit_list = []
for batch in tqdm(raw_loader):
    current_logits = eval_util.calculate_batched_predictions(
        batch, model, device, args.target_publication)
    data_logit_list = data_logit_list + list(current_logits)
converted_list = np.array(data_logit_list)
sorted_preds = np.sort(converted_list)
indices = np.argsort(converted_list)

ranked_df = eval_util.create_ranked_results_list(final_word_ids, sorted_preds,
                                                 indices, raw_data)
eval_util.save_ranked_df(output_path, args.data_name, ranked_df)

print("Predictions Made")
print(
    f"Ranked Data Saved to {output_path / 'results' / 'evaluation'} directory!"
)