-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautoencoders.py
More file actions
371 lines (303 loc) · 14.8 KB
/
autoencoders.py
File metadata and controls
371 lines (303 loc) · 14.8 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# pylint: disable=too-many-ancestors
from typing import Dict, Optional
import tensorflow as tf
from tensorflow.keras import layers, Model
from text2vec.models.components.text_inputs import TokenEmbed, Embed, Tokenizer
from text2vec.models.transformer import TransformerEncoder, TransformerDecoder
from text2vec.models.sequential import RecurrentEncoder, RecurrentDecoder
class TransformerAutoEncoder(Model):
"""Wrapper model class to combine the transformer based encoder-decoder training pipeline.
Parameters
----------
max_sequence_len : int
Longest sequence seen at training time.
embedding_size : int
Dimensionality of the word-embeddings.
token_hash : dict, optional
Token -> integer vocabulary lookup, by default None
vocab_size : int, optional
Size of the vocabulary. Set this if pre-computing token IDs to pass to the model, by default None
unknown_token : str, optional
The placeholder value for OOV terms, by default '<unk>'
sep : str, optional
Token separator by default ' '
input_drop_rate : float, optional
Value between 0 and 1.0, by default 0.
hidden_drop_rate : float, optional
Value between 0 and 1.0, by default 0.
Raises
------
ValueError
Raised if neither a vocab dictionary or a vocab size is provided.
Examples
--------
```python
import tensorflow as tf
from text2vec.autoencoders import TransformerAutoEncoder
lookup = {'string': 0, 'is': 1, 'example': 2, '<unk>': 3}
model = TransformerAutoEncoder(token_hash=lookup, max_sequence_len=10, embedding_size=16)
text = tf.constant(["sample string .", "this is a second example ."])
encoded, context_vectors = model(text)
```
"""
def __init__(self, max_sequence_len: int, embedding_size: int,
token_hash: Optional[dict] = None, vocab_size: Optional[int] = None,
unknown_token: str = '<unk>', sep: str = ' ',
input_drop_rate: float = 0, hidden_drop_rate: float = 0):
super().__init__()
if token_hash is None and vocab_size is None:
raise ValueError("Must provide either a dictionary mapping or a dictionary size if using token IDs")
params = dict(
max_sequence_len=max_sequence_len,
embedding_size=embedding_size,
input_drop_rate=input_drop_rate,
hidden_drop_rate=hidden_drop_rate
)
if token_hash is not None:
self.tokenizer = Tokenizer(sep)
self.embed_layer = TokenEmbed(
token_hash=token_hash,
embedding_size=embedding_size,
max_sequence_len=max_sequence_len,
unknown_token=unknown_token
)
else:
self.tokenizer = layers.Lambda(lambda x: x) # this is only for consistency, identity map
self.embed_layer = Embed(
vocab_size=vocab_size,
embedding_size=embedding_size,
max_sequence_len=max_sequence_len
)
self.encode_layer = TransformerEncoder(n_stacks=1, num_layers=8, **params)
self.decode_layer = TransformerDecoder(n_stacks=1, num_layers=8, **params)
def call(self, inputs, training: bool = False): # pylint: disable=missing-function-docstring
encoding_text = inputs[0]
decoding_text = inputs[1] if len(inputs) > 1 else encoding_text
encode_tokens = self.tokenizer(encoding_text)
x_embed, mask_encode, _ = self.embed_layer(encode_tokens, training=training)
x_encode, context = self.encode_layer(x_embed, mask=mask_encode, training=training)
decode_tokens = self.tokenizer(decoding_text)
x_decode, mask_decode, _ = self.embed_layer(decode_tokens[:, :-1]) # skip </s>
x_decode = self.decode_layer(
x_enc=x_encode,
x_dec=x_decode,
dec_mask=mask_decode,
context=context,
attention=self.encode_layer.attention,
training=training
)
return x_embed, x_decode, mask_decode, decode_tokens
def train_step(self, data): # pylint: disable=missing-function-docstring
with tf.GradientTape() as tape:
_, x_decode, mask_decode, decode_tokens = self(data, training=True)
targets = decode_tokens[:, 1:] # skip the <s> token with the slice on axis=1
if isinstance(self.embed_layer, TokenEmbed):
targets = tf.ragged.map_flat_values(self.embed_layer.table.lookup, targets)
targets = self.embed_layer.slicer(targets)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=tf.tensordot(x_decode, self.embed_layer.embeddings, axes=[2, 1]),
labels=targets.to_tensor(default_value=0)
)
loss = loss * mask_decode
loss = tf.reduce_mean(loss)
gradients = tape.gradient(loss, self.trainable_variables)
self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))
if hasattr(self.optimizer, 'learning_rate') and callable(self.optimizer.learning_rate):
return {"loss": loss, 'learning_rate': self.optimizer.learning_rate(self.optimizer.iterations)}
return {"loss": loss, 'learning_rate': self.optimizer.learning_rate}
def test_step(self, data): # pylint: disable=missing-function-docstring
_, x_decode, mask_decode, decode_tokens = self(data, training=False)
targets = decode_tokens[:, 1:] # skip the <s> token with the slice on axis=1
if isinstance(self.embed_layer, TokenEmbed):
targets = tf.ragged.map_flat_values(self.embed_layer.table.lookup, targets)
targets = self.embed_layer.slicer(targets)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=tf.tensordot(x_decode, self.embed_layer.embeddings, axes=[2, 1]),
labels=targets.to_tensor(default_value=0)
)
loss = loss * mask_decode
loss = tf.reduce_mean(loss)
return {"loss": loss, **{m.name: m.result() for m in self.metrics}}
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def embed(self, sentences) -> Dict[str, tf.Tensor]:
"""Takes batches of free text and returns context vectors for each example.
Parameters
----------
sentences : tf.Tensor
Tensor of dtype tf.string.
Returns
-------
Dict[str, tf.Tensor]
Attention vector and hidden state sequences with shapes (batch_size, embedding_size)
and (batch_size, max_sequence_len, embedding_size) respectively.
"""
tokens = self.tokenizer(sentences)
x, mask, _ = self.embed_layer(tokens, training=False)
x, context = self.encode_layer(x, mask=mask, training=False)
return {"sequences": x, "attention": context}
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def token_embed(self, sentences) -> Dict[str, tf.RaggedTensor]:
"""Takes batches of free text and returns word embeddings along with the associate token.
Parameters
----------
sentences : tf.Tensor
Tensor of dtype tf.string.
Returns
-------
Dict[str, tf.RaggedTensor]
Ragged tokens and embedding tensors with shapes (batch_size, None)
and (batch_size, None, embedding_size) respectively.
"""
tokens = self.tokenizer(sentences)
return {"tokens": tokens, "embeddings": self.embed_layer.get_embedding(tokens)}
class LstmAutoEncoder(Model):
"""Wrapper model class to combine the LSTM based encoder-decoder training pipeline.
Parameters
----------
max_sequence_len : int
Longest sequence seen at training time.
embedding_size : int
Dimensionality of the word-embeddings.
num_hidden : int, optional
Size of the hidden LSTM state, by default 64
token_hash : dict, optional
Token -> integer vocabulary lookup, by default None
vocab_size : int, optional
Size of the vocabulary. Set this if pre-computing token IDs to pass to the model, by default None
unknown_token : str, optional
The placeholder value for OOV terms, by default '<unk>'
sep : str, optional
Token separator by default ' '
input_drop_rate : float, optional
Value between 0 and 1.0, by default 0.
hidden_drop_rate : float, optional
Value between 0 and 1.0, by default 0.
Raises
------
ValueError
Raised if neither a vocab dictionary or a vocab size is provided.
Examples
--------
```python
import tensorflow as tf
from text2vec.autoencoders import LstmAutoEncoder
lookup = {'string': 0, 'is': 1, 'example': 2, '<unk>': 3}
model = LstmAutoEncoder(token_hash=lookup, max_sequence_len=10, embedding_size=16)
text = tf.constant(["sample string .", "this is a second example ."])
encoded, context_vectors = model(text)
```
"""
def __init__(self, max_sequence_len: int, embedding_size: int, num_hidden: int = 64,
token_hash: Optional[dict] = None, vocab_size: Optional[int] = None,
unknown_token: str = '<unk>', sep: str = ' ',
input_drop_rate: float = 0., hidden_drop_rate: float = 0.):
super().__init__()
if token_hash is None and vocab_size is None:
raise ValueError("Must provide either a dictionary mapping or a dictionary size if using token IDs")
params = dict(
max_sequence_len=max_sequence_len,
embedding_size=embedding_size,
input_drop_rate=input_drop_rate,
hidden_drop_rate=hidden_drop_rate
)
if token_hash is not None:
self.tokenizer = Tokenizer(sep)
self.embed_layer = TokenEmbed(
token_hash=token_hash,
embedding_size=embedding_size,
max_sequence_len=max_sequence_len,
unknown_token=unknown_token
)
else:
self.tokenizer = layers.Lambda(lambda x: x) # this is only for consistency, identity map
self.embed_layer = Embed(
vocab_size=vocab_size,
embedding_size=embedding_size,
max_sequence_len=max_sequence_len
)
self.encode_layer = RecurrentEncoder(num_hidden=num_hidden, **params)
self.decode_layer = RecurrentDecoder(num_hidden=num_hidden, **params)
def call(self, inputs, training: bool = False): # pylint: disable=missing-function-docstring
encoding_text = inputs[0]
decoding_text = inputs[1] if len(inputs) > 1 else encoding_text
encode_tokens = self.tokenizer(encoding_text)
x_embed, mask_encode, _ = self.embed_layer(encode_tokens, training=training)
x_encode, context, *states = self.encode_layer(x_embed, mask=mask_encode, training=training)
decode_tokens = self.tokenizer(decoding_text)
x_decode, mask_decode, _ = self.embed_layer(decode_tokens[:, :-1]) # skip </s>
x_decode = self.decode_layer(
x_enc=x_encode,
x_dec=x_decode,
dec_mask=mask_decode,
context=context,
# attention=self.encode_layer.attention,
initial_state=states,
training=training
)
return x_embed, x_decode, mask_decode, decode_tokens
def train_step(self, data): # pylint: disable=missing-function-docstring
with tf.GradientTape() as tape:
_, x_decode, mask_decode, decode_tokens = self(data, training=True)
targets = decode_tokens[:, 1:] # skip the <s> token with the slice on axis=1
if isinstance(self.embed_layer, TokenEmbed):
targets = tf.ragged.map_flat_values(self.embed_layer.table.lookup, targets)
targets = self.embed_layer.slicer(targets)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=tf.tensordot(x_decode, self.embed_layer.embeddings, axes=[2, 1]),
labels=targets.to_tensor(default_value=0)
)
loss = loss * mask_decode
loss = tf.reduce_mean(loss)
gradients = tape.gradient(loss, self.trainable_variables)
self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))
if hasattr(self.optimizer, 'learning_rate') and callable(self.optimizer.learning_rate):
return {"loss": loss, 'learning_rate': self.optimizer.learning_rate(self.optimizer.iterations)}
return {"loss": loss, 'learning_rate': self.optimizer.learning_rate}
def test_step(self, data): # pylint: disable=missing-function-docstring
_, x_decode, mask_decode, decode_tokens = self(data, training=False)
targets = decode_tokens[:, 1:] # skip the <s> token with the slice on axis=1
if isinstance(self.embed_layer, TokenEmbed):
targets = tf.ragged.map_flat_values(self.embed_layer.table.lookup, targets)
targets = self.embed_layer.slicer(targets)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=tf.tensordot(x_decode, self.embed_layer.embeddings, axes=[2, 1]),
labels=targets.to_tensor(default_value=0)
)
loss = loss * mask_decode
loss = tf.reduce_mean(loss)
return {"loss": loss, **{m.name: m.result() for m in self.metrics}}
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def embed(self, sentences) -> Dict[str, tf.Tensor]:
"""Takes batches of free text and returns context vectors for each example.
Parameters
----------
sentences : tf.Tensor
Tensor of dtype tf.string.
Returns
-------
Dict[str, tf.Tensor]
Attention vector and hidden state sequences with shapes (batch_size, embedding_size)
and (batch_size, max_sequence_len, embedding_size) respectively.
"""
tokens = self.tokenizer(sentences)
x, mask, _ = self.embed_layer(tokens, training=False)
x, context, *_ = self.encode_layer(x, mask=mask, training=False)
return {"sequences": x, "attention": context}
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def token_embed(self, sentences) -> Dict[str, tf.RaggedTensor]:
"""Takes batches of free text and returns word embeddings along with the associate token.
Parameters
----------
sentences : tf.Tensor
Tensor of dtype tf.string.
Returns
-------
Dict[str, tf.RaggedTensor]
Ragged tokens and embedding tensors with shapes (batch_size, None)
and (batch_size, None, embedding_size) respectively.
"""
tokens = self.tokenizer(sentences)
return {
"tokens": tokens,
"embeddings": self.embed_layer.get_embedding(tokens)
}