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