An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
| 540 | |
| 541 | |
| 542 | class BertPreTrainedModel(PreTrainedModel): |
| 543 | """ An abstract class to handle weights initialization and |
| 544 | a simple interface for downloading and loading pretrained models. |
| 545 | """ |
| 546 | |
| 547 | config_class = BertConfig |
| 548 | load_tf_weights = load_tf_weights_in_bert |
| 549 | base_model_prefix = "bert" |
| 550 | |
| 551 | def _init_weights(self, module): |
| 552 | """ Initialize the weights """ |
| 553 | if isinstance(module, (nn.Linear, nn.Embedding)): |
| 554 | # Slightly different from the TF version which uses truncated_normal for initialization |
| 555 | # cf https://github.com/pytorch/pytorch/pull/5617 |
| 556 | module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) |
| 557 | elif isinstance(module, BertLayerNorm): |
| 558 | module.bias.data.zero_() |
| 559 | module.weight.data.fill_(1.0) |
| 560 | if isinstance(module, nn.Linear) and module.bias is not None: |
| 561 | module.bias.data.zero_() |
| 562 | |
| 563 | |
| 564 | BERT_START_DOCSTRING = r""" |
nothing calls this directly
no outgoing calls
no test coverage detected