def evaluate_model()

in source/sagemaker/src/package/model/inference.py [0:0]


def evaluate_model(test_dataset, model_file, vocab_file, output_dir, embedding_size=300):
    TEXT, LABEL = create_fields()

    fields = [('sentiment', LABEL),
              ('title', None),
              ('review', TEXT)]

    test_reviews = TabularDataset(
        path=test_dataset, format='csv',
        fields=fields,
        skip_header=True)

    train_vocab = torch.load(vocab_file)

    TEXT.vocab = train_vocab
    LABEL.build_vocab(test_reviews)

    model = create_model(
        len(TEXT.vocab),
        embedding_size,
        TEXT.vocab.stoi[TEXT.pad_token],
        TEXT.vocab.stoi[TEXT.unk_token],
        torch.zeros((len(TEXT.vocab), embedding_size)))

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    model.load_state_dict(torch.load(model_file, map_location=device))

    model.to(device)

    batch_size = 64
    criterion = torch.nn.BCEWithLogitsLoss()
    test_iterator = BucketIterator(
        test_reviews, batch_size=batch_size, sort=False, device=device)

    test_loss, test_acc, fpr, tpr, prec, rec = calculate_metrics(model, test_iterator, criterion)

    print(f'Test Loss: {test_loss:.3f} | Test Acc: {test_acc * 100:.2f}%')

    with open(os.path.join(output_dir, "output.json"), 'w') as outfile:
        json.dump({"test_loss": test_loss, "test_accuracy": test_acc}, outfile)

    return test_loss, test_acc, fpr, tpr, prec, rec