TomokiFujihara commited on
Commit
c505fde
·
1 Parent(s): 79ce989

Upload model

Browse files
Files changed (4) hide show
  1. config.json +16 -0
  2. configuration.py +24 -0
  3. model.safetensors +3 -0
  4. modeling.py +52 -0
config.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "OffensivenessEstimationModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration.OffensivenessEstimationConfig",
7
+ "AutoModel": "modeling.OffensivenessEstimationModel"
8
+ },
9
+ "dropout_rate": 0.1,
10
+ "language_model": "studio-ousia/luke-japanese-base-lite",
11
+ "model_type": "offensiveness_estimation",
12
+ "output_class_num": 11,
13
+ "reinit_n_layers": 1,
14
+ "torch_dtype": "float32",
15
+ "transformers_version": "4.35.2"
16
+ }
configuration.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+ class OffensivenessEstimationConfig(PretrainedConfig):
5
+ model_type = "offensiveness_estimation"
6
+
7
+ def __init__(
8
+ self,
9
+ language_model: str = 'studio-ousia/luke-japanese-base-lite',
10
+ output_class_num: int = 11,
11
+ reinit_n_layers: int = 1,
12
+ dropout_rate: float = 0.1,
13
+ **kwargs,
14
+ ):
15
+ # if block_type not in ["basic", "bottleneck"]:
16
+ # raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.")
17
+ # if stem_type not in ["", "deep", "deep-tiered"]:
18
+ # raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.")
19
+
20
+ self.language_model = language_model
21
+ self.output_class_num = output_class_num
22
+ self.reinit_n_layers = reinit_n_layers
23
+ self.dropout_rate = dropout_rate
24
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:63c3389d4b0f0650fc41ec64abf8fcecc9b70a7deaad8cf5215dbbdbc983b25a
3
+ size 532341340
modeling.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+ from .configuration import *
3
+
4
+ import torch.nn as nn
5
+ import torch
6
+ from transformers import AutoModel
7
+
8
+ class OffensivenessEstimationModel(PreTrainedModel):
9
+ config_class = OffensivenessEstimationConfig
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+ self.text_encoder = PretrainedLanguageModel(config)
14
+ self.decoder = nn.Sequential(
15
+ nn.Dropout(p=config.dropout_rate),
16
+ nn.Linear(768, config.output_class_num)
17
+ )
18
+
19
+ def forward(self, ids, mask):
20
+ h = self.text_encoder(ids, mask)
21
+ output = self.decoder(h)
22
+ return output
23
+
24
+ class PretrainedLanguageModel(PreTrainedModel):
25
+ config_class = OffensivenessEstimationConfig
26
+
27
+ def __init__(self, config):
28
+ super().__init__(config)
29
+ self.language_model = AutoModel.from_pretrained(config.language_model)
30
+ self.reinit_n_layers = config.reinit_n_layers
31
+ if self.reinit_n_layers > 0:
32
+ self._do_reinit()
33
+
34
+ def _do_reinit(self):
35
+ # Re-init last n layers.
36
+ for layer in self.language_model.encoder.layer[-1*self.reinit_n_layers:]:
37
+ for module in layer.modules():
38
+ if isinstance(module, nn.Linear):
39
+ module.weight.data.normal_(mean=0.0, std=self.language_model.config.initializer_range)
40
+ if module.bias is not None:
41
+ module.bias.data.zero_()
42
+ elif isinstance(module, nn.Embedding):
43
+ module.weight.data.normal_(mean=0.0, std=self.language_model.config.initializer_range)
44
+ if module.padding_idx is not None:
45
+ module.weight.data[module.padding_idx].zero_()
46
+ elif isinstance(module, nn.LayerNorm):
47
+ module.bias.data.zero_()
48
+ module.weight.data.fill_(1.0)
49
+
50
+ def forward(self, ids, mask):
51
+ output = self.language_model(ids, attention_mask=mask)
52
+ return output[0][:,0,:]