snisioi commited on
Commit
94fcf8e
·
verified ·
1 Parent(s): c5752f7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +75 -1
README.md CHANGED
@@ -4,4 +4,78 @@ base_model:
4
  pipeline_tag: text-classification
5
  ---
6
 
7
- It's exactly this model https://github.com/i-need-sleep/referee/ made so that it is easier to run.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  pipeline_tag: text-classification
5
  ---
6
 
7
+ It's exactly this model https://github.com/i-need-sleep/referee/ made so that it is easier to run.
8
+
9
+ ```python
10
+ '''
11
+ Code from i-need-sleep https://github.com/i-need-sleep/referee/tree/main/code
12
+ '''
13
+
14
+ import torch
15
+ from transformers import AutoModel, AutoTokenizer
16
+
17
+ class DebertaForEval(torch.nn.Module):
18
+ def __init__(self, model_path, device='cuda', n_supervision=13, head_type='linear', backbone='deberta'):
19
+ super(DebertaForEval, self).__init__()
20
+ self.n_supervision = n_supervision
21
+ self.device = device
22
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path)
23
+ self.deberta = AutoModel.from_pretrained(model_path)
24
+ self.backbone = backbone
25
+ if backbone == 'deberta':
26
+ self.hidden_size = 768
27
+ elif backbone == 'roberta':
28
+ self.hidden_size = 1024
29
+ else:
30
+ raise NotImplementedError
31
+ self.head_type = head_type
32
+ if head_type == 'mlp':
33
+ self.regression_heads_layer_1 = torch.nn.ModuleList([torch.nn.Linear(self.hidden_size, 512) for i in range(n_supervision)])
34
+ self.regression_heads_layer_2 = torch.nn.ModuleList([torch.nn.Linear(512, 1) for i in range(n_supervision)])
35
+ self.relu = torch.nn.ReLU()
36
+ elif head_type == 'linear':
37
+ self.linear_out = torch.nn.ModuleList([torch.nn.Linear(self.hidden_size, 1) for i in range(n_supervision)])
38
+ else:
39
+ raise NotImplementedError
40
+ self.to(device)
41
+ self.float()
42
+
43
+ def forward(self, sents):
44
+ if self.backbone == 'deberta':
45
+ tokenized = self.tokenizer(sents, padding=True, truncation=True, max_length=512)
46
+ if len(tokenized['input_ids']) >= 512:
47
+ print("Warning: input exceeds 512 tokens.")
48
+ input_ids = torch.tensor(tokenized['input_ids']).to(self.device)
49
+ token_type_ids = torch.tensor(tokenized['token_type_ids']).to(self.device)
50
+ attention_mask = torch.tensor(tokenized['attention_mask']).to(self.device)
51
+ model_out = self.deberta(input_ids=input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask)[0][:, 0, :] # Take the emb for the first token
52
+ elif self.backbone == 'roberta':
53
+ encoded_input = self.tokenizer(sents, return_tensors='pt', padding=True, truncation=True, max_length=512)
54
+ for key, val in encoded_input.items():
55
+ encoded_input[key] = val.to(self.device)
56
+ model_out = self.deberta(**encoded_input)[0][:, 0, :]
57
+ else:
58
+ raise NotImplementedError
59
+ heads_out = []
60
+ for head_idx in range(self.n_supervision):
61
+ if self.head_type == 'mlp':
62
+ head_out = self.regression_heads_layer_1[head_idx](model_out)
63
+ head_out = self.relu(head_out)
64
+ head_out = self.regression_heads_layer_2[head_idx](head_out)
65
+ heads_out.append(head_out)
66
+ elif self.head_type == 'linear':
67
+ head_out = self.linear_out[head_idx](model_out)
68
+ heads_out.append(head_out)
69
+ heads_out = torch.cat(heads_out, dim=1)
70
+ return heads_out # [batch_size, n_head]
71
+
72
+
73
+ model = DebertaForEval('snisioi/referee', head_type='linear')
74
+ example_complex = """This book constitutes an argument for the power of Marxism to analyse the issues that face women today in their struggle for liberation."""
75
+ example_simple = """This book explains how Marxism can help us understand the problems women face."""
76
+
77
+ model_input = [example_complex + ' ' + model.tokenizer.sep_token + ' ' + example_simple]
78
+ model_out = model(model_input)
79
+ score = model_out[:, -1].item()
80
+ print(score)
81
+ ```