-
Notifications
You must be signed in to change notification settings - Fork 598
/
tf_utils.py
90 lines (76 loc) · 2.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import tensorflow as tf
def dense_layer(inputs, output_units, bias=True, activation=None, batch_norm=None,
dropout=None, scope='dense-layer', reuse=False):
"""
Applies a dense layer to a 2D tensor of shape [batch_size, input_units]
to produce a tensor of shape [batch_size, output_units].
Args:
inputs: Tensor of shape [batch size, input_units].
output_units: Number of output units.
activation: activation function.
dropout: dropout keep prob.
Returns:
Tensor of shape [batch size, output_units].
"""
with tf.variable_scope(scope, reuse=reuse):
W = tf.get_variable(
name='weights',
initializer=tf.contrib.layers.variance_scaling_initializer(),
shape=[shape(inputs, -1), output_units]
)
z = tf.matmul(inputs, W)
if bias:
b = tf.get_variable(
name='biases',
initializer=tf.constant_initializer(),
shape=[output_units]
)
z = z + b
if batch_norm is not None:
z = tf.layers.batch_normalization(z, training=batch_norm, reuse=reuse)
z = activation(z) if activation else z
z = tf.nn.dropout(z, dropout) if dropout is not None else z
return z
def time_distributed_dense_layer(
inputs, output_units, bias=True, activation=None, batch_norm=None,
dropout=None, scope='time-distributed-dense-layer', reuse=False):
"""
Applies a shared dense layer to each timestep of a tensor of shape
[batch_size, max_seq_len, input_units] to produce a tensor of shape
[batch_size, max_seq_len, output_units].
Args:
inputs: Tensor of shape [batch size, max sequence length, ...].
output_units: Number of output units.
activation: activation function.
dropout: dropout keep prob.
Returns:
Tensor of shape [batch size, max sequence length, output_units].
"""
with tf.variable_scope(scope, reuse=reuse):
W = tf.get_variable(
name='weights',
initializer=tf.contrib.layers.variance_scaling_initializer(),
shape=[shape(inputs, -1), output_units]
)
z = tf.einsum('ijk,kl->ijl', inputs, W)
if bias:
b = tf.get_variable(
name='biases',
initializer=tf.constant_initializer(),
shape=[output_units]
)
z = z + b
if batch_norm is not None:
z = tf.layers.batch_normalization(z, training=batch_norm, reuse=reuse)
z = activation(z) if activation else z
z = tf.nn.dropout(z, dropout) if dropout is not None else z
return z
def shape(tensor, dim=None):
"""Get tensor shape/dimension as list/int"""
if dim is None:
return tensor.shape.as_list()
else:
return tensor.shape.as_list()[dim]
def rank(tensor):
"""Get tensor rank as python list"""
return len(tensor.shape.as_list())