commit files to HF hub
Browse files- config.json +9 -0
- integrated_gradients.py +81 -0
- model.safetensors +1 -1
config.json
CHANGED
|
@@ -5,6 +5,15 @@
|
|
| 5 |
"DistilBertForSequenceClassification"
|
| 6 |
],
|
| 7 |
"attention_dropout": 0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"dim": 768,
|
| 9 |
"dropout": 0.1,
|
| 10 |
"hidden_dim": 3072,
|
|
|
|
| 5 |
"DistilBertForSequenceClassification"
|
| 6 |
],
|
| 7 |
"attention_dropout": 0.1,
|
| 8 |
+
"custom_pipelines": {
|
| 9 |
+
"integrated-gradients": {
|
| 10 |
+
"impl": "integrated_gradients.IntegratedGradients",
|
| 11 |
+
"pt": [
|
| 12 |
+
"AutoModelForSequenceClassification"
|
| 13 |
+
],
|
| 14 |
+
"tf": []
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
"dim": 768,
|
| 18 |
"dropout": 0.1,
|
| 19 |
"hidden_dim": 3072,
|
integrated_gradients.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
from transformers import (
|
| 3 |
+
Pipeline,
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
AutoModelForSequenceClassification
|
| 6 |
+
)
|
| 7 |
+
import torch
|
| 8 |
+
import copy
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class IntegratedGradients(Pipeline):
|
| 13 |
+
def _sanitize_parameters(self, **kwargs):
|
| 14 |
+
forward_params = {}
|
| 15 |
+
if "baseline" in kwargs:
|
| 16 |
+
forward_params["baseline"] = kwargs["baseline"]
|
| 17 |
+
if "num_steps" in kwargs:
|
| 18 |
+
forward_params["num_steps"] = kwargs["num_steps"]
|
| 19 |
+
return {}, forward_params, {}
|
| 20 |
+
|
| 21 |
+
def preprocess(self, text):
|
| 22 |
+
return self.tokenizer(text, return_tensors=self.framework)
|
| 23 |
+
|
| 24 |
+
def _forward(
|
| 25 |
+
self,
|
| 26 |
+
model_inputs,
|
| 27 |
+
baseline,
|
| 28 |
+
num_steps
|
| 29 |
+
):
|
| 30 |
+
torch.set_grad_enabled(True)
|
| 31 |
+
input_embed = self.model.base_model.embeddings.word_embeddings(model_inputs['input_ids'])
|
| 32 |
+
copy_embed = copy.deepcopy(input_embed.data)
|
| 33 |
+
|
| 34 |
+
if baseline is None:
|
| 35 |
+
# create baseline
|
| 36 |
+
baseline = torch.zeros_like(copy_embed)
|
| 37 |
+
|
| 38 |
+
grads = []
|
| 39 |
+
|
| 40 |
+
for step in range(num_steps + 1):
|
| 41 |
+
print(f"step: {step}/{num_steps}")
|
| 42 |
+
input_embed.data = baseline + step/num_steps * (copy_embed - baseline)
|
| 43 |
+
torch.set_grad_enabled(True)
|
| 44 |
+
input_embed = self.model.base_model.embeddings.word_embeddings(model_inputs['input_ids'])
|
| 45 |
+
copy_embed = copy.deepcopy(input_embed.data)
|
| 46 |
+
|
| 47 |
+
if baseline is None:
|
| 48 |
+
# create baseline
|
| 49 |
+
baseline = torch.zeros_like(copy_embed)
|
| 50 |
+
|
| 51 |
+
grads = []
|
| 52 |
+
|
| 53 |
+
for step in range(num_steps + 1):
|
| 54 |
+
print(f"step: {step}/{num_steps}")
|
| 55 |
+
input_embed.data = baseline + step/num_steps * (copy_embed - baseline)
|
| 56 |
+
print(input_embed.data)
|
| 57 |
+
outputs = self.model(**model_inputs, output_hidden_states=True, output_attentions=True)
|
| 58 |
+
logits, hidden_states = outputs.logits, outputs.hidden_states
|
| 59 |
+
|
| 60 |
+
# calculate the derivates of the output embeddings
|
| 61 |
+
out_embed = hidden_states[0]
|
| 62 |
+
g = torch.autograd.grad(logits, out_embed, grad_outputs=torch.ones_like(logits))[0]
|
| 63 |
+
grads.append(g)
|
| 64 |
+
|
| 65 |
+
# stack grads along first dimension to create a new tensor
|
| 66 |
+
grads = torch.stack(grads)
|
| 67 |
+
|
| 68 |
+
# approx integral
|
| 69 |
+
grads = (grads[:-1] + grads[1:]) / 2
|
| 70 |
+
avg_grad = grads.mean(0)
|
| 71 |
+
|
| 72 |
+
integrated_grads = out_embed * avg_grad
|
| 73 |
+
|
| 74 |
+
return integrated_grads
|
| 75 |
+
|
| 76 |
+
def postprocess(self, integrated_grads):
|
| 77 |
+
scores = torch.sqrt((integrated_grads ** 2).sum(-1))
|
| 78 |
+
# normalize scores
|
| 79 |
+
max_s, min_s = scores.max(1, True).values, scores.min(1, True).values
|
| 80 |
+
normalized_scores = (scores - min_s) / (max_s - min_s)
|
| 81 |
+
return normalized_scores[0].tolist()
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 267832560
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:02a8411042d3ebcd765ee5ec4879eab02276f5747465af5ff2257d2fc492e2d1
|
| 3 |
size 267832560
|