/
tf_utils.py
executable file
·71 lines (57 loc) · 2.52 KB
/
tf_utils.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
import tensorflow as tf
import tensorflow.contrib.layers as ly
import tf_utils
def length(sequence):
"""
@sequence: 3D tensor of shape (batch_size, sequence_length, embedding_size)
"""
used = tf.sign(tf.reduce_sum(tf.abs(sequence), reduction_indices=2))
length = tf.reduce_sum(used, reduction_indices=1)
length = tf.cast(length, tf.int32)
return length # vector of size (batch_size) containing sentence lengths
def last_relevant(output, length):
batch_size = tf.shape(output)[0]
max_length = tf.shape(output)[1]
out_size = int(output.get_shape()[2])
index = tf.range(0, batch_size) * max_length + (length - 1)
flat = tf.reshape(output, [-1, out_size])
relevant = tf.gather(flat, index)
return relevant
def leaky_rectify(x, leakiness=0.2):
return tf.maximum(x, leakiness * x)
def cust_conv2d(input_layer, out_dim, h_f=3, w_f=3, h_s=2, w_s=2, padding="SAME", scope_name=None,
batch_norm=True, activation_fn=tf_utils.leaky_rectify, is_training=True):
with tf.variable_scope(scope_name) as _:
out = ly.conv2d(input_layer,
out_dim,
[w_f, h_f],
[h_s, w_s],
padding,
activation_fn=None)
if batch_norm:
out = ly.batch_norm(out, is_training=is_training, updates_collections=None)
if activation_fn:
out = activation_fn(out)
return out
def cust_conv2d_transpose(input_layer, out_dim, h_f=3, w_f=3, h_s=2, w_s=2, padding="SAME",
scope_name="transpose_conv_2D",
batch_norm=True, activation_fn=tf_utils.leaky_rectify, is_training=True):
with tf.variable_scope(scope_name) as _:
out = ly.conv2d_transpose(input_layer,
out_dim,
[w_f, h_f],
[w_s, h_s],
padding,
activation_fn=None)
if batch_norm:
out = ly.batch_norm(out, is_training=is_training, updates_collections=None)
if activation_fn:
out = activation_fn(out)
return out
def channel_wise_fc(input_layer):
return cust_conv2d(input_layer, input_layer.shape[-1], h_f=1, w_f=1, h_s=1, w_s=1, batch_norm=False,
activation_fn=None)
def add_summary_python_scalar(name, scalar):
return tf.Summary(value=[
tf.Summary.Value(tag=name, simple_value=scalar),
])