helboukkouri
commited on
Commit
•
30397d8
1
Parent(s):
09ee222
Create configuration_character_bert.py
Browse files- configuration_character_bert.py +156 -0
configuration_character_bert.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright Hicham EL BOUKKOURI, Olivier FERRET, Thomas LAVERGNE, Hiroshi NOJI,
|
3 |
+
# Pierre ZWEIGENBAUM, Junichi TSUJII and The HuggingFace Inc. team.
|
4 |
+
# All rights reserved.
|
5 |
+
#
|
6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
+
# you may not use this file except in compliance with the License.
|
8 |
+
# You may obtain a copy of the License at
|
9 |
+
#
|
10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
+
#
|
12 |
+
# Unless required by applicable law or agreed to in writing, software
|
13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
+
# See the License for the specific language governing permissions and
|
16 |
+
# limitations under the License.
|
17 |
+
""" CharacterBERT model configuration"""
|
18 |
+
|
19 |
+
from transformers.configuration_utils import PretrainedConfig
|
20 |
+
from transformers.utils import logging
|
21 |
+
|
22 |
+
|
23 |
+
logger = logging.get_logger(__name__)
|
24 |
+
|
25 |
+
CHARACTER_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
26 |
+
"helboukkouri/character-bert": "https://huggingface.co/helboukkouri/character-bert/resolve/main/config.json",
|
27 |
+
"helboukkouri/character-bert-medical": "https://huggingface.co/helboukkouri/character-bert-medical/resolve/main/config.json",
|
28 |
+
# See all CharacterBERT models at https://huggingface.co/models?filter=character_bert
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
class CharacterBertConfig(PretrainedConfig):
|
33 |
+
r"""
|
34 |
+
This is the configuration class to store the configuration of a [`CharacterBertModel`]. It is
|
35 |
+
used to instantiate an CharacterBERT model according to the specified arguments, defining the model architecture.
|
36 |
+
Instantiating a configuration with the defaults will yield a similar configuration to that of the CharacterBERT
|
37 |
+
[helboukkouri/character-bert](https://huggingface.co/helboukkouri/character-bert) architecture.
|
38 |
+
|
39 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model
|
40 |
+
outputs. Read the documentation from [`PretrainedConfig`] for more information.
|
41 |
+
|
42 |
+
|
43 |
+
Args:
|
44 |
+
character_embeddings_dim (`int`, *optional*, defaults to `16`):
|
45 |
+
The size of the character embeddings.
|
46 |
+
cnn_activation (`str`, *optional*, defaults to `"relu"`):
|
47 |
+
The activation function to apply to the cnn representations.
|
48 |
+
cnn_filters (:
|
49 |
+
obj:*list(list(int))*, *optional*, defaults to `[[1, 32], [2, 32], [3, 64], [4, 128], [5, 256], [6, 512], [7, 1024]]`): The list of CNN filters to use in the CharacterCNN module.
|
50 |
+
num_highway_layers (`int`, *optional*, defaults to `2`):
|
51 |
+
The number of Highway layers to apply to the CNNs output.
|
52 |
+
max_word_length (`int`, *optional*, defaults to `50`):
|
53 |
+
The maximum token length in characters (actually, in bytes as any non-ascii characters will be converted to
|
54 |
+
a sequence of utf-8 bytes).
|
55 |
+
hidden_size (`int`, *optional*, defaults to 768):
|
56 |
+
Dimensionality of the encoder layers and the pooler layer.
|
57 |
+
num_hidden_layers (`int`, *optional*, defaults to 12):
|
58 |
+
Number of hidden layers in the Transformer encoder.
|
59 |
+
num_attention_heads (`int`, *optional*, defaults to 12):
|
60 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
61 |
+
intermediate_size (`int`, *optional*, defaults to 3072):
|
62 |
+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
63 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
|
64 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string,
|
65 |
+
`"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported.
|
66 |
+
hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
|
67 |
+
The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.
|
68 |
+
attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
|
69 |
+
The dropout ratio for the attention probabilities.
|
70 |
+
max_position_embeddings (`int`, *optional*, defaults to 512):
|
71 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
72 |
+
just in case (e.g., 512 or 1024 or 2048).
|
73 |
+
type_vocab_size (`int`, *optional*, defaults to 2):
|
74 |
+
The vocabulary size of the `token_type_ids` passed when calling
|
75 |
+
[`CharacterBertModel`] or [`TFCharacterBertModel`].
|
76 |
+
mlm_vocab_size (`int`, *optional*, defaults to 100000):
|
77 |
+
Size of the output vocabulary for MLM.
|
78 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
79 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
80 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-12):
|
81 |
+
The epsilon used by the layer normalization layers.
|
82 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
83 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
84 |
+
relevant if `config.is_decoder=True`.
|
85 |
+
|
86 |
+
Example:
|
87 |
+
|
88 |
+
```python
|
89 |
+
|
90 |
+
```
|
91 |
+
|
92 |
+
>>> from transformers import CharacterBertModel, CharacterBertConfig
|
93 |
+
|
94 |
+
>>> # Initializing a CharacterBERT helboukkouri/character-bert style configuration
|
95 |
+
>>> configuration = CharacterBertConfig()
|
96 |
+
|
97 |
+
>>> # Initializing a model from the helboukkouri/character-bert style configuration
|
98 |
+
>>> model = CharacterBertModel(configuration)
|
99 |
+
|
100 |
+
>>> # Accessing the model configuration
|
101 |
+
>>> configuration = model.config
|
102 |
+
"""
|
103 |
+
model_type = "character_bert"
|
104 |
+
|
105 |
+
def __init__(
|
106 |
+
self,
|
107 |
+
character_embeddings_dim=16,
|
108 |
+
cnn_activation="relu",
|
109 |
+
cnn_filters=None,
|
110 |
+
num_highway_layers=2,
|
111 |
+
max_word_length=50,
|
112 |
+
hidden_size=768,
|
113 |
+
num_hidden_layers=12,
|
114 |
+
num_attention_heads=12,
|
115 |
+
intermediate_size=3072,
|
116 |
+
hidden_act="gelu",
|
117 |
+
hidden_dropout_prob=0.1,
|
118 |
+
attention_probs_dropout_prob=0.1,
|
119 |
+
max_position_embeddings=512,
|
120 |
+
type_vocab_size=2,
|
121 |
+
mlm_vocab_size=100000,
|
122 |
+
initializer_range=0.02,
|
123 |
+
layer_norm_eps=1e-12,
|
124 |
+
is_encoder_decoder=False,
|
125 |
+
use_cache=True,
|
126 |
+
**kwargs
|
127 |
+
):
|
128 |
+
tie_word_embeddings = kwargs.pop("tie_word_embeddings", False)
|
129 |
+
if tie_word_embeddings:
|
130 |
+
raise ValueError(
|
131 |
+
"Cannot tie word embeddings in CharacterBERT. Please set " "`config.tie_word_embeddings=False`."
|
132 |
+
)
|
133 |
+
super().__init__(
|
134 |
+
type_vocab_size=type_vocab_size,
|
135 |
+
layer_norm_eps=layer_norm_eps,
|
136 |
+
use_cache=use_cache,
|
137 |
+
tie_word_embeddings=tie_word_embeddings,
|
138 |
+
**kwargs,
|
139 |
+
)
|
140 |
+
if cnn_filters is None:
|
141 |
+
cnn_filters = [[1, 32], [2, 32], [3, 64], [4, 128], [5, 256], [6, 512], [7, 1024]]
|
142 |
+
self.character_embeddings_dim = character_embeddings_dim
|
143 |
+
self.cnn_activation = cnn_activation
|
144 |
+
self.cnn_filters = cnn_filters
|
145 |
+
self.num_highway_layers = num_highway_layers
|
146 |
+
self.max_word_length = max_word_length
|
147 |
+
self.hidden_size = hidden_size
|
148 |
+
self.num_hidden_layers = num_hidden_layers
|
149 |
+
self.num_attention_heads = num_attention_heads
|
150 |
+
self.intermediate_size = intermediate_size
|
151 |
+
self.mlm_vocab_size = mlm_vocab_size
|
152 |
+
self.hidden_act = hidden_act
|
153 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
154 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
155 |
+
self.max_position_embeddings = max_position_embeddings
|
156 |
+
self.initializer_range = initializer_range
|