<jupyter_start><jupyter_text>IntroductionIn this notebook we will see how one can use LEMON to explain entity matching model predictions.To ease quick experimentation the LEMON library also contains some convenient functionally for fetching common datasets and using some common matchers in `lemon.utils`.  Dataset<jupyter_code># Will download and load the Abt-Buy dataset
dataset = lemon.utils.datasets.deepmatcher.textual_abt_buy()<jupyter_output><empty_output><jupyter_text>Explain Magellan Model  Train<jupyter_code>matcher = lemon.utils.matchers.MagellanMatcher()
matcher.fit(
    dataset.train.records.a,
    dataset.train.records.b,
    dataset.train.record_id_pairs,
    dataset.train.labels,
)<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code>matcher.evaluate(
    dataset.test.records.a,
    dataset.test.records.b,
    dataset.test.record_id_pairs,
    dataset.test.labels,
)<jupyter_output><empty_output><jupyter_text>Explain<jupyter_code>exp = lemon.explain(
    dataset.records.a,
    dataset.records.b,
    dataset.test.record_id_pairs.iloc[0:1],
    matcher.predict_proba,
)
exp
# Explanations can optionally be saved and loaded
exp.save("exp.json")
lemon.MatchingAttributionExplanation.load("exp.json")<jupyter_output><empty_output><jupyter_text>Explain Transformer ModelYou are recommended to have a GPU to run this section  Train<jupyter_code>import transformers
transformers.logging.disable_default_handler() # Silence excessive logging

matcher = lemon.utils.matchers.TransformerMatcher(
    "google/bert_uncased_L-4_H-256_A-4",
    tokenizer_args={"model_max_length": 256}
)
matcher.fit(
    dataset.train.records.a,
    dataset.train.records.b,
    dataset.train.record_id_pairs,
    dataset.train.labels,
    dataset.val.record_id_pairs,
    dataset.val.labels,
)<jupyter_output>Some weights of the model checkpoint at google/bert_uncased_L-4_H-256_A-4 were not used when initializing BertForSequenceClassification: ['cls.predictions.bias', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.decoder.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.dense.weight', 'cls.predictions.decoder.weight', 'cls.predictions.transform.dense.bias', 'cls.seq_relationship.weight']
- This IS expected if you are initializing BertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of BertForSequenceClassification w[...]<jupyter_text>Evaluate<jupyter_code>matcher.evaluate(
    dataset.test.records.a,
    dataset.test.records.b,
    dataset.test.record_id_pairs,
    dataset.test.labels,
)<jupyter_output><empty_output><jupyter_text>Explain<jupyter_code>lemon.explain(
    dataset.records.a,
    dataset.records.b,
    dataset.test.record_id_pairs.iloc[0:1],
    matcher.predict_proba,
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09342023587833644, 'fraction_numerical': 0.005276225946617008, 'mean_word_length': 5.916309012875536, 'pattern_counts': {'":': 1, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '415010', 'license': 'MIT', 'n_tokens_mistral': 1040, 'n_tokens_neox': 1008, 'n_words': 208, 'path': 'Example.ipynb', 'repo_name': 'NilsBarlaug/lemon', 'size': '3222'}