|
|
|
@@ -21,7 +21,7 @@ import logging
|
|
|
|
import tensorflow as tf
|
|
|
|
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
from .configuration_albert import AlbertConfig
|
|
|
|
from .configuration_albert import AlbertConfig
|
|
|
|
from .file_utils import add_start_docstrings
|
|
|
|
from .file_utils import add_start_docstrings, add_start_docstrings_to_callable
|
|
|
|
from .modeling_tf_bert import ACT2FN, TFBertSelfAttention
|
|
|
|
from .modeling_tf_bert import ACT2FN, TFBertSelfAttention
|
|
|
|
from .modeling_tf_utils import TFPreTrainedModel, get_initializer, shape_list
|
|
|
|
from .modeling_tf_utils import TFPreTrainedModel, get_initializer, shape_list
|
|
|
|
|
|
|
|
|
|
|
|
@@ -478,12 +478,9 @@ class TFAlbertMLMHead(tf.keras.layers.Layer):
|
|
|
|
return hidden_states
|
|
|
|
return hidden_states
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ALBERT_START_DOCSTRING = r""" The ALBERT model was proposed in
|
|
|
|
ALBERT_START_DOCSTRING = r"""
|
|
|
|
`ALBERT: A Lite BERT for Self-supervised Learning of Language Representations`_
|
|
|
|
This model is a `tf.keras.Model <https://www.tensorflow.org/api_docs/python/tf/keras/Model>`__ sub-class.
|
|
|
|
by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. It presents
|
|
|
|
Use it as a regular TF 2.0 Keras Model and
|
|
|
|
two parameter-reduction techniques to lower memory consumption and increase the trainig speed of BERT.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This model is a tf.keras.Model `tf.keras.Model`_ sub-class. Use it as a regular TF 2.0 Keras Model and
|
|
|
|
|
|
|
|
refer to the TF 2.0 documentation for all matter related to general usage and behavior.
|
|
|
|
refer to the TF 2.0 documentation for all matter related to general usage and behavior.
|
|
|
|
|
|
|
|
|
|
|
|
.. _`ALBERT: A Lite BERT for Self-supervised Learning of Language Representations`:
|
|
|
|
.. _`ALBERT: A Lite BERT for Self-supervised Learning of Language Representations`:
|
|
|
|
@@ -492,108 +489,77 @@ ALBERT_START_DOCSTRING = r""" The ALBERT model was proposed in
|
|
|
|
.. _`tf.keras.Model`:
|
|
|
|
.. _`tf.keras.Model`:
|
|
|
|
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Model
|
|
|
|
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Model
|
|
|
|
|
|
|
|
|
|
|
|
Note on the model inputs:
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
|
|
TF 2.0 models accepts two formats as inputs:
|
|
|
|
TF 2.0 models accepts two formats as inputs:
|
|
|
|
|
|
|
|
|
|
|
|
- having all inputs as keyword arguments (like PyTorch models), or
|
|
|
|
- having all inputs as keyword arguments (like PyTorch models), or
|
|
|
|
- having all inputs as a list, tuple or dict in the first positional arguments.
|
|
|
|
- having all inputs as a list, tuple or dict in the first positional arguments.
|
|
|
|
|
|
|
|
|
|
|
|
This second option is usefull when using `tf.keras.Model.fit()` method which currently requires having all the tensors in the first argument of the model call function: `model(inputs)`.
|
|
|
|
This second option is usefull when using :obj:`tf.keras.Model.fit()` method which currently requires having
|
|
|
|
|
|
|
|
all the tensors in the first argument of the model call function: :obj:`model(inputs)`.
|
|
|
|
|
|
|
|
|
|
|
|
If you choose this second option, there are three possibilities you can use to gather all the input Tensors in the first positional argument :
|
|
|
|
If you choose this second option, there are three possibilities you can use to gather all the input Tensors
|
|
|
|
|
|
|
|
in the first positional argument :
|
|
|
|
|
|
|
|
|
|
|
|
- a single Tensor with input_ids only and nothing else: `model(inputs_ids)
|
|
|
|
- a single Tensor with input_ids only and nothing else: :obj:`model(inputs_ids)`
|
|
|
|
- a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
|
|
|
|
- a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
|
|
|
|
`model([input_ids, attention_mask])` or `model([input_ids, attention_mask, token_type_ids])`
|
|
|
|
:obj:`model([input_ids, attention_mask])` or :obj:`model([input_ids, attention_mask, token_type_ids])`
|
|
|
|
- a dictionary with one or several input Tensors associaed to the input names given in the docstring:
|
|
|
|
- a dictionary with one or several input Tensors associated to the input names given in the docstring:
|
|
|
|
`model({'input_ids': input_ids, 'token_type_ids': token_type_ids})`
|
|
|
|
:obj:`model({'input_ids': input_ids, 'token_type_ids': token_type_ids})`
|
|
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
Args:
|
|
|
|
config (:class:`~transformers.AlbertConfig`): Model configuration class with all the parameters of the model.
|
|
|
|
config (:class:`~transformers.AlbertConfig`): Model configuration class with all the parameters of the model.
|
|
|
|
Initializing with a config file does not load the weights associated with the model, only the configuration.
|
|
|
|
Initializing with a config file does not load the weights associated with the model, only the configuration.
|
|
|
|
Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights.
|
|
|
|
Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
ALBERT_INPUTS_DOCSTRING = r"""
|
|
|
|
ALBERT_INPUTS_DOCSTRING = r"""
|
|
|
|
Inputs:
|
|
|
|
Args:
|
|
|
|
**input_ids**: ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, sequence_length)``:
|
|
|
|
input_ids (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`):
|
|
|
|
Indices of input sequence tokens in the vocabulary.
|
|
|
|
Indices of input sequence tokens in the vocabulary.
|
|
|
|
To match pre-training, ALBERT input sequence should be formatted with [CLS] and [SEP] tokens as follows:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(a) For sequence pairs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``token_type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(b) For single sequences:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``tokens: [CLS] the dog is hairy . [SEP]``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``token_type_ids: 0 0 0 0 0 0 0``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Albert is a model with absolute position embeddings so it's usually advised to pad the inputs on
|
|
|
|
|
|
|
|
the right rather than the left.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Indices can be obtained using :class:`transformers.AlbertTokenizer`.
|
|
|
|
Indices can be obtained using :class:`transformers.AlbertTokenizer`.
|
|
|
|
See :func:`transformers.PreTrainedTokenizer.encode` and
|
|
|
|
See :func:`transformers.PreTrainedTokenizer.encode` and
|
|
|
|
:func:`transformers.PreTrainedTokenizer.convert_tokens_to_ids` for details.
|
|
|
|
:func:`transformers.PreTrainedTokenizer.encode_plus` for details.
|
|
|
|
**attention_mask**: (`optional`) ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, sequence_length)``:
|
|
|
|
|
|
|
|
|
|
|
|
`What are input IDs? <../glossary.html#input-ids>`__
|
|
|
|
|
|
|
|
attention_mask (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`, `optional, defaults to :obj:`None`):
|
|
|
|
Mask to avoid performing attention on padding token indices.
|
|
|
|
Mask to avoid performing attention on padding token indices.
|
|
|
|
Mask values selected in ``[0, 1]``:
|
|
|
|
Mask values selected in ``[0, 1]``:
|
|
|
|
``1`` for tokens that are NOT MASKED, ``0`` for MASKED tokens.
|
|
|
|
``1`` for tokens that are NOT MASKED, ``0`` for MASKED tokens.
|
|
|
|
**token_type_ids**: (`optional`) ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, sequence_length)``:
|
|
|
|
|
|
|
|
|
|
|
|
`What are attention masks? <../glossary.html#attention-mask>`__
|
|
|
|
|
|
|
|
token_type_ids (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
|
|
|
|
Segment token indices to indicate first and second portions of the inputs.
|
|
|
|
Segment token indices to indicate first and second portions of the inputs.
|
|
|
|
Indices are selected in ``[0, 1]``: ``0`` corresponds to a `sentence A` token, ``1``
|
|
|
|
Indices are selected in ``[0, 1]``: ``0`` corresponds to a `sentence A` token, ``1``
|
|
|
|
corresponds to a `sentence B` token
|
|
|
|
corresponds to a `sentence B` token
|
|
|
|
(see `ALBERT: Pre-training of Deep Bidirectional Transformers for Language Understanding`_ for more details).
|
|
|
|
|
|
|
|
**position_ids**: (`optional`) ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, sequence_length)``:
|
|
|
|
`What are token type IDs? <../glossary.html#token-type-ids>`_
|
|
|
|
|
|
|
|
position_ids (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`):
|
|
|
|
Indices of positions of each input sequence tokens in the position embeddings.
|
|
|
|
Indices of positions of each input sequence tokens in the position embeddings.
|
|
|
|
Selected in the range ``[0, config.max_position_embeddings - 1]``.
|
|
|
|
Selected in the range ``[0, config.max_position_embeddings - 1]``.
|
|
|
|
**head_mask**: (`optional`) ``Numpy array`` or ``tf.Tensor`` of shape ``(num_heads,)`` or ``(num_layers, num_heads)``:
|
|
|
|
|
|
|
|
|
|
|
|
`What are position IDs? <../glossary.html#position-ids>`_
|
|
|
|
|
|
|
|
head_mask (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`, defaults to :obj:`None`):
|
|
|
|
Mask to nullify selected heads of the self-attention modules.
|
|
|
|
Mask to nullify selected heads of the self-attention modules.
|
|
|
|
Mask values selected in ``[0, 1]``:
|
|
|
|
Mask values selected in ``[0, 1]``:
|
|
|
|
``1`` indicates the head is **not masked**, ``0`` indicates the head is **masked**.
|
|
|
|
``1`` indicates the head is **not masked**, ``0`` indicates the head is **masked**.
|
|
|
|
|
|
|
|
input_embeds (:obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`, defaults to :obj:`None`):
|
|
|
|
|
|
|
|
Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation.
|
|
|
|
|
|
|
|
This is useful if you want more control over how to convert `input_ids` indices into associated vectors
|
|
|
|
|
|
|
|
than the model's internal embedding lookup matrix.
|
|
|
|
|
|
|
|
training (:obj:`boolean`, `optional`, defaults to :obj:`False`):
|
|
|
|
|
|
|
|
Whether to activate dropout modules (if set to :obj:`True`) during training or to de-activate them
|
|
|
|
|
|
|
|
(if set to :obj:`False`) for evaluation.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@add_start_docstrings(
|
|
|
|
@add_start_docstrings(
|
|
|
|
"The bare Albert Model transformer outputing raw hidden-states without any specific head on top.",
|
|
|
|
"The bare Albert Model transformer outputing raw hidden-states without any specific head on top.",
|
|
|
|
ALBERT_START_DOCSTRING,
|
|
|
|
ALBERT_START_DOCSTRING,
|
|
|
|
ALBERT_INPUTS_DOCSTRING,
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
class TFAlbertModel(TFAlbertPreTrainedModel):
|
|
|
|
class TFAlbertModel(TFAlbertPreTrainedModel):
|
|
|
|
r"""
|
|
|
|
|
|
|
|
Outputs: `Tuple` comprising various elements depending on the configuration (config) and inputs:
|
|
|
|
|
|
|
|
**last_hidden_state**: ``tf.Tensor`` of shape ``(batch_size, sequence_length, hidden_size)``
|
|
|
|
|
|
|
|
Sequence of hidden-states at the output of the last layer of the model.
|
|
|
|
|
|
|
|
**pooler_output**: ``tf.Tensor`` of shape ``(batch_size, hidden_size)``
|
|
|
|
|
|
|
|
Last layer hidden-state of the first token of the sequence (classification token)
|
|
|
|
|
|
|
|
further processed by a Linear layer and a Tanh activation function. The Linear
|
|
|
|
|
|
|
|
layer weights are trained from the next sentence prediction (classification)
|
|
|
|
|
|
|
|
objective during Albert pretraining. This output is usually *not* a good summary
|
|
|
|
|
|
|
|
of the semantic content of the input, you're often better with averaging or pooling
|
|
|
|
|
|
|
|
the sequence of hidden-states for the whole input sequence.
|
|
|
|
|
|
|
|
**hidden_states**: (`optional`, returned when ``config.output_hidden_states=True``)
|
|
|
|
|
|
|
|
list of ``tf.Tensor`` (one for the output of each layer + the output of the embeddings)
|
|
|
|
|
|
|
|
of shape ``(batch_size, sequence_length, hidden_size)``:
|
|
|
|
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
|
|
|
|
**attentions**: (`optional`, returned when ``config.output_attentions=True``)
|
|
|
|
|
|
|
|
list of ``tf.Tensor`` (one for each layer) of shape ``(batch_size, num_heads, sequence_length, sequence_length)``:
|
|
|
|
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Examples::
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
|
|
|
from transformers import AlbertTokenizer, TFAlbertModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = AlbertTokenizer.from_pretrained('albert-base-v1')
|
|
|
|
|
|
|
|
model = TFAlbertModel.from_pretrained('albert-base-v1')
|
|
|
|
|
|
|
|
input_ids = tf.constant(tokenizer.encode("Hello, my dog is cute"))[None, :] # Batch size 1
|
|
|
|
|
|
|
|
outputs = model(input_ids)
|
|
|
|
|
|
|
|
last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config, **kwargs):
|
|
|
|
def __init__(self, config, **kwargs):
|
|
|
|
super().__init__(config, **kwargs)
|
|
|
|
super().__init__(config, **kwargs)
|
|
|
|
@@ -621,6 +587,7 @@ class TFAlbertModel(TFAlbertPreTrainedModel):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@add_start_docstrings_to_callable(ALBERT_INPUTS_DOCSTRING)
|
|
|
|
def call(
|
|
|
|
def call(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
|
inputs,
|
|
|
|
inputs,
|
|
|
|
@@ -631,6 +598,41 @@ class TFAlbertModel(TFAlbertPreTrainedModel):
|
|
|
|
inputs_embeds=None,
|
|
|
|
inputs_embeds=None,
|
|
|
|
training=False,
|
|
|
|
training=False,
|
|
|
|
):
|
|
|
|
):
|
|
|
|
|
|
|
|
r"""
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
:obj:`tuple(tf.Tensor)` comprising various elements depending on the configuration (:class:`~transformers.AlbertConfig`) and inputs:
|
|
|
|
|
|
|
|
last_hidden_state (:obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`):
|
|
|
|
|
|
|
|
Sequence of hidden-states at the output of the last layer of the model.
|
|
|
|
|
|
|
|
pooler_output (:obj:`tf.Tensor` of shape :obj:`(batch_size, hidden_size)`):
|
|
|
|
|
|
|
|
Last layer hidden-state of the first token of the sequence (classification token)
|
|
|
|
|
|
|
|
further processed by a Linear layer and a Tanh activation function. The Linear
|
|
|
|
|
|
|
|
layer weights are trained from the next sentence prediction (classification)
|
|
|
|
|
|
|
|
objective during Albert pretraining. This output is usually *not* a good summary
|
|
|
|
|
|
|
|
of the semantic content of the input, you're often better with averaging or pooling
|
|
|
|
|
|
|
|
the sequence of hidden-states for the whole input sequence.
|
|
|
|
|
|
|
|
hidden_states (:obj:`tuple(tf.Tensor)`, `optional`, returned when :obj:`config.output_hidden_states=True`):
|
|
|
|
|
|
|
|
tuple of :obj:`tf.Tensor` (one for the output of the embeddings + one for the output of each layer)
|
|
|
|
|
|
|
|
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
|
|
|
|
attentions (:obj:`tuple(tf.Tensor)`, `optional`, returned when ``config.output_attentions=True``):
|
|
|
|
|
|
|
|
tuple of :obj:`tf.Tensor` (one for each layer) of shape
|
|
|
|
|
|
|
|
:obj:`(batch_size, num_heads, sequence_length, sequence_length)`:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Examples::
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
|
|
|
from transformers import AlbertTokenizer, TFAlbertModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
|
|
|
|
|
|
|
|
model = TFAlbertModel.from_pretrained('albert-base-v2')
|
|
|
|
|
|
|
|
input_ids = tf.constant(tokenizer.encode("Hello, my dog is cute"))[None, :] # Batch size 1
|
|
|
|
|
|
|
|
outputs = model(input_ids)
|
|
|
|
|
|
|
|
last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
if isinstance(inputs, (tuple, list)):
|
|
|
|
if isinstance(inputs, (tuple, list)):
|
|
|
|
input_ids = inputs[0]
|
|
|
|
input_ids = inputs[0]
|
|
|
|
attention_mask = inputs[1] if len(inputs) > 1 else attention_mask
|
|
|
|
attention_mask = inputs[1] if len(inputs) > 1 else attention_mask
|
|
|
|
@@ -704,19 +706,35 @@ class TFAlbertModel(TFAlbertPreTrainedModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@add_start_docstrings(
|
|
|
|
@add_start_docstrings(
|
|
|
|
"""Albert Model with a `language modeling` head on top. """, ALBERT_START_DOCSTRING, ALBERT_INPUTS_DOCSTRING
|
|
|
|
"""Albert Model with a `language modeling` head on top. """, ALBERT_START_DOCSTRING
|
|
|
|
)
|
|
|
|
)
|
|
|
|
class TFAlbertForMaskedLM(TFAlbertPreTrainedModel):
|
|
|
|
class TFAlbertForMaskedLM(TFAlbertPreTrainedModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config, *inputs, **kwargs):
|
|
|
|
|
|
|
|
super(TFAlbertForMaskedLM, self).__init__(config, *inputs, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.albert = TFAlbertModel(config, name="albert")
|
|
|
|
|
|
|
|
self.predictions = TFAlbertMLMHead(config, self.albert.embeddings, name="predictions")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_output_embeddings(self):
|
|
|
|
|
|
|
|
return self.albert.embeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@add_start_docstrings_to_callable(ALBERT_INPUTS_DOCSTRING)
|
|
|
|
|
|
|
|
def call(self, inputs, **kwargs):
|
|
|
|
r"""
|
|
|
|
r"""
|
|
|
|
Outputs: `Tuple` comprising various elements depending on the configuration (config) and inputs:
|
|
|
|
Returns:
|
|
|
|
**prediction_scores**: ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, sequence_length, config.vocab_size)``
|
|
|
|
:obj:`tuple(tf.Tensor)` comprising various elements depending on the configuration (config) and inputs:
|
|
|
|
|
|
|
|
prediction_scores (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`
|
|
|
|
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
|
|
|
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
|
|
|
**hidden_states**: (`optional`, returned when ``config.output_hidden_states=True``)
|
|
|
|
hidden_states (:obj:`tuple(tf.Tensor)`, `optional`, returned when :obj:`config.output_hidden_states=True`):
|
|
|
|
list of ``Numpy array`` or ``tf.Tensor`` (one for the output of each layer + the output of the embeddings)
|
|
|
|
tuple of :obj:`tf.Tensor` (one for the output of the embeddings + one for the output of each layer)
|
|
|
|
of shape ``(batch_size, sequence_length, hidden_size)``:
|
|
|
|
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
|
|
|
|
|
|
|
|
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
**attentions**: (`optional`, returned when ``config.output_attentions=True``)
|
|
|
|
attentions (:obj:`tuple(tf.Tensor)`, `optional`, returned when ``config.output_attentions=True``):
|
|
|
|
list of ``Numpy array`` or ``tf.Tensor`` (one for each layer) of shape ``(batch_size, num_heads, sequence_length, sequence_length)``:
|
|
|
|
tuple of :obj:`tf.Tensor` (one for each layer) of shape
|
|
|
|
|
|
|
|
:obj:`(batch_size, num_heads, sequence_length, sequence_length)`:
|
|
|
|
|
|
|
|
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
|
|
|
|
|
|
|
|
Examples::
|
|
|
|
Examples::
|
|
|
|
@@ -731,17 +749,6 @@ class TFAlbertForMaskedLM(TFAlbertPreTrainedModel):
|
|
|
|
prediction_scores = outputs[0]
|
|
|
|
prediction_scores = outputs[0]
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config, *inputs, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(config, *inputs, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.albert = TFAlbertModel(config, name="albert")
|
|
|
|
|
|
|
|
self.predictions = TFAlbertMLMHead(config, self.albert.embeddings, name="predictions")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_output_embeddings(self):
|
|
|
|
|
|
|
|
return self.albert.embeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def call(self, inputs, **kwargs):
|
|
|
|
|
|
|
|
outputs = self.albert(inputs, **kwargs)
|
|
|
|
outputs = self.albert(inputs, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
sequence_output = outputs[0]
|
|
|
|
sequence_output = outputs[0]
|
|
|
|
@@ -757,19 +764,35 @@ class TFAlbertForMaskedLM(TFAlbertPreTrainedModel):
|
|
|
|
"""Albert Model transformer with a sequence classification/regression head on top (a linear layer on top of
|
|
|
|
"""Albert Model transformer with a sequence classification/regression head on top (a linear layer on top of
|
|
|
|
the pooled output) e.g. for GLUE tasks. """,
|
|
|
|
the pooled output) e.g. for GLUE tasks. """,
|
|
|
|
ALBERT_START_DOCSTRING,
|
|
|
|
ALBERT_START_DOCSTRING,
|
|
|
|
ALBERT_INPUTS_DOCSTRING,
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
class TFAlbertForSequenceClassification(TFAlbertPreTrainedModel):
|
|
|
|
class TFAlbertForSequenceClassification(TFAlbertPreTrainedModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config, *inputs, **kwargs):
|
|
|
|
|
|
|
|
super(TFAlbertForSequenceClassification, self).__init__(config, *inputs, **kwargs)
|
|
|
|
|
|
|
|
self.num_labels = config.num_labels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.albert = TFAlbertModel(config, name="albert")
|
|
|
|
|
|
|
|
self.dropout = tf.keras.layers.Dropout(config.hidden_dropout_prob)
|
|
|
|
|
|
|
|
self.classifier = tf.keras.layers.Dense(
|
|
|
|
|
|
|
|
config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="classifier"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@add_start_docstrings_to_callable(ALBERT_INPUTS_DOCSTRING)
|
|
|
|
|
|
|
|
def call(self, inputs, **kwargs):
|
|
|
|
r"""
|
|
|
|
r"""
|
|
|
|
Outputs: `Tuple` comprising various elements depending on the configuration (config) and inputs:
|
|
|
|
Returns:
|
|
|
|
**logits**: ``Numpy array`` or ``tf.Tensor`` of shape ``(batch_size, config.num_labels)``
|
|
|
|
:obj:`tuple(tf.Tensor)` comprising various elements depending on the configuration (:obj:`~transformers.AlbertConfig`) and inputs:
|
|
|
|
|
|
|
|
logits (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, config.num_labels)`)
|
|
|
|
Classification (or regression if config.num_labels==1) scores (before SoftMax).
|
|
|
|
Classification (or regression if config.num_labels==1) scores (before SoftMax).
|
|
|
|
**hidden_states**: (`optional`, returned when ``config.output_hidden_states=True``)
|
|
|
|
hidden_states (:obj:`tuple(tf.Tensor)`, `optional`, returned when :obj:`config.output_hidden_states=True`):
|
|
|
|
list of ``Numpy array`` or ``tf.Tensor`` (one for the output of each layer + the output of the embeddings)
|
|
|
|
tuple of :obj:`tf.Tensor` (one for the output of the embeddings + one for the output of each layer)
|
|
|
|
of shape ``(batch_size, sequence_length, hidden_size)``:
|
|
|
|
of shape :obj:`(batch_size, sequence_length, hidden_size)`.
|
|
|
|
|
|
|
|
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
Hidden-states of the model at the output of each layer plus the initial embedding outputs.
|
|
|
|
**attentions**: (`optional`, returned when ``config.output_attentions=True``)
|
|
|
|
attentions (:obj:`tuple(tf.Tensor)`, `optional`, returned when ``config.output_attentions=True``):
|
|
|
|
list of ``Numpy array`` or ``tf.Tensor`` (one for each layer) of shape ``(batch_size, num_heads, sequence_length, sequence_length)``:
|
|
|
|
tuple of :obj:`tf.Tensor` (one for each layer) of shape
|
|
|
|
|
|
|
|
:obj:`(batch_size, num_heads, sequence_length, sequence_length)`:
|
|
|
|
|
|
|
|
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
|
|
|
|
|
|
|
|
|
|
|
|
Examples::
|
|
|
|
Examples::
|
|
|
|
@@ -784,18 +807,6 @@ class TFAlbertForSequenceClassification(TFAlbertPreTrainedModel):
|
|
|
|
logits = outputs[0]
|
|
|
|
logits = outputs[0]
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config, *inputs, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(config, *inputs, **kwargs)
|
|
|
|
|
|
|
|
self.num_labels = config.num_labels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.albert = TFAlbertModel(config, name="albert")
|
|
|
|
|
|
|
|
self.dropout = tf.keras.layers.Dropout(config.hidden_dropout_prob)
|
|
|
|
|
|
|
|
self.classifier = tf.keras.layers.Dense(
|
|
|
|
|
|
|
|
config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="classifier"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def call(self, inputs, **kwargs):
|
|
|
|
|
|
|
|
outputs = self.albert(inputs, **kwargs)
|
|
|
|
outputs = self.albert(inputs, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
pooled_output = outputs[1]
|
|
|
|
pooled_output = outputs[1]
|
|
|
|
|