brownfortress commited on
Commit
d34f202
·
1 Parent(s): 3b562e6
Files changed (3) hide show
  1. amc.py +62 -0
  2. config.json +17 -0
  3. pytorch_model.bin +3 -0
amc.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+
3
+ from transformers import PreTrainedModel, PretrainedConfig, AutoModel, AutoTokenizer
4
+ import torch.nn as nn
5
+ import torch
6
+ from typing import List
7
+
8
+
9
+ class AMC_OPT_conf(PretrainedConfig):
10
+ model_type = "bert"
11
+ def __init__(self,
12
+ out_labels=3,
13
+ emb_size=768,
14
+ drop_out=0.1,
15
+ pretrained_model="m-polignano-uniba/bert_uncased_L-12_H-768_A-12_italian_alb3rt0",
16
+ **kwargs):
17
+ self.out_labels = out_labels
18
+ self.drop_out = drop_out
19
+ self.emb_size = emb_size
20
+ self.pretrained_model = pretrained_model
21
+ self.fine_tuned_model = "data/models/amc_opt_msmd_nocoadapt.pt"
22
+ super().__init__(**kwargs)
23
+
24
+
25
+ class AMC_OPT_sub(nn.Module):
26
+ def __init__(self, config):
27
+ super(AMC_OPT_sub, self).__init__()
28
+
29
+ self.model = copy.deepcopy(AutoModel.from_pretrained(config.pretrained_model))
30
+ self.dropout1 = nn.Dropout(config.drop_out)
31
+ self.linear1 = nn.Linear(config.emb_size, config.out_labels)
32
+
33
+ self.loss_fct = nn.CrossEntropyLoss()
34
+ self.hyper_params = {'learning_rate': 6.599917952321265e-05,
35
+ 'weight_decay': 0.02157165894420757,
36
+ 'warmup_steps': 0.8999999999999999,
37
+ 'num_epochs': 11}
38
+ self.params_name = "alberto_multiclass_opt_msmd.pt"
39
+
40
+ def forward(self, labels, input_ids, attention_mask, **args):
41
+ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **args)
42
+ x = self.dropout1(outputs[1])
43
+ logits = self.linear1(x)
44
+ if labels != None:
45
+ loss = self.loss_fct(logits, labels)
46
+ return {'logits':logits, 'loss':loss}
47
+ else:
48
+ return {'logits':logits}
49
+
50
+ class AMC_OPT(PreTrainedModel):
51
+ config_class = AMC_OPT_conf
52
+
53
+ def __init__(self, config):
54
+ super().__init__(config)
55
+ self.model = AMC_OPT_sub(config)
56
+ self.model.load_state_dict(torch.load(config.fine_tuned_model))
57
+
58
+ def forward(self, labels, input_ids, attention_mask, **args):
59
+ return self.model(labels, input_ids, attention_mask, **args)
60
+
61
+ tokenizer = AutoTokenizer.from_pretrained("m-polignano-uniba/bert_uncased_L-12_H-768_A-12_italian_alb3rt0")
62
+ tokenizer.model_max_length = 128
config.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "AMC_OPT"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "amc.AMC_OPT_conf",
7
+ "AutoModelForSequenceClassification": "amc.AMC_OPT"
8
+ },
9
+ "drop_out": 0.1,
10
+ "emb_size": 768,
11
+ "fine_tuned_model": "data/models/amc_opt_msmd_nocoadapt.pt",
12
+ "model_type": "bert",
13
+ "out_labels": 3,
14
+ "pretrained_model": "m-polignano-uniba/bert_uncased_L-12_H-768_A-12_italian_alb3rt0",
15
+ "torch_dtype": "float32",
16
+ "transformers_version": "4.18.0"
17
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e8e4602d1fabefb736c5ec5abd5de581f74a03fe58d653a958ec22b060dee6c
3
+ size 737463021