def test_save_load():
    sgnn_fit_data = "oiasdjf opiasd fjopias dfijop asdpoi fjakl;jswe fklzjsnd cvoiuasherf iuhans dfkjuans cvpiouha sdifjn asdkfjn"  # TODO: ???.
    preproc_sgnn_sklearn_pipeline = get_sgnn_projection_pipeline(
        sgnn_training_data=sgnn_fit_data)
    sentence_projection_model = make_sentence_model()
    test_model_name = MY_MODEL_NAME + ".__unit_test_delete_this_file__"
    success = True
    try:

        # Test save.
        save_model(preproc_sgnn_sklearn_pipeline, sentence_projection_model,
                   test_model_name)
        # Test load.
        preproc_sgnn_sklearn_pipeline, sentence_projection_model = load_model(
            test_model_name)
        preproc_sgnn_sklearn_pipeline2, sentence_projection_model2 = load_most_recent_model(
            test_model_name, None)

        assert isinstance(preproc_sgnn_sklearn_pipeline, BaseEstimator)
        assert isinstance(sentence_projection_model, nn.Module)
        assert isinstance(preproc_sgnn_sklearn_pipeline2, BaseEstimator)
        assert isinstance(sentence_projection_model2, nn.Module)
    except:
        traceback.print_exc()
        success = False
    finally:
        delete_model(test_model_name)
    assert success
示例#2
0
def test_model_works():
    some_random_d_model = 2 ** 9
    five_sentences_of_twenty_words = torch.from_numpy(np.random.random((5, 20, T * d))).float()
    five_sentences_of_twenty_words_mask = torch.from_numpy(np.ones((5, 1, 20))).float()
    pytorch_model = make_sentence_model(d_model=some_random_d_model, T_sgnn=T, d_sgnn=d)

    output_before_match = pytorch_model(five_sentences_of_twenty_words, five_sentences_of_twenty_words_mask)

    print(output_before_match.shape)
    assert output_before_match.shape == (5, some_random_d_model)
def test_trainer_model_works():
    raw_data = "This is a sentence. And this is another one. This test needs sentences as such"
    category_per_sentence = [0, 1, 3]
    preproc_sgnn_sklearn_pipeline = get_sgnn_projection_pipeline(sgnn_training_data=raw_data)
    sentence_projection_model = make_sentence_model(d_ff=1024)
    list_of_sentences = raw_data.split(". ")
    projected_words = preproc_sgnn_sklearn_pipeline.transform(list_of_sentences)
    projected_words, mask = pad_right(projected_words)
    target_diagonal_block_matrix = categories_to_block_matrix(category_per_sentence)

    model = TrainerModel(sentence_projection_model)

    loss = model(projected_words, mask, target_diagonal_block_matrix)
    assert type(loss.item()) == float
def test_trainer_model_works_on_gpu():
    device_id = 0
    torch.cuda.set_device(device_id)
    with torch.cuda.device(device_id) as cuda:
        raw_data = "This is a sentence. And this is another one. This test needs sentences as such"
        category_per_sentence = [0, 1, 3]
        preproc_sgnn_sklearn_pipeline = get_sgnn_projection_pipeline(
            sgnn_training_data=raw_data)
        sentence_projection_model = make_sentence_model(d_ff=1024).cuda(device_id)
        list_of_sentences = raw_data.split(". ")
        projected_words = preproc_sgnn_sklearn_pipeline.transform(list_of_sentences)
        projected_words, mask = pad_right(projected_words)
        projected_words, mask = projected_words.cuda(device_id), mask.cuda(device_id)
        target_diagonal_block_matrix = categories_to_block_matrix(category_per_sentence).cuda(device_id)

        model = TrainerModel(sentence_projection_model).cuda(device_id)

        loss = model(projected_words, mask, target_diagonal_block_matrix)
        assert loss.is_cuda
示例#5
0
def test_model_works_on_gpu():
    device_id = 0
    with torch.cuda.device(device_id) as cuda:
        some_random_d_model = 2 ** 9
        five_sentences_of_twenty_words = torch.from_numpy(np.random.random((5, 20, T * d))).float()
        five_sentences_of_twenty_words_mask = torch.from_numpy(np.ones((5, 1, 20))).float()
        pytorch_model = make_sentence_model(d_model=some_random_d_model, T_sgnn=T, d_sgnn=d)

        five_sentences_of_twenty_words = five_sentences_of_twenty_words.cuda(device_id)
        five_sentences_of_twenty_words_mask = five_sentences_of_twenty_words_mask.cuda(device_id)
        print(type(five_sentences_of_twenty_words), type(five_sentences_of_twenty_words_mask))
        print(five_sentences_of_twenty_words.is_cuda, five_sentences_of_twenty_words_mask.is_cuda)
        pytorch_model = pytorch_model.cuda(device_id)
        output_before_match = pytorch_model(five_sentences_of_twenty_words, five_sentences_of_twenty_words_mask)

        assert output_before_match.shape == (5, some_random_d_model)
        print(type(output_before_match))
        print(output_before_match.is_cuda, output_before_match.get_device())
        assert output_before_match.is_cuda
        assert five_sentences_of_twenty_words.is_cuda
        assert five_sentences_of_twenty_words_mask.is_cuda
示例#6
0
def test_can_create_model():
    pytorch_model = make_sentence_model()

    assert isinstance(pytorch_model, torch.nn.Module)