lukecq commited on
Commit
7044ac1
·
1 Parent(s): 8a69f7f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +6 -6
README.md CHANGED
@@ -59,20 +59,20 @@ list_label = ["negative", "positive"]
59
 
60
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
61
  list_ABC = [x for x in string.ascii_uppercase]
62
- def add_prefix(text,list_label, shuffle=False):
 
63
  list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
64
  list_label_new = list_label + [tokenizer.pad_token]* (20 - len(list_label))
65
  if shuffle:
66
  random.shuffle(list_label_new)
67
  s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
68
- return f'{s_option} {tokenizer.sep_token} {text}', list_label_new
69
 
70
- def check_text(model, text, list_label, shuffle=False):
71
- text, list_label_new = add_prefix(text,list_label, shuffle = shuffle)
72
  model.to(device).eval()
73
- encoding = tokenizer([text],truncation=True, max_length=512)
74
- item = {key: torch.tensor(val).to(device) for key, val in encoding.items()}
75
  logits = model(**item).logits
 
76
  logits = logits if shuffle else logits[:,0:len(list_label)]
77
  probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
78
  predictions = torch.argmax(logits, dim=-1).item()
 
59
 
60
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
61
  list_ABC = [x for x in string.ascii_uppercase]
62
+
63
+ def check_text(model, text, list_label, shuffle=False):
64
  list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
65
  list_label_new = list_label + [tokenizer.pad_token]* (20 - len(list_label))
66
  if shuffle:
67
  random.shuffle(list_label_new)
68
  s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
69
+ text = f'{s_option} {tokenizer.sep_token} {text}'
70
 
 
 
71
  model.to(device).eval()
72
+ encoding = tokenizer([text],truncation=True, max_length=512,return_tensors='pt')
73
+ item = {key: val.to(device) for key, val in encoding.items()}
74
  logits = model(**item).logits
75
+
76
  logits = logits if shuffle else logits[:,0:len(list_label)]
77
  probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
78
  predictions = torch.argmax(logits, dim=-1).item()