ragarwal's picture
Update README.md
3e63368
|
raw
history blame
1.82 kB
---
license: mit
---
### Usage
In Transformers
```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from torch.nn.functional import softmax, sigmoid
device = "cuda" if torch.cuda.is_available() else "cpu"
model_name="ragarwal/deberta-v3-base-nli-mixer"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
sentence = "During its monthly call, the National Oceanic and Atmospheric Administration warned of \
increased temperatures and low precipitation"
labels = ["Computer", "Climate Change", "Tablet", "Football", "Artificial Intelligence", "Global Warming"]
features = tokenizer([[sentence, l] for l in labels], padding=True, truncation=True, return_tensors="pt")
model.eval()
with torch.no_grad():
scores = model(**features).logits
print("Multi-Label:", sigmoid(scores)) #Multi-Label Classification
print("Single-Label:", softmax(scores, dim=0)) #Single-Label Classification
#Multi-Label: tensor([[0.0412],[0.2436],[0.0394],[0.0020],[0.0050],[0.1424]])
#Single-Label: tensor([[0.0742],[0.5561],[0.0709],[0.0035],[0.0087],[0.2867]])
```
In [Sentence-Transformers](https://www.sbert.net/)
```python
from sentence_transformers import CrossEncoder
model_name="ragarwal/deberta-v3-base-nli-mixer"
model = CrossEncoder(model_name, max_length=256)
sentence = "During its monthly call, the National Oceanic and Atmospheric Administration warned of \
increased temperatures and low precipitation"
labels = ["Computer", "Climate Change", "Tablet", "Football", "Artificial Intelligence", "Global Warming"]
scores = model.predict([[sentence, l] for l in labels])
print(scores)
#array([0.04118565, 0.2435827 , 0.03941465, 0.00203637, 0.00501176, 0.1423797], dtype=float32)
```