unitorch.models.bert¤
BertProcessor¤
Bases: HfTextClassificationProcessor
Initializes the BertProcessor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab_path |
str
|
The path to the vocabulary file. |
required |
max_seq_length |
Optional[int]
|
The maximum sequence length. Defaults to 128. |
128
|
special_input_ids |
Optional[Dict]
|
Special input IDs mapping. Defaults to an empty dictionary. |
dict()
|
do_lower_case |
Optional[bool]
|
Whether to perform lowercase tokenization. Defaults to True. |
True
|
do_basic_tokenize |
Optional[bool]
|
Whether to perform basic tokenization. Defaults to True. |
True
|
do_whole_word_mask |
Optional[bool]
|
Whether to perform whole word masking. Defaults to True. |
True
|
masked_lm_prob |
Optional[float]
|
The probability of masking a token for pretraining. Defaults to 0.15. |
0.15
|
max_predictions_per_seq |
Optional[int]
|
The maximum number of masked tokens per sequence for pretraining. Defaults to 20. |
20
|
Source code in src/unitorch/models/bert/processing.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
pretrain ¤
pretrain(
text: str,
text_pair: str,
nsp_label: int,
max_seq_length: Optional[int] = None,
masked_lm_prob: Optional[float] = None,
do_whole_word_mask: Optional[bool] = None,
max_predictions_per_seq: Optional[int] = None,
)
The Bert pretrain processor on the given text and text pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text. |
required |
text_pair |
str
|
The input text pair. |
required |
nsp_label |
int
|
The next sentence prediction label. |
required |
max_seq_length |
Optional[int]
|
The maximum sequence length. Defaults to None. |
None
|
masked_lm_prob |
Optional[float]
|
The probability of masking a token for pretraining. Defaults to None. |
None
|
do_whole_word_mask |
Optional[bool]
|
Whether to perform whole word masking. Defaults to None. |
None
|
max_predictions_per_seq |
Optional[int]
|
The maximum number of masked tokens per sequence for pretraining. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
pretrain processing outputs. |
Source code in src/unitorch/models/bert/processing.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
BertForClassification¤
Bases: GenericModel
Initializes the BertForClassification model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path |
str
|
The path to the configuration file. |
required |
num_classes |
Optional[int]
|
The number of classes for classification. Defaults to 1. |
1
|
gradient_checkpointing |
Optional[bool]
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
Source code in src/unitorch/models/bert/modeling.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
forward ¤
forward(
input_ids: Tensor,
attention_mask: Optional[Tensor] = None,
token_type_ids: Optional[Tensor] = None,
position_ids: Optional[Tensor] = None,
)
Forward pass of the BertForClassification model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids |
Tensor
|
The input tensor of token indices. |
required |
attention_mask |
torch.Tensor optional
|
The attention mask tensor. Defaults to None. |
None
|
token_type_ids |
torch.Tensor optional
|
The token type IDs tensor. Defaults to None. |
None
|
position_ids |
torch.Tensor optional
|
The position IDs tensor. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The logits of the model output. |
Source code in src/unitorch/models/bert/modeling.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|