赞
踩
import transformers
from transformers import pipeline
task_name = 'sentiment-analysis'
自动下载的模型就是"distilbert-base-uncased-finetuned-sst-2-english"
pipeline(task_name)
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Downloading model.safetensors: 63%|████████████████████████████████████████▍ | 169M/268M [00:35<00:20, 4.79MB/s]
Downloading model.safetensors: 100%|████████████████████████████████████████████████████████████████| 268M/268M [00:51<00:00, 5.18MB/s]
<transformers.pipelines.text_classification.TextClassificationPipeline at 0x14543df30>
test_sentence = ['today is not that bad', 'today is so bad']
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
batch_input = tokenizer(test_sentence, return_tensors='pt', padding=True)
batch_input
{'input_ids': tensor([[ 101, 2651, 2003, 2025, 2008, 2919, 102],
[ 101, 2651, 2003, 2061, 2919, 102, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0]])}
import torch
import torch.nn.functional as F
output = model(**batch_input)
output
SequenceClassifierOutput(loss=None, logits=tensor([[-3.4620, 3.6118],
[ 4.7508, -3.7899]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)
model.config
DistilBertConfig { "_name_or_path": "distilbert-base-uncased-finetuned-sst-2-english", "activation": "gelu", "architectures": [ "DistilBertForSequenceClassification" ], "attention_dropout": 0.1, "dim": 768, "dropout": 0.1, "finetuning_task": "sst-2", "hidden_dim": 3072, "id2label": { "0": "NEGATIVE", "1": "POSITIVE" }, "initializer_range": 0.02, "label2id": { "NEGATIVE": 0, "POSITIVE": 1 }, "max_position_embeddings": 512, "model_type": "distilbert", "n_heads": 12, "n_layers": 6, "output_past": true, "pad_token_id": 0, "qa_dropout": 0.1, "seq_classif_dropout": 0.2, "sinusoidal_pos_embds": false, "tie_weights_": true, "transformers_version": "4.34.0", "vocab_size": 30522 }
with torch.no_grad():
output = model(**batch_input)
print(output)
scores = F.softmax(output.logits, dim=-1)
print(scores)
labels = scores.argmax(dim=-1)
print(labels)
labels = [model.config.id2label[id] for id in labels.tolist()]
print(labels)
SequenceClassifierOutput(loss=None, logits=tensor([[-3.4620, 3.6118],
[ 4.7508, -3.7899]]), hidden_states=None, attentions=None)
tensor([[8.4631e-04, 9.9915e-01],
[9.9980e-01, 1.9531e-04]])
tensor([1, 0])
['POSITIVE', 'NEGATIVE']
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。