BERT model ("Bidirectional Embedding Representations from a Transformer"). Params: config: a BertConfig class instance with the configuration to build a new model Inputs: `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length] with the word token
| 850 | |
| 851 | |
| 852 | class BertModel(PreTrainedBertModel): |
| 853 | """BERT model ("Bidirectional Embedding Representations from a Transformer"). |
| 854 | |
| 855 | Params: |
| 856 | config: a BertConfig class instance with the configuration to build a new model |
| 857 | |
| 858 | Inputs: |
| 859 | `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length] |
| 860 | with the word token indices in the vocabulary(see the tokens preprocessing logic in the scripts |
| 861 | `extract_features.py`, `run_classifier.py` and `run_squad.py`) |
| 862 | `token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token |
| 863 | types indices selected in [0, 1]. Type 0 corresponds to a `sentence A` and type 1 corresponds to |
| 864 | a `sentence B` token (see BERT paper for more details). |
| 865 | `attention_mask`: an optional torch.LongTensor of shape [batch_size, sequence_length] with indices |
| 866 | selected in [0, 1]. It's a mask to be used if the input sequence length is smaller than the max |
| 867 | input sequence length in the current batch. It's the mask that we typically use for attention when |
| 868 | a batch has varying length sentences. |
| 869 | `output_all_encoded_layers`: boolean which controls the content of the `encoded_layers` output as described below. Default: `True`. |
| 870 | |
| 871 | Outputs: Tuple of (encoded_layers, pooled_output) |
| 872 | `encoded_layers`: controled by `output_all_encoded_layers` argument: |
| 873 | - `output_all_encoded_layers=True`: outputs a list of the full sequences of encoded-hidden-states at the end |
| 874 | of each attention block (i.e. 12 full sequences for BERT-base, 24 for BERT-large), each |
| 875 | encoded-hidden-state is a torch.FloatTensor of size [batch_size, sequence_length, hidden_size], |
| 876 | - `output_all_encoded_layers=False`: outputs only the full sequence of hidden-states corresponding |
| 877 | to the last attention block of shape [batch_size, sequence_length, hidden_size], |
| 878 | `pooled_output`: a torch.FloatTensor of size [batch_size, hidden_size] which is the output of a |
| 879 | classifier pretrained on top of the hidden state associated to the first character of the |
| 880 | input (`CLF`) to train on the Next-Sentence task (see BERT's paper). |
| 881 | |
| 882 | Example usage: |
| 883 | ```python |
| 884 | # Already been converted into WordPiece token ids |
| 885 | input_ids = torch.LongTensor([[31, 51, 99], [15, 5, 0]]) |
| 886 | input_mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]]) |
| 887 | token_type_ids = torch.LongTensor([[0, 0, 1], [0, 1, 0]]) |
| 888 | |
| 889 | config = modeling.BertConfig(vocab_size_or_config_json_file=32000, hidden_size=768, |
| 890 | num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072) |
| 891 | |
| 892 | model = modeling.BertModel(config=config) |
| 893 | all_encoder_layers, pooled_output = model(input_ids, token_type_ids, input_mask) |
| 894 | ``` |
| 895 | """ |
| 896 | |
| 897 | def __init__(self, config): |
| 898 | super(BertModel, self).__init__(config) |
| 899 | self.embeddings = BertEmbeddings(config) |
| 900 | self.encoder = BertEncoder(config) |
| 901 | self.pooler = BertPooler(config) |
| 902 | self.apply(self.init_bert_weights) |
| 903 | |
| 904 | def forward(self, input_ids, token_type_ids=None, attention_mask=None, output_all_encoded_layers=True, |
| 905 | checkpoint_activations=False): |
| 906 | if attention_mask is None: |
| 907 | attention_mask = torch.ones_like(input_ids) |
| 908 | if token_type_ids is None: |
| 909 | token_type_ids = torch.zeros_like(input_ids) |