def test_rnn_explain_local(self):
        train_data = get_ssts_dataset('train')
        test_data = get_ssts_dataset('test')
        all_data = pd.concat([train_data, test_data])
        X_train = train_data[TEXT_COL]
        X_test = test_data[TEXT_COL]
        preprocessor = GlovePreprocessor(count_threshold=TOKEN_COUNT_THRESHOLD,
                                         token_cutoff=MAX_SENT_COUNT)
        preprocessor.build_vocab(all_data[TEXT_COL])

        df_train = pd.concat(
            [train_data[LABEL_COL],
             preprocessor.preprocess(X_train)], axis=1)
        df_test = pd.concat(
            [test_data[LABEL_COL],
             preprocessor.preprocess(X_test)], axis=1)
        model_config = RNN_MODEL_CONFIG
        explainer = IntrospectiveRationaleExplainer(
            classifier_type=CLASSIFIER_TYPE_RNN, cuda=CUDA)
        explainer.build_model_config(model_config)
        explainer.set_preprocessor(preprocessor)
        explainer.load()
        explainer.fit(df_train, df_test)

        local_explanation = explainer.explain_local(SENTENCE)
        assert len(local_explanation.local_importance_values) == len(
            SENTENCE.split())
Esempio n. 2
0
def setup_mock_rnn_introspective_rationale_explainer(model_config, data):
    explainer = IntrospectiveRationaleExplainer(classifier_type=CLASSIFIER_TYPE_RNN, cuda=CUDA)
    preprocessor = GlovePreprocessor(TOKEN_COUNT_THRESHOLD, MAX_SENT_COUNT)
    preprocessor.build_vocab(data)
    explainer.set_preprocessor(preprocessor)
    explainer.build_model_config(model_config)
    return explainer
 def test_set_custom_classifier(self):
     all_data = get_ssts_dataset('train')
     model_config = RNN_MODEL_CONFIG
     preprocessor = GlovePreprocessor(TOKEN_COUNT_THRESHOLD, MAX_SENT_COUNT)
     preprocessor.build_vocab(all_data[TEXT_COL])
     explainer = IntrospectiveRationaleExplainer(
         classifier_type=CLASSIFIER_TYPE_CUSTOM, cuda=CUDA)
     explainer.build_model_config(model_config)
     # set custom classifier
     classifier = ClassifierModule(explainer.get_model_config(),
                                   preprocessor.word_vocab)
     explainer.set_preprocessor(preprocessor)
     explainer.set_classifier(classifier)
     assert explainer.classifier_type is CLASSIFIER_TYPE_CUSTOM