Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132937 views
License: OTHER
1
def saturating_sigmoid(x):
2
"""Saturating sigmoid: 1.2 * sigmoid(x) - 0.1 cut to [0, 1]."""
3
with tf.name_scope("saturating_sigmoid", [x]):
4
y = tf.sigmoid(x)
5
return tf.minimum(1.0, tf.maximum(0.0, 1.2 * y - 0.1))
6
7
8
def embedding(x, vocab_size, dense_size, name=None, reuse=None):
9
"""Embed x of type int64 into dense vectors, reducing to max 4 dimensions."""
10
with tf.variable_scope(name, default_name="embedding",
11
values=[x], reuse=reuse):
12
embedding_var = tf.get_variable("kernel", [vocab_size, dense_size])
13
return tf.gather(embedding_var, x)
14
15
16
def conv_gru(x, kernel_size, filters, padding="same", dilation_rate=1,
17
name=None, reuse=None):
18
"""Convolutional GRU in 1 dimension."""
19
# Let's make a shorthand for conv call first.
20
def do_conv(args, name, bias_start, padding):
21
return tf.layers.conv1d(args, filters, kernel_size,
22
padding=padding, dilation_rate=dilation_rate,
23
bias_initializer=tf.constant_initializer(bias_start), name=name)
24
# Here comes the GRU gate.
25
with tf.variable_scope(name, default_name="conv_gru",
26
values=[x], reuse=reuse):
27
reset = saturating_sigmoid(do_conv(x, "reset", 1.0, padding))
28
gate = saturating_sigmoid(do_conv(x, "gate", 1.0, padding))
29
candidate = tf.tanh(do_conv(reset * x, "candidate", 0.0, padding))
30
return gate * x + (1 - gate) * candidate
31
32