tdunlap607 commited on
Commit
3c22ce9
·
1 Parent(s): 401e471

Fold 3 - Epoch 2

Browse files
Files changed (3) hide show
  1. config.json +34 -0
  2. custom_models.py +125 -0
  3. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "microsoft/codebert-base",
3
+ "architectures": [
4
+ "CustomModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "auto_map": {
8
+ "AutoModel": "custom_models.CustomModel"
9
+ },
10
+ "bos_token_id": 0,
11
+ "classifier_dropout": null,
12
+ "eos_token_id": 2,
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout_prob": 0.1,
15
+ "hidden_size": 768,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 3072,
18
+ "layer_norm_eps": 1e-05,
19
+ "max_position_embeddings": 514,
20
+ "model_type": "roberta",
21
+ "num_attention_heads": 12,
22
+ "num_hidden_layers": 12,
23
+ "output_hidden_states": true,
24
+ "output_past": true,
25
+ "pad_token_id": 1,
26
+ "position_embedding_type": "absolute",
27
+ "torch_dtype": "float32",
28
+ "transformers_version": "4.22.2",
29
+ "type_vocab_size": 1,
30
+ "use_cache": true,
31
+ "vocab_size": 50265,
32
+ "model_name": "microsoft/codebert-base",
33
+ "gradient_checkpointing": false
34
+ }
custom_models.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import AutoModel, AutoConfig
4
+ from transformers.modeling_utils import PreTrainedModel
5
+ from transformers import PretrainedConfig
6
+
7
+
8
+ class CustomConfig(PretrainedConfig):
9
+ model_type = "roberta"
10
+
11
+ def __init__(
12
+ self,
13
+ num_classes: int = 2,
14
+ **kwargs,
15
+ ):
16
+ self.num_classes = num_classes
17
+ super().__init__(**kwargs)
18
+
19
+
20
+ # ====================================================
21
+ # Model
22
+ # ====================================================
23
+ # class MeanPooling(nn.Module):
24
+ class MeanPooling(PreTrainedModel):
25
+ def __init__(
26
+ self,
27
+ config
28
+ # **kwargs,
29
+ ):
30
+ super(MeanPooling, self).__init__(config)
31
+
32
+ def forward(self, last_hidden_state, attention_mask):
33
+ input_mask_expanded = (
34
+ attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()
35
+ )
36
+ sum_embeddings = torch.sum(last_hidden_state * input_mask_expanded, 1)
37
+ sum_mask = input_mask_expanded.sum(1)
38
+ sum_mask = torch.clamp(sum_mask, min=1e-9)
39
+ mean_embeddings = sum_embeddings / sum_mask
40
+ return mean_embeddings
41
+
42
+
43
+ # class CustomModel(nn.Module):
44
+ class CustomModel(PreTrainedModel):
45
+ config_class = CustomConfig
46
+
47
+ def __init__(
48
+ self,
49
+ cfg,
50
+ num_labels=2,
51
+ config_path=None,
52
+ pretrained=True,
53
+ binary_classification=True,
54
+ **kwargs,
55
+ ):
56
+ # super().__init__()
57
+ self.cfg = cfg
58
+ self.num_labels = num_labels
59
+ if config_path is None:
60
+ self.config = AutoConfig.from_pretrained(
61
+ self.cfg.model_name, output_hidden_states=True
62
+ )
63
+
64
+ else:
65
+ self.config = torch.load(config_path)
66
+
67
+ super().__init__(self.config)
68
+
69
+ if pretrained:
70
+ self.model = AutoModel.from_pretrained(
71
+ self.cfg.model_name, config=self.config
72
+ )
73
+ else:
74
+ self.model = AutoModel(self.config)
75
+
76
+ if self.cfg.gradient_checkpointing:
77
+ self.model.gradient_checkpointing_enable()
78
+
79
+ self.pool = MeanPooling(config=self.config)
80
+
81
+ self.binary_classification = binary_classification
82
+
83
+ if self.binary_classification:
84
+ # for binary classification we only want to output a single value
85
+ self.fc = nn.Linear(self.config.hidden_size, self.num_labels - 1)
86
+ else:
87
+ self.fc = nn.Linear(self.config.hidden_size, self.num_labels)
88
+
89
+ self._init_weights(self.fc)
90
+
91
+ self.sigmoid_fn = nn.Sigmoid()
92
+
93
+ def _init_weights(self, module):
94
+ if isinstance(module, nn.Linear):
95
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
96
+ if module.bias is not None:
97
+ module.bias.data.zero_()
98
+ elif isinstance(module, nn.Embedding):
99
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
100
+ if module.padding_idx is not None:
101
+ module.weight.data[module.padding_idx].zero_()
102
+ elif isinstance(module, nn.LayerNorm):
103
+ module.bias.data.zero_()
104
+ module.weight.data.fill_(1.0)
105
+
106
+ def feature(self, input_ids, attention_mask, token_type_ids):
107
+ outputs = self.model(
108
+ input_ids=input_ids,
109
+ attention_mask=attention_mask,
110
+ token_type_ids=token_type_ids,
111
+ )
112
+ last_hidden_states = outputs[0]
113
+ feature = self.pool(last_hidden_states, attention_mask)
114
+ return feature
115
+
116
+ def forward(self, input_ids, attention_mask, token_type_ids):
117
+ feature = self.feature(input_ids, attention_mask, token_type_ids)
118
+ output = self.fc(feature)
119
+ if self.binary_classification:
120
+ # for binary classification we have to use Sigmoid Function
121
+ # https://towardsdatascience.com/sigmoid-and-softmax-functions-in-5-minutes-f516c80ea1f9
122
+ # https://towardsdatascience.com/bert-to-the-rescue-17671379687f
123
+ output = self.sigmoid_fn(output)
124
+
125
+ return output
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea8a45a5fe39d50aa4758cfff7aef00d9e7baae698229a1adbca8bd00b2a9aaa
3
+ size 498658613