|
13 | 13 | # limitations under the License. |
14 | 14 | """Tests for Sinusoidal Positional encoding.""" |
15 | 15 |
|
16 | | - |
17 | 16 | import tensorflow as tf |
18 | 17 | from tensorflow import keras |
19 | 18 |
|
@@ -92,6 +91,84 @@ def test_output_correct_values(self): |
92 | 91 | self.assertAllClose(output[0, 0, :], expected_encoding_position_0) |
93 | 92 | self.assertAllClose(output[0, 3, :], expected_encoding_position_3) |
94 | 93 |
|
| 94 | + def test_ragged_tensor_with_3_dimensions(self): |
| 95 | + feature_size = 2 |
| 96 | + test_layer = sine_position_encoding.SinePositionEncoding() |
| 97 | + # Create a 3-dimensional ragged input (the first dimension is implicit). |
| 98 | + input_tensor = keras.Input( |
| 99 | + shape=(None, feature_size), dtype=tf.float32, ragged=True |
| 100 | + ) |
| 101 | + output_tensor = test_layer(input_tensor) |
| 102 | + model = keras.Model(input_tensor, output_tensor) |
| 103 | + |
| 104 | + input_data = tf.ragged.constant( |
| 105 | + [ |
| 106 | + [[1.0, 1.0], [1.0, 1.0]], |
| 107 | + [], |
| 108 | + [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]], |
| 109 | + [[1.0, 1.0]], |
| 110 | + ], |
| 111 | + ragged_rank=1, |
| 112 | + inner_shape=(2,), |
| 113 | + ) |
| 114 | + expected_output_data = tf.ragged.constant( |
| 115 | + [ |
| 116 | + [[0.0, 1.0], [0.84147096, 0.5403023]], |
| 117 | + [], |
| 118 | + [[0.0, 1.0], [0.84147096, 0.5403023], [0.9092974, -0.41614684]], |
| 119 | + [[0.0, 1.0]], |
| 120 | + ], |
| 121 | + ragged_rank=1, |
| 122 | + inner_shape=(2,), |
| 123 | + ) |
| 124 | + output_data = model.predict(input_data) |
| 125 | + self.assertAllClose(output_data, expected_output_data) |
| 126 | + |
| 127 | + def test_ragged_tensor_with_4_dimensions(self): |
| 128 | + feature_size = 2 |
| 129 | + test_layer = sine_position_encoding.SinePositionEncoding() |
| 130 | + # Create a 4-dimensional ragged input (the first dimension is implicit). |
| 131 | + input_tensor = keras.Input( |
| 132 | + shape=(None, None, feature_size), dtype=tf.float32, ragged=True |
| 133 | + ) |
| 134 | + output_tensor = test_layer(input_tensor) |
| 135 | + model = keras.Model(input_tensor, output_tensor) |
| 136 | + |
| 137 | + input_data = tf.ragged.constant( |
| 138 | + [ |
| 139 | + [ |
| 140 | + [[1.0, 1.0], [1.0, 1.0]], |
| 141 | + [], |
| 142 | + ], |
| 143 | + [ |
| 144 | + [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]], |
| 145 | + [[1.0, 1.0]], |
| 146 | + ], |
| 147 | + ], |
| 148 | + ragged_rank=2, |
| 149 | + inner_shape=(2,), |
| 150 | + ) |
| 151 | + expected_output_data = tf.ragged.constant( |
| 152 | + [ |
| 153 | + [ |
| 154 | + [[0.0, 1.0], [0.84147096, 0.5403023]], |
| 155 | + [], |
| 156 | + ], |
| 157 | + [ |
| 158 | + [ |
| 159 | + [0.0, 1.0], |
| 160 | + [0.84147096, 0.5403023], |
| 161 | + [0.9092974, -0.41614684], |
| 162 | + ], |
| 163 | + [[0.0, 1.0]], |
| 164 | + ], |
| 165 | + ], |
| 166 | + ragged_rank=2, |
| 167 | + inner_shape=(2,), |
| 168 | + ) |
| 169 | + output_data = model.predict(input_data) |
| 170 | + self.assertAllClose(output_data, expected_output_data) |
| 171 | + |
95 | 172 | def test_get_config_and_from_config(self): |
96 | 173 | pos_encoding = sine_position_encoding.SinePositionEncoding( |
97 | 174 | max_wavelength=1000, |
|
0 commit comments