Example #1
0
def test_train_mlp_mock():
    directory = "/tmp"
    file_path = os.path.join(directory, const.MLPMODEL_FILE)

    mlp_clf = MLPMultiClass(
        model_dir=directory,
        dest="output.intents",
        debug=False,
    )

    train_df = pd.DataFrame([
        {
            "data": "yes",
            "labels": "_confirm_"
        },
        {
            "data": "yea",
            "labels": "_confirm_"
        },
        {
            "data": "no",
            "labels": "_cancel_"
        },
        {
            "data": "nope",
            "labels": "_cancel_"
        },
    ])

    mlp_clf.train(train_df)
    assert mlp_clf.labels_num == 2

    # This copy loads from the same directory that was trained previously.
    # So this instance would have read the saved mlp model.
    mlp_clf_copy = MLPMultiClass(
        model_dir=directory,
        dest="output.intents",
        debug=False,
    )
    mlp_clf_copy.load()
    assert isinstance(mlp_clf_copy.model_pipeline, sklearn.pipeline.Pipeline)
    assert mlp_clf_copy.valid_mlpmodel is True
    os.remove(file_path)
Example #2
0
def test_train_mlp_gridsearch_mock():
    directory = "/tmp"
    file_path = os.path.join(directory, const.MLPMODEL_FILE)
    USE = "use"
    fake_args = {
        const.TRAIN: {
            const.NUM_TRAIN_EPOCHS: 10,
            const.USE_GRIDSEARCH: {
                USE: True,
                const.CV: 2,
                const.VERBOSE_LEVEL: 2,
                const.PARAMS: {
                    "activation": ["relu", "tanh"],
                    "hidden_layer_sizes": [(1, ), (2, 1)],
                    "ngram_range": [(1, 1)],
                    "max_iter": [1, 2],
                },
            },
        },
        const.TEST: {},
        const.PRODUCTION: {},
    }

    mlp_clf = MLPMultiClass(
        model_dir=directory,
        dest="output.intents",
        debug=False,
        args_map=fake_args,
    )

    train_df = pd.DataFrame([
        {
            "data": "yes",
            "labels": "_confirm_"
        },
        {
            "data": "yea",
            "labels": "_confirm_"
        },
        {
            "data": "no",
            "labels": "_cancel_"
        },
        {
            "data": "nope",
            "labels": "_cancel_"
        },
    ])

    mlp_clf.train(train_df)
    assert mlp_clf.labels_num == 2

    # This copy loads from the same directory that was trained previously.
    # So this instance would have read the saved mlp model.
    mlp_clf_copy = MLPMultiClass(
        model_dir=directory,
        dest="output.intents",
        debug=False,
    )
    mlp_clf_copy.load()
    assert mlp_clf_copy.valid_mlpmodel is True
    mlp_clf_copy.train(
        train_df)  # When trying to train an already trained model
    os.remove(file_path)