-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodules.py
38 lines (32 loc) · 1.37 KB
/
modules.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
import tensorflow as tf
from tensorflow import keras
class BiGRUlayer(object):
def __init__(self, hidden, name='bi-gru'):
self.hidden = hidden
self.name = name
with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
self.bigru_layer = keras.layers.Bidirectional(
keras.layers.GRU(units=self.hidden,
return_sequences=True),
merge_mode='concat'
)
def __call__(self, inputs, seq_lens=None):
with tf.variable_scope(self.name):
mask = tf.sequence_mask(seq_lens, dtype=tf.float32) \
if seq_lens is not None else None
return self.bigru_layer(inputs, mask=mask)
class BLSTMlayer(object):
def __init__(self, hidden, name='blstm'):
self.hidden = hidden
self.name = name
with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
self.blstm_layer = keras.layers.Bidirectional(
keras.layers.CuDNNLSTM(units=self.hidden,
return_sequences=True),
merge_mode='concat'
)
def __call__(self, inputs, seq_lens=None):
with tf.variable_scope(self.name):
#mask = tf.sequence_mask(seq_lens, dtype=tf.float32) \
# if seq_lens is not None else None
return self.blstm_layer(inputs)