bert_classifier.py 4.49 KB
Newer Older
Hongkun Yu's avatar
Hongkun Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
15
"""BERT cls-token classifier."""
16
# pylint: disable=g-classes-have-attributes
Hongkun Yu's avatar
Hongkun Yu committed
17

Hongkun Yu's avatar
Hongkun Yu committed
18
import tensorflow as tf
Hongkun Yu's avatar
Hongkun Yu committed
19

Hongkun Yu's avatar
Hongkun Yu committed
20
from official.nlp.modeling import layers
Hongkun Yu's avatar
Hongkun Yu committed
21
22
23
24
25
26
27
28
29
30
31
32
33
from official.nlp.modeling import networks


@tf.keras.utils.register_keras_serializable(package='Text')
class BertClassifier(tf.keras.Model):
  """Classifier model based on a BERT-style transformer-based encoder.

  This is an implementation of the network structure surrounding a transformer
  encoder as described in "BERT: Pre-training of Deep Bidirectional Transformers
  for Language Understanding" (https://arxiv.org/abs/1810.04805).

  The BertClassifier allows a user to pass in a transformer stack, and
  instantiates a classification network based on the passed `num_classes`
34
  argument. If `num_classes` is set to 1, a regression network is instantiated.
Hongkun Yu's avatar
Hongkun Yu committed
35

36
37
38
  *Note* that the model is constructed by
  [Keras Functional API](https://keras.io/guides/functional_api/).

39
  Arguments:
Hongkun Yu's avatar
Hongkun Yu committed
40
41
42
43
44
45
    network: A transformer network. This network should output a sequence output
      and a classification output. Furthermore, it should expose its embedding
      table via a "get_embedding_table" method.
    num_classes: Number of classes to predict from the classification network.
    initializer: The initializer (if any) to use in the classification networks.
      Defaults to a Glorot uniform initializer.
Hongkun Yu's avatar
Hongkun Yu committed
46
    dropout_rate: The dropout probability of the cls head.
Hongkun Yu's avatar
Hongkun Yu committed
47
48
    use_encoder_pooler: Whether to use the pooler layer pre-defined inside the
      encoder.
Hongkun Yu's avatar
Hongkun Yu committed
49
50
51
52
53
54
55
  """

  def __init__(self,
               network,
               num_classes,
               initializer='glorot_uniform',
               dropout_rate=0.1,
Hongkun Yu's avatar
Hongkun Yu committed
56
               use_encoder_pooler=True,
Hongkun Yu's avatar
Hongkun Yu committed
57
58
               **kwargs):
    self._self_setattr_tracking = False
Hongkun Yu's avatar
Hongkun Yu committed
59
    self._network = network
Hongkun Yu's avatar
Hongkun Yu committed
60
61
62
63
    self._config = {
        'network': network,
        'num_classes': num_classes,
        'initializer': initializer,
Hongkun Yu's avatar
Hongkun Yu committed
64
        'use_encoder_pooler': use_encoder_pooler,
Hongkun Yu's avatar
Hongkun Yu committed
65
66
67
68
69
70
71
    }

    # We want to use the inputs of the passed network as the inputs to this
    # Model. To do this, we need to keep a handle to the network inputs for use
    # when we construct the Model object at the end of init.
    inputs = network.inputs

Hongkun Yu's avatar
Hongkun Yu committed
72
73
74
    if use_encoder_pooler:
      # Because we have a copy of inputs to create this Model object, we can
      # invoke the Network object with its own input tensors to start the Model.
75
76
77
78
79
      outputs = network(inputs)
      if isinstance(outputs, list):
        cls_output = outputs[1]
      else:
        cls_output = outputs['pooled_output']
Hongkun Yu's avatar
Hongkun Yu committed
80
      cls_output = tf.keras.layers.Dropout(rate=dropout_rate)(cls_output)
Hongkun Yu's avatar
Hongkun Yu committed
81

Hongkun Yu's avatar
Hongkun Yu committed
82
83
84
85
86
87
88
89
      self.classifier = networks.Classification(
          input_width=cls_output.shape[-1],
          num_classes=num_classes,
          initializer=initializer,
          output='logits',
          name='sentence_prediction')
      predictions = self.classifier(cls_output)
    else:
90
91
92
93
94
      outputs = network(inputs)
      if isinstance(outputs, list):
        sequence_output = outputs[0]
      else:
        sequence_output = outputs['sequence_output']
Hongkun Yu's avatar
Hongkun Yu committed
95
96
97
98
99
100
101
      self.classifier = layers.ClassificationHead(
          inner_dim=sequence_output.shape[-1],
          num_classes=num_classes,
          initializer=initializer,
          dropout_rate=dropout_rate,
          name='sentence_prediction')
      predictions = self.classifier(sequence_output)
Hongkun Yu's avatar
Hongkun Yu committed
102
103
104
105

    super(BertClassifier, self).__init__(
        inputs=inputs, outputs=predictions, **kwargs)

Hongkun Yu's avatar
Hongkun Yu committed
106
107
  @property
  def checkpoint_items(self):
Hongkun Yu's avatar
Hongkun Yu committed
108
109
110
111
112
    items = dict(encoder=self._network)
    if hasattr(self.classifier, 'checkpoint_items'):
      for key, item in self.classifier.checkpoint_items.items():
        items['.'.join([self.classifier.name, key])] = item
    return items
Hongkun Yu's avatar
Hongkun Yu committed
113

Hongkun Yu's avatar
Hongkun Yu committed
114
115
116
117
118
119
  def get_config(self):
    return self._config

  @classmethod
  def from_config(cls, config, custom_objects=None):
    return cls(**config)