Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,117 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- ru
|
5 |
+
base_model:
|
6 |
+
- ai-forever/ruBert-large
|
7 |
+
---
|
8 |
+
# Model Card for Model ID
|
9 |
+
|
10 |
+
Regression model which predicts difficulty score for an input text. Predicted scores can be mapped to CEFT levels.
|
11 |
+
|
12 |
+
|
13 |
+
## Model Details
|
14 |
+
|
15 |
+
Frozen BERT-large layers with a regressor on top. Trained on a mix of manually annotated datasets (more details on data will follow).
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
## How to Get Started with the Model
|
20 |
+
|
21 |
+
Use the code below to get started with the model.
|
22 |
+
|
23 |
+
```
|
24 |
+
class CustomModel(BertPreTrainedModel):
|
25 |
+
def __init__(self, config, load_path=None, use_auth_token: str = None,):
|
26 |
+
super().__init__(config)
|
27 |
+
self.bert = BertModel(config)
|
28 |
+
self.pre_classifier = nn.Linear(config.hidden_size, 128)
|
29 |
+
self.dropout = nn.Dropout(0.2)
|
30 |
+
self.classifier = nn.Linear(128, 1)
|
31 |
+
|
32 |
+
# Apply Xavier initialization
|
33 |
+
nn.init.xavier_uniform_(self.pre_classifier.weight)
|
34 |
+
nn.init.xavier_uniform_(self.classifier.weight)
|
35 |
+
if self.pre_classifier.bias is not None:
|
36 |
+
nn.init.constant_(self.pre_classifier.bias, 0)
|
37 |
+
if self.classifier.bias is not None:
|
38 |
+
nn.init.constant_(self.classifier.bias, 0)
|
39 |
+
|
40 |
+
|
41 |
+
def forward(
|
42 |
+
self,
|
43 |
+
input_ids,
|
44 |
+
labels=None,
|
45 |
+
attention_mask=None,
|
46 |
+
token_type_ids=None,
|
47 |
+
position_ids=None,
|
48 |
+
):
|
49 |
+
outputs = self.bert(
|
50 |
+
input_ids,
|
51 |
+
attention_mask=attention_mask,
|
52 |
+
token_type_ids=token_type_ids,
|
53 |
+
position_ids=position_ids,
|
54 |
+
)
|
55 |
+
|
56 |
+
|
57 |
+
pooled_output = outputs[0][:, 0]
|
58 |
+
pooled_output = self.pre_classifier(pooled_output)
|
59 |
+
pooled_output = nn.ReLU()(pooled_output)
|
60 |
+
pooled_output = self.dropout(pooled_output)
|
61 |
+
logits = self.classifier(pooled_output)
|
62 |
+
|
63 |
+
if labels is not None:
|
64 |
+
loss_fn = nn.MSELoss()
|
65 |
+
loss = loss_fn(logits.view(-1), labels.view(-1))
|
66 |
+
return loss, logits
|
67 |
+
else:
|
68 |
+
return None, logits
|
69 |
+
|
70 |
+
|
71 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
72 |
+
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
73 |
+
config.num_labels = 1
|
74 |
+
|
75 |
+
model = CustomModel(config)
|
76 |
+
model.load_state_dict(torch.load(f'{model_path}/pytorch_model.bin'))
|
77 |
+
|
78 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
79 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
80 |
+
|
81 |
+
with torch.no_grad():
|
82 |
+
_, logits = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], token_type_ids=inputs["token_type_ids"])
|
83 |
+
|
84 |
+
```
|
85 |
+
|
86 |
+
To map to CEFR, use:
|
87 |
+
```
|
88 |
+
reg2cl2 = {'1.0': 'A1', '1.5': 'A12', '2.0': 'A2', '2.5': 'A2', '3.0': 'B1', '3.5': 'B12', '4.0': 'B2', '4.5': 'B2', '5.0': 'C1', '5.5': 'C12', '6.0': 'C2', '0.0': 'A1'}
|
89 |
+
print("Predicted output (logits):", logits.item(), reg2cl2[str(float(round(logits.item())))])
|
90 |
+
```
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
## Training Details
|
95 |
+
|
96 |
+
|
97 |
+
#### Training Hyperparameters
|
98 |
+
|
99 |
+
+ learning_rate: 3e-4
|
100 |
+
+ num_train_epochs: 15.0
|
101 |
+
+ batch_size: 32
|
102 |
+
+ weight_decay: 0.1
|
103 |
+
+ adam_beta1: 0.9
|
104 |
+
+ adam_beta2: 0.99
|
105 |
+
+ adam_epsilon: 1e-8
|
106 |
+
+ max_grad_norm: 1.0
|
107 |
+
+ fp16: True
|
108 |
+
|
109 |
+
|
110 |
+
## Evaluation
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
## Citation [optional]
|
115 |
+
|
116 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
117 |
+
|