forked from codeaudit/polyai-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_layers_test.py
233 lines (209 loc) · 9.54 KB
/
encoder_layers_test.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Unit tests for encoder_layers.py.
Copyright PolyAI Limited.
"""
import tensorflow as tf
import encoder_layers
_TEST_ENCODER = "testdata/tfhub_modules/encoder"
_TEST_EXTRA_CONTEXT_ENCODER = "testdata/tfhub_modules/extra_context_encoder"
class EncoderLayersTest(tf.test.TestCase):
def test_encode_sentences(self):
with self.test_session() as sess:
layer = encoder_layers.SentenceEncoderLayer(_TEST_ENCODER)
encodings = layer(
["hello world", "what's up?", "hello world",
"sentence 4"])
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
self.assertEqual(len(weights), len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
encodings_val = sess.run(encodings)
self.assertEqual(list(encodings_val.shape), [4, 3])
self.assertAllClose(encodings_val[0], encodings_val[2])
grads = tf.gradients(
[encodings] + layer.losses, layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[encodings] + layer.losses, layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_encode_contexts(self):
with self.test_session() as sess:
layer = encoder_layers.ContextEncoderLayer(_TEST_ENCODER)
encodings = layer(
["hello world", "what's up?", "hello world",
"sentence 4"])
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
self.assertEqual(len(weights), len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
encodings_val = sess.run(encodings)
self.assertEqual(list(encodings_val.shape), [4, 5])
self.assertAllClose(encodings_val[0], encodings_val[2])
grads = tf.gradients(
[encodings] + layer.losses, layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[encodings] + layer.losses, layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_encode_responses(self):
with self.test_session() as sess:
layer = encoder_layers.ResponseEncoderLayer(_TEST_ENCODER)
encodings = layer(
["hello world", "what's up?", "hello world",
"sentence 4"])
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
self.assertEqual(len(weights), len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
encodings_val = sess.run(encodings)
self.assertEqual(list(encodings_val.shape), [4, 5])
self.assertAllClose(encodings_val[0], encodings_val[2])
grads = tf.gradients(
[encodings] + layer.losses, layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[encodings] + layer.losses, layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_encode_contexts_and_responses(self):
with self.test_session() as sess:
layer = encoder_layers.ContextAndResponseEncoderLayer(
_TEST_ENCODER)
context_encodings, response_encodings = layer([
["context 1", "context 2"],
["response 1", "response 2", "response 3"],
])
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
# Plus one because the embedding regularization is applied for
# both context and response.
self.assertEqual(len(weights) + 1, len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
context_encodings_val = sess.run(context_encodings)
self.assertEqual(list(context_encodings_val.shape), [2, 5])
response_encodings_val = sess.run(response_encodings)
self.assertEqual(list(response_encodings_val.shape), [3, 5])
grads = tf.gradients(
[context_encodings, response_encodings] + layer.losses,
layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[context_encodings, response_encodings] + layer.losses,
layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_encode_contexts_and_responses_with_extra_contexts(self):
with self.test_session() as sess:
layer = encoder_layers.ContextAndResponseEncoderLayer(
_TEST_EXTRA_CONTEXT_ENCODER, uses_extra_context=True)
context_encodings, response_encodings = layer([
["context 1", "context 2"],
["extra context 1", "extra context 2"],
["response 1", "response 2", "response 3"],
])
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
# Plus two because the embedding regularization is applied for
# context, extra contexts, and response.
self.assertEqual(len(weights) + 2, len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
context_encodings_val = sess.run(context_encodings)
self.assertEqual(list(context_encodings_val.shape), [2, 5])
response_encodings_val = sess.run(response_encodings)
self.assertEqual(list(response_encodings_val.shape), [3, 5])
grads = tf.gradients(
[context_encodings, response_encodings] + layer.losses,
layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[context_encodings, response_encodings] + layer.losses,
layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_encode_to_contextualized_subwords(self):
with self.test_session() as sess:
layer = encoder_layers.ContextualizedSubwordsLayer(_TEST_ENCODER)
tokens, sequence_encodings = layer(
["contextualised subword sequence 1", "sequence encoding 2"]
)
weights = [
var for var in layer.trainable_variables
if "layer_norm" not in var.name
]
self.assertEqual(len(weights), len(layer.losses))
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
tokens_val = sess.run(tokens)
self.assertEqual(list(tokens_val.shape), [2, 26])
sequence_encodings_val = sess.run(sequence_encodings)
self.assertEqual(list(sequence_encodings_val.shape), [2, 26, 3])
grads = tf.gradients(
[sequence_encodings] + layer.losses,
layer.trainable_variables)
for grad in grads:
self.assertIsNotNone(grad)
non_grads = tf.gradients(
[sequence_encodings] + layer.losses,
layer.non_trainable_variables)
for grad in non_grads:
self.assertIsNone(grad)
def test_non_trainable(self):
with self.test_session() as sess:
layer = encoder_layers.ContextualizedSubwordsLayer(
_TEST_ENCODER, trainable=False)
tokens, sequence_encodings = layer(
["contextualised subword sequence 1", "sequence encoding 2"]
)
self.assertEqual(layer.trainable_variables, [])
self.assertEqual(layer.losses, [])
# check layer still works
sess.run([
tf.compat.v1.local_variables_initializer(),
tf.compat.v1.global_variables_initializer(),
tf.compat.v1.tables_initializer(),
])
tokens_val = sess.run(tokens)
self.assertEqual(list(tokens_val.shape), [2, 26])
sequence_encodings_val = sess.run(sequence_encodings)
self.assertEqual(list(sequence_encodings_val.shape), [2, 26, 3])
if __name__ == "__main__":
tf.test.main()