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 remote.py 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))
Metadata
Release files for triplex 0.0.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| triplex-0.0.18.tar.gz | 38.7 kB | Without provenance | Details for triplex-0.0.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| triplex-0.0.18-py3-none-any.whl | Python 3 | none | any | Details for triplex-0.0.18-py3-none-any.whl |
Total release size: 79.0 kB
Release files / triplex-0.0.18.tar.gz
| Download URL | triplex-0.0.18.tar.gz |
|---|---|
| Size | 38.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
142d77c5f637f56665ec28d8d47ac8cba62320bf9a45054403a0e1f397ce88f9
|
|
BLAKE2b-256 checksum How to use checksums |
ca105b841e8c01bfb3d78cba067d49d05b94351641eeb7e9a2337a75cc493af2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / triplex-0.0.18-py3-none-any.whl
| Download URL | triplex-0.0.18-py3-none-any.whl |
|---|---|
| Size | 40.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c508e2408befd7d3488aeb983ccaffb035c5a7538feb6d3f47fe594098e41315
|
|
BLAKE2b-256 checksum How to use checksums |
04a67514d02911e817f5de96f9b544c932462d80a775e989a4ef8a65e8a7fa67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for triplex 0.0.18
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.