lukecq commited on
Commit
459852d
·
1 Parent(s): d334121

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +21 -13
README.md CHANGED
@@ -49,31 +49,39 @@ You can try the model with the Colab [Notebook](https://colab.research.google.co
49
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
50
  import torch, string, random
51
 
52
- tokenizer = AutoTokenizer.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
53
- model = AutoModelForSequenceClassification.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
54
 
55
  text = "I love this place! The food is always so fresh and delicious."
56
  list_label = ["negative", "positive"]
57
 
 
58
  list_ABC = [x for x in string.ascii_uppercase]
59
- def add_prefix(text, list_label, shuffle = False):
 
60
  list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
61
  list_label_new = list_label + [tokenizer.pad_token]* (20 - len(list_label))
62
  if shuffle:
63
  random.shuffle(list_label_new)
64
  s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
65
- return f'{s_option} {tokenizer.sep_token} {text}', list_label_new
66
-
67
- text_new, list_label_new = add_prefix(text,list_label,shuffle=False)
68
-
69
- encoding = tokenizer([text_new],truncation=True, padding='max_length',max_length=512, return_tensors='pt')
70
- with torch.no_grad():
71
- logits = model(**encoding).logits
 
72
  probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
73
- predictions = torch.argmax(logits, dim=-1)
 
 
 
 
74
 
75
- print(probs)
76
- print(predictions)
 
77
  ```
78
 
79
 
 
49
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
50
  import torch, string, random
51
 
52
+ tokenizer = AutoTokenizer.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-large")
53
+ model = AutoModelForSequenceClassification.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-large")
54
 
55
  text = "I love this place! The food is always so fresh and delicious."
56
  list_label = ["negative", "positive"]
57
 
58
+ device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
59
  list_ABC = [x for x in string.ascii_uppercase]
60
+
61
+ def check_text(model, text, list_label, shuffle=False):
62
  list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
63
  list_label_new = list_label + [tokenizer.pad_token]* (20 - len(list_label))
64
  if shuffle:
65
  random.shuffle(list_label_new)
66
  s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
67
+ text = f'{s_option} {tokenizer.sep_token} {text}'
68
+
69
+ model.to(device).eval()
70
+ encoding = tokenizer([text],truncation=True, max_length=512,return_tensors='pt')
71
+ item = {key: val.to(device) for key, val in encoding.items()}
72
+ logits = model(**item).logits
73
+
74
+ logits = logits if shuffle else logits[:,0:len(list_label)]
75
  probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
76
+ predictions = torch.argmax(logits, dim=-1).item()
77
+ probabilities = [round(x,5) for x in probs[0]]
78
+
79
+ print(f'prediction: {predictions} => ({list_ABC[predictions]}) {list_label_new[predictions]}')
80
+ print(f'probability: {round(probabilities[predictions]*100,2)}%')
81
 
82
+ check_text(model, text, list_label)
83
+ # prediction: 1 => (B) positive.
84
+ # probability: 99.84%
85
  ```
86
 
87