Address Patrick's comments.
This commit is contained in:
@@ -466,7 +466,7 @@ class TFAlbertMLMHead(tf.keras.layers.Layer):
|
||||
class TFAlbertMainLayer(tf.keras.layers.Layer):
|
||||
config_class = AlbertConfig
|
||||
|
||||
def __init__(self, config, **kwargs):
|
||||
def __init__(self, config, add_pooling_layer=True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.num_hidden_layers = config.num_hidden_layers
|
||||
self.output_attentions = config.output_attentions
|
||||
@@ -480,7 +480,7 @@ class TFAlbertMainLayer(tf.keras.layers.Layer):
|
||||
kernel_initializer=get_initializer(config.initializer_range),
|
||||
activation="tanh",
|
||||
name="pooler",
|
||||
)
|
||||
) if add_pooling_layer else None
|
||||
|
||||
def get_input_embeddings(self):
|
||||
return self.embeddings
|
||||
@@ -594,7 +594,7 @@ class TFAlbertMainLayer(tf.keras.layers.Layer):
|
||||
)
|
||||
|
||||
sequence_output = encoder_outputs[0]
|
||||
pooled_output = self.pooler(sequence_output[:, 0])
|
||||
pooled_output = self.pooler(sequence_output[:, 0]) if self.pooler is not None else None
|
||||
|
||||
if not return_dict:
|
||||
return (
|
||||
@@ -826,13 +826,12 @@ class TFAlbertSOPHead(tf.keras.layers.Layer):
|
||||
|
||||
@add_start_docstrings("""Albert Model with a `language modeling` head on top. """, ALBERT_START_DOCSTRING)
|
||||
class TFAlbertForMaskedLM(TFAlbertPreTrainedModel, TFMaskedLanguageModelingLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
self.albert = TFAlbertMainLayer(config, name="albert")
|
||||
self.albert = TFAlbertMainLayer(config, add_pooling_layer=False, name="albert")
|
||||
self.predictions = TFAlbertMLMHead(config, self.albert.embeddings, name="predictions")
|
||||
|
||||
def get_output_embeddings(self):
|
||||
@@ -994,14 +993,13 @@ class TFAlbertForSequenceClassification(TFAlbertPreTrainedModel, TFSequenceClass
|
||||
ALBERT_START_DOCSTRING,
|
||||
)
|
||||
class TFAlbertForTokenClassification(TFAlbertPreTrainedModel, TFTokenClassificationLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
self.num_labels = config.num_labels
|
||||
|
||||
self.albert = TFAlbertMainLayer(config, name="albert")
|
||||
self.albert = TFAlbertMainLayer(config, add_pooling_layer=False, 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"
|
||||
@@ -1079,14 +1077,13 @@ class TFAlbertForTokenClassification(TFAlbertPreTrainedModel, TFTokenClassificat
|
||||
ALBERT_START_DOCSTRING,
|
||||
)
|
||||
class TFAlbertForQuestionAnswering(TFAlbertPreTrainedModel, TFQuestionAnsweringLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
self.num_labels = config.num_labels
|
||||
|
||||
self.albert = TFAlbertMainLayer(config, name="albert")
|
||||
self.num_labels = config.num_labels
|
||||
self.albert = TFAlbertMainLayer(config, add_pooling_layer=False, name="albert")
|
||||
self.qa_outputs = tf.keras.layers.Dense(
|
||||
config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="qa_outputs"
|
||||
)
|
||||
|
||||
@@ -781,8 +781,6 @@ BERT_INPUTS_DOCSTRING = r"""
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertModel(TFBertPreTrainedModel):
|
||||
authorized_unexpected_keys = [r"nsp___cls"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
@@ -855,7 +853,7 @@ class TFBertForPreTraining(TFBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings("""Bert Model with a `language modeling` head on top. """, BERT_START_DOCSTRING)
|
||||
class TFBertForMaskedLM(TFBertPreTrainedModel, TFMaskedLanguageModelingLoss):
|
||||
authorized_unexpected_keys = [r"pooler", r"nsp___cls"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
@@ -939,7 +937,7 @@ class TFBertForMaskedLM(TFBertPreTrainedModel, TFMaskedLanguageModelingLoss):
|
||||
|
||||
|
||||
class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss):
|
||||
authorized_unexpected_keys = [r"pooler", r"nsp___cls"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
@@ -1027,9 +1025,7 @@ class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss):
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertForNextSentencePrediction(TFBertPreTrainedModel):
|
||||
authorized_unexpected_keys = [r"mlm___cls"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
self.bert = TFBertMainLayer(config, name="bert")
|
||||
@@ -1078,8 +1074,6 @@ class TFBertForNextSentencePrediction(TFBertPreTrainedModel):
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertForSequenceClassification(TFBertPreTrainedModel, TFSequenceClassificationLoss):
|
||||
authorized_unexpected_keys = [r"nsp___cls", r"mlm___cls"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
@@ -1163,8 +1157,6 @@ class TFBertForSequenceClassification(TFBertPreTrainedModel, TFSequenceClassific
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertForMultipleChoice(TFBertPreTrainedModel, TFMultipleChoiceLoss):
|
||||
authorized_unexpected_keys = [r"nsp___cls", r"mlm___cls"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
@@ -1291,7 +1283,7 @@ class TFBertForMultipleChoice(TFBertPreTrainedModel, TFMultipleChoiceLoss):
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertForTokenClassification(TFBertPreTrainedModel, TFTokenClassificationLoss):
|
||||
authorized_unexpected_keys = [r"pooler", r"nsp___cls", r"mlm___cls"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
@@ -1373,7 +1365,7 @@ class TFBertForTokenClassification(TFBertPreTrainedModel, TFTokenClassificationL
|
||||
BERT_START_DOCSTRING,
|
||||
)
|
||||
class TFBertForQuestionAnswering(TFBertPreTrainedModel, TFQuestionAnsweringLoss):
|
||||
authorized_unexpected_keys = [r"pooler", r"nsp___cls", r"mlm___cls"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
@@ -1225,7 +1225,7 @@ class TFLongformerEncoder(tf.keras.layers.Layer):
|
||||
class TFLongformerMainLayer(tf.keras.layers.Layer):
|
||||
config_class = LongformerConfig
|
||||
|
||||
def __init__(self, config, **kwargs):
|
||||
def __init__(self, config, add_pooling_layer=True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
if isinstance(config.attention_window, int):
|
||||
@@ -1247,7 +1247,7 @@ class TFLongformerMainLayer(tf.keras.layers.Layer):
|
||||
self.attention_window = config.attention_window
|
||||
self.embeddings = TFLongformerEmbeddings(config, name="embeddings")
|
||||
self.encoder = TFLongformerEncoder(config, name="encoder")
|
||||
self.pooler = TFLongformerPooler(config, name="pooler")
|
||||
self.pooler = TFLongformerPooler(config, name="pooler") if add_pooling_layer else None
|
||||
|
||||
def get_input_embeddings(self):
|
||||
return self.embeddings
|
||||
@@ -1371,7 +1371,7 @@ class TFLongformerMainLayer(tf.keras.layers.Layer):
|
||||
training=training,
|
||||
)
|
||||
sequence_output = encoder_outputs[0]
|
||||
pooled_output = self.pooler(sequence_output)
|
||||
pooled_output = self.pooler(sequence_output) if self.pooler is not None else None
|
||||
|
||||
# undo padding
|
||||
if padding_len > 0:
|
||||
@@ -1618,13 +1618,12 @@ class TFLongformerModel(TFLongformerPreTrainedModel):
|
||||
LONGFORMER_START_DOCSTRING,
|
||||
)
|
||||
class TFLongformerForMaskedLM(TFLongformerPreTrainedModel, TFMaskedLanguageModelingLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
authorized_unexpected_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
self.longformer = TFLongformerMainLayer(config, name="longformer")
|
||||
self.longformer = TFLongformerMainLayer(config, add_pooling_layer=False, name="longformer")
|
||||
self.lm_head = TFLongformerLMHead(config, self.longformer.embeddings, name="lm_head")
|
||||
|
||||
def get_output_embeddings(self):
|
||||
@@ -1703,14 +1702,13 @@ class TFLongformerForMaskedLM(TFLongformerPreTrainedModel, TFMaskedLanguageModel
|
||||
LONGFORMER_START_DOCSTRING,
|
||||
)
|
||||
class TFLongformerForQuestionAnswering(TFLongformerPreTrainedModel, TFQuestionAnsweringLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
self.num_labels = config.num_labels
|
||||
self.longformer = TFLongformerMainLayer(config, name="longformer")
|
||||
self.longformer = TFLongformerMainLayer(config, add_pooling_layer=False, name="longformer")
|
||||
self.qa_outputs = tf.keras.layers.Dense(
|
||||
config.num_labels,
|
||||
kernel_initializer=get_initializer(config.initializer_range),
|
||||
|
||||
@@ -679,7 +679,7 @@ class TFMobileBertMLMHead(tf.keras.layers.Layer):
|
||||
class TFMobileBertMainLayer(tf.keras.layers.Layer):
|
||||
config_class = MobileBertConfig
|
||||
|
||||
def __init__(self, config, **kwargs):
|
||||
def __init__(self, config, add_pooling_layer=True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.num_hidden_layers = config.num_hidden_layers
|
||||
self.output_attentions = config.output_attentions
|
||||
@@ -688,7 +688,7 @@ class TFMobileBertMainLayer(tf.keras.layers.Layer):
|
||||
|
||||
self.embeddings = TFMobileBertEmbeddings(config, name="embeddings")
|
||||
self.encoder = TFMobileBertEncoder(config, name="encoder")
|
||||
self.pooler = TFMobileBertPooler(config, name="pooler")
|
||||
self.pooler = TFMobileBertPooler(config, name="pooler") if add_pooling_layer else None
|
||||
|
||||
def get_input_embeddings(self):
|
||||
return self.embeddings
|
||||
@@ -797,7 +797,7 @@ class TFMobileBertMainLayer(tf.keras.layers.Layer):
|
||||
)
|
||||
|
||||
sequence_output = encoder_outputs[0]
|
||||
pooled_output = self.pooler(sequence_output)
|
||||
pooled_output = self.pooler(sequence_output) if self.pooler is not None else None
|
||||
|
||||
if not return_dict:
|
||||
return (
|
||||
@@ -1019,13 +1019,12 @@ class TFMobileBertForPreTraining(TFMobileBertPreTrainedModel):
|
||||
|
||||
@add_start_docstrings("""MobileBert Model with a `language modeling` head on top. """, MOBILEBERT_START_DOCSTRING)
|
||||
class TFMobileBertForMaskedLM(TFMobileBertPreTrainedModel, TFMaskedLanguageModelingLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
|
||||
self.mobilebert = TFMobileBertMainLayer(config, name="mobilebert")
|
||||
self.mobilebert = TFMobileBertMainLayer(config, add_pooling_layer=False, name="mobilebert")
|
||||
self.mlm = TFMobileBertMLMHead(config, name="mlm___cls")
|
||||
|
||||
def get_output_embeddings(self):
|
||||
@@ -1244,14 +1243,13 @@ class TFMobileBertForSequenceClassification(TFMobileBertPreTrainedModel, TFSeque
|
||||
MOBILEBERT_START_DOCSTRING,
|
||||
)
|
||||
class TFMobileBertForQuestionAnswering(TFMobileBertPreTrainedModel, TFQuestionAnsweringLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
self.num_labels = config.num_labels
|
||||
|
||||
self.mobilebert = TFMobileBertMainLayer(config, name="mobilebert")
|
||||
self.mobilebert = TFMobileBertMainLayer(config, add_pooling_layer=False, name="mobilebert")
|
||||
self.qa_outputs = tf.keras.layers.Dense(
|
||||
config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="qa_outputs"
|
||||
)
|
||||
@@ -1469,14 +1467,13 @@ class TFMobileBertForMultipleChoice(TFMobileBertPreTrainedModel, TFMultipleChoic
|
||||
MOBILEBERT_START_DOCSTRING,
|
||||
)
|
||||
class TFMobileBertForTokenClassification(TFMobileBertPreTrainedModel, TFTokenClassificationLoss):
|
||||
|
||||
authorized_missing_keys = [r"pooler"]
|
||||
|
||||
def __init__(self, config, *inputs, **kwargs):
|
||||
super().__init__(config, *inputs, **kwargs)
|
||||
self.num_labels = config.num_labels
|
||||
|
||||
self.mobilebert = TFMobileBertMainLayer(config, name="mobilebert")
|
||||
self.mobilebert = TFMobileBertMainLayer(config, add_pooling_layer=False, name="mobilebert")
|
||||
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"
|
||||
|
||||
@@ -24,6 +24,7 @@ import h5py
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from tensorflow.python.keras.saving import hdf5_format
|
||||
from tensorflow.python.keras import backend as K
|
||||
|
||||
from .configuration_utils import PretrainedConfig
|
||||
from .file_utils import DUMMY_INPUTS, TF2_WEIGHTS_NAME, WEIGHTS_NAME, cached_path, hf_bucket_url, is_remote_url
|
||||
@@ -265,8 +266,6 @@ def load_tf_weights(model, resolved_archive_file):
|
||||
resolved_archive_file (:obj:`str`):
|
||||
The location of the H5 file.
|
||||
"""
|
||||
from tensorflow.python.keras import backend as K
|
||||
|
||||
with h5py.File(resolved_archive_file, "r") as f:
|
||||
saved_layer_names = set(hdf5_format.load_attributes_from_hdf5_group(f, "layer_names"))
|
||||
weight_value_tuples = []
|
||||
|
||||
Reference in New Issue
Block a user