classification.py 3.38 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
"""Classification and regression network."""
16
# pylint: disable=g-classes-have-attributes
Hongkun Yu's avatar
Hongkun Yu committed
17
18
19
20
21
22
23
24
25
from __future__ import absolute_import
from __future__ import division
# from __future__ import google_type_annotations
from __future__ import print_function

import tensorflow as tf


@tf.keras.utils.register_keras_serializable(package='Text')
26
class Classification(tf.keras.Model):
Hongkun Yu's avatar
Hongkun Yu committed
27
28
  """Classification network head for BERT modeling.

29
30
  This network implements a simple classifier head based on a dense layer. If
  num_classes is one, it can be considered as a regression problem.
Hongkun Yu's avatar
Hongkun Yu committed
31

32
33
34
  *Note* that the network is constructed by
  [Keras Functional API](https://keras.io/guides/functional_api/).

35
  Arguments:
Hongkun Yu's avatar
Hongkun Yu committed
36
    input_width: The innermost dimension of the input tensor to this network.
37
38
    num_classes: The number of classes that this network should classify to. If
      equal to 1, a regression problem is assumed.
Hongkun Yu's avatar
Hongkun Yu committed
39
    activation: The activation, if any, for the dense layer in this network.
Hongkun Yu's avatar
Hongkun Yu committed
40
41
    initializer: The initializer for the dense layer in this network. Defaults
      to a Glorot uniform initializer.
Hongkun Yu's avatar
Hongkun Yu committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    output: The output style for this network. Can be either 'logits' or
      'predictions'.
  """

  def __init__(self,
               input_width,
               num_classes,
               initializer='glorot_uniform',
               output='logits',
               **kwargs):
    self._self_setattr_tracking = False
    self._config_dict = {
        'input_width': input_width,
        'num_classes': num_classes,
        'initializer': initializer,
        'output': output,
    }

    cls_output = tf.keras.layers.Input(
        shape=(input_width,), name='cls_output', dtype=tf.float32)

    self.logits = tf.keras.layers.Dense(
        num_classes,
        activation=None,
        kernel_initializer=initializer,
        name='predictions/transform/logits')(
            cls_output)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
69
70
71
72
73
74
75

    policy = tf.keras.mixed_precision.experimental.global_policy()
    if policy.name == 'mixed_bfloat16':
      # b/158514794: bf16 is not stable with post-softmax cross-entropy.
      policy = tf.float32
    predictions = tf.keras.layers.Activation(tf.nn.log_softmax,
                                             dtype=policy)(self.logits)
Hongkun Yu's avatar
Hongkun Yu committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

    if output == 'logits':
      output_tensors = self.logits
    elif output == 'predictions':
      output_tensors = predictions
    else:
      raise ValueError(
          ('Unknown `output` value "%s". `output` can be either "logits" or '
           '"predictions"') % output)

    super(Classification, self).__init__(
        inputs=[cls_output], outputs=output_tensors, **kwargs)

  def get_config(self):
    return self._config_dict

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