Skip to main content

Explaining models, with Triples.

Project description

TripleX

Explaining models, with triples

Triplex is a local explainability method to explain transformer models by creating small knowledge graphs in the form of triplets. This implementation focuses on explaining predictions on NLI (natural language inference) tasks. Explanations are provided as dfas.DFAH (Deterministic Finite state Automata of Hypernyms).

import pathlib
import copy
import json

from dfas import DFAH

# base path
BASE_PATH = str(pathlib.Path().absolute()) + '/'
# Load a sample DFAH
dfah = DFAH.from_json(BASE_PATH + 'data/dummies/dfah.json')
# Show a DFAH visually
print(dfah)
# access the perturbations it went through
perturbations = dfah.perturbations

# dfah are copy-able and serializable
copy_dfah = copy.copy(dfah)
with open('data/dummies/my_dfah.json') as log:
    json.dump(dfah.to_json(), log)

Getting started

Install dependencies:

pip install triplex

python -m spacy download en_core_web_sm

Run

from transformers import AutoModel
import logzero

from triplex.triplex import TripleX

# logging level, set to logging.DEBUG for verbose output
logzero.loglevel(logzero.logging.INFO)

model = 'microsoft/deberta-base'
model = AutoModel.from_pretrained(model, output_attentions=True)
# create explainer
explainer = TripleX(model)

premise = 'Dana Reeve, the widow of the actor Christopher Reeve, has died of lung cancer at age 44, according to the Christopher Reeve Foundation.'
hypothesis = 'Christopher Reeve had an accident.'
dfas, counterfactual_dfas = explainer.extract(premise, hypothesis,
                                              depth=2,
                                              max_perturbations_per_token=3)
print('--- Explanations')
for d in dfas[:3]:
    print(str(d))
for d in counterfactual_dfas[:3]:
    print(str(d))

To run on a local JSONL dataset:

from transformers import AutoModel
import pandas as pd

from scripts.extract_from_dataset import to_standard_labels
from triplex.triplex import TripleX

dataset = 'path/to/dataset.jsonl'
data = pd.read_json(dataset, lines=True)
data = data.drop('idx', axis='columns')
data['label'] = to_standard_labels(data['label'].values, dataset)
data = data[['premise', 'hypothesis', 'label']]

model = AutoModel.from_pretrained('microsoft/deberta-base', output_attentions=True)
explainer = TripleX(model)
explanations = list()
for idx, row in data.iterrows():
    premise, hypothesis, label = row.premise, row.hypothesis, row.label
    dfas, counterfactual_dfas = explainer.extract(premise, hypothesis,
                                                  depth=2,
                                                  max_perturbations_per_token=3)
    explanations.append((premise, hypothesis, label, dfas, counterfactual_dfas))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

triplex-0.0.12.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

triplex-0.0.12-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file triplex-0.0.12.tar.gz.

File metadata

  • Download URL: triplex-0.0.12.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for triplex-0.0.12.tar.gz
Algorithm Hash digest
SHA256 1659aae64716c517d32fd5c2fcd690cfcf30951e7ee6058542835e88b21043ef
MD5 97fca3ad0b19958a87dea1c951746778
BLAKE2b-256 34bb4218796df2f07d8256fd34bcf0a27e3d2adf3d8d0d159ba46b6ad14bdded

See more details on using hashes here.

File details

Details for the file triplex-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: triplex-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 33.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for triplex-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4b1545d1003b3a5bd7c8372f5ea13c415a89fba85026345e90c6bb530ee5dbf4
MD5 2f4d828f86e461139297e0e7eb45d452
BLAKE2b-256 5b1372dc2728fb7b221fd035facd7e43fa09e945e1b1a855b69ad21043c41c62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page