Но мой код не запускается. Вот что у меня есть:
Код: Выделить всё
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from captum.attr import IntegratedGradients
device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
# load model and tokenizer
model_name = "lxyuan/distilbert-base-multilingual-cased-sentiments-student"
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# let's get it to run with this first
text = "I love this movie! It's amazing and heartfelt."
bl_text = "" # baseline
# tokenize
input_ids = tokenizer(text, return_tensors="pt", padding=True, truncation=True)['input_ids']
input_length = input_ids.shape[1]
# create baseline that has same length as input
b_input_ids = tokenizer(bl_text, return_tensors="pt", padding="max_length", max_length=input_length, truncation=True)["input_ids"]
# cast tensors (got that from stackoverflow thread [chatGPT didn't help either])
input_ids = torch.tensor(input_ids).to(device).to(torch.int64)
b_input_ids = torch.tensor(b_input_ids).to(device).to(torch.int64)
# trying if the model works i.g.
# produced output: SequenceClassifierOutput(loss=None, logits=tensor([[ 3.7188, -1.5369, -2.4152]], device='mps:0',
# grad_fn=), hidden_states=None, attentions=None)
output = model(input_ids)
print(output)
target_class = 2
ig = IntegratedGradients(model)
# check if types are correct before going further
print("Inputs dtype:", input_ids.dtype) # expects: torch.int64 (success)
print("Baseline dtype:", b_input_ids.dtype) # expects: torch.int64 (success)
print("Inputs device:", input_ids.device) # expects: mps (success)
print("Baseline device:", b_input_ids.device) # expects: mps (success)
# calc attributions
attributions, delta = ig.attribute(
input_ids,
b_input_ids,
target=target_class,
return_convergence_delta=True,
)
tokens = tokenizer.convert_ids_to_tokens(input_ids[0].tolist())
attributions = attributions[0].detach().cpu().numpy()
print("token with attributes:")
for token, attribution in zip(tokens, attributions):
print(f"{token}: {attribution}")
Код: Выделить всё
Traceback (most recent call last):
File "/Users/Explainable AI/Project/Integrated_Gradients_Project/integ_grad.py", line 45, in
attributions, delta = ig.attribute(
^^^^^^^^^^^^^
[...]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/x/.pyenv/versions/3.12.0/lib/python3.12/site-packages/torch/nn/modules/sparse.py", line 190, in forward
return F.embedding(
^^^^^^^^^^^^
File "/Users/x/.pyenv/versions/3.12.0/lib/python3.12/site-packages/torch/nn/functional.py", line 2551, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got MPSFloatType instead (while checking arguments for embedding)
Process finished with exit code 1
Как вы можете видеть, я использую Python 3.12.0 с недавно установленными PyTorch, Captum, Transfomers, используя виртуальную среду. Я также искал в Интернете и stackoverflow, но решения мне не помогли.
Если вам нужна дополнительная информация, свяжитесь со мной. Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/793 ... -the-follo