File size: 4,403 Bytes
5672777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2023 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.

"""Tests for Keras-based masked softmax layer."""

import numpy as np
import tensorflow as tf, tf_keras

from official.nlp.modeling.layers import masked_softmax


class MaskedSoftmaxLayerTest(tf.test.TestCase):

  def test_non_masked_softmax(self):
    test_layer = masked_softmax.MaskedSoftmax()
    input_tensor = tf_keras.Input(shape=(4, 8))
    output = test_layer(input_tensor)
    model = tf_keras.Model(input_tensor, output)

    input_data = 10 * np.random.random_sample((3, 4, 8))
    output_data = model.predict(input_data)
    expected_data = tf.nn.softmax(input_data)
    self.assertAllClose(expected_data, output_data)

  def test_masked_softmax(self):
    test_layer = masked_softmax.MaskedSoftmax()
    input_tensor = tf_keras.Input(shape=(4, 8))
    mask_tensor = tf_keras.Input(shape=(4, 8))
    output = test_layer(input_tensor, mask_tensor)
    model = tf_keras.Model([input_tensor, mask_tensor], output)

    input_data = 10 * np.random.random_sample((3, 4, 8))
    mask_data = np.random.randint(2, size=(3, 4, 8))

    output_data = model.predict([input_data, mask_data])
    expected_zeros = np.greater(mask_data, 0)
    is_zeros = np.greater(output_data, 0)
    self.assertAllEqual(expected_zeros, is_zeros)

  def test_masked_softmax_with_none_mask(self):
    test_layer = masked_softmax.MaskedSoftmax()
    input_tensor = tf_keras.Input(shape=(4, 8))
    output = test_layer(input_tensor, None)
    model = tf_keras.Model(input_tensor, output)

    input_data = 10 * np.random.random_sample((3, 4, 8))
    output_data = model.predict(input_data)
    expected_data = tf.nn.softmax(input_data)
    self.assertAllClose(expected_data, output_data)

  def test_softmax_with_axes_expansion(self):
    test_layer = masked_softmax.MaskedSoftmax(mask_expansion_axes=[1])
    input_tensor = tf_keras.Input(shape=(4, 8))
    mask_tensor = tf_keras.Input(shape=(8))
    output = test_layer(input_tensor, mask_tensor)
    model = tf_keras.Model([input_tensor, mask_tensor], output)

    input_data = 10 * np.random.random_sample((3, 4, 8))
    mask_data = np.random.randint(2, size=(3, 8))

    output_data = model.predict([input_data, mask_data])
    expanded_mask = np.expand_dims(mask_data, axis=1) * np.ones_like(input_data)
    expected_zeros = np.greater(expanded_mask, 0)
    is_zeros = np.greater(output_data, 0)
    self.assertAllEqual(expected_zeros, is_zeros)

  def test_masked_softmax_high_dims(self):
    test_layer = masked_softmax.MaskedSoftmax(
        mask_expansion_axes=[1], normalization_axes=[6, 7])
    input_shape = [2, 3, 4, 5, 6, 7, 8]
    mask_shape = [5, 6, 7, 8]
    input_tensor = tf_keras.Input(shape=input_shape)
    mask_tensor = tf_keras.Input(shape=mask_shape)
    output = test_layer(input_tensor, mask_tensor)
    model = tf_keras.Model([input_tensor, mask_tensor], output)

    input_data = 10 * np.random.random_sample([3] + input_shape)
    mask_data = np.random.randint(2, size=[3] + mask_shape)

    output_data = model.predict([input_data, mask_data])
    expanded_mask = np.expand_dims(mask_data, axis=1)
    expanded_mask = np.expand_dims(expanded_mask, axis=1)
    expanded_mask = np.expand_dims(
        expanded_mask, axis=1) * np.ones_like(input_data)
    expected_zeros = np.greater(expanded_mask, 0)
    is_zeros = np.greater(output_data, 0)
    self.assertAllEqual(expected_zeros, is_zeros)

  def test_serialize_deserialize(self):
    test_layer = masked_softmax.MaskedSoftmax(
        mask_expansion_axes=[1], normalization_axes=[6, 7])
    new_layer = masked_softmax.MaskedSoftmax.from_config(
        test_layer.get_config())

    # If the serialization was successful, the new config should match the old.
    self.assertAllEqual(test_layer.get_config(), new_layer.get_config())


if __name__ == '__main__':
  tf.test.main()