-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_util1.py
147 lines (112 loc) · 5.12 KB
/
tf_util1.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import tensorflow as tf
def lstm_layer(inputs, lengths, state_size, keep_prob=1.0, scope='lstm-layer', reuse=False, return_final_state=False):
with tf.variable_scope(scope, reuse=reuse):
cell_fw = tf.contrib.rnn.DropoutWrapper(
tf.contrib.rnn.LSTMCell(
state_size,
reuse=reuse
),
output_keep_prob=keep_prob
)
outputs, output_state = tf.nn.dynamic_rnn(
inputs=inputs,
cell=cell_fw,
sequence_length=lengths,
dtype=tf.float32
)
if return_final_state:
return outputs, output_state
else:
return outputs
def temporal_convolution_layer(inputs, output_units, convolution_width, causal=False, dilation_rate=[1], bias=True,
activation=None, dropout=None, scope='temporal-convolution-layer', reuse=False):
with tf.variable_scope(scope, reuse=reuse):
if causal:
shift = (convolution_width / 2) + (int(dilation_rate[0] - 1) / 2)
pad = tf.zeros([tf.shape(inputs)[0], shift, inputs.shape.as_list()[2]])
inputs = tf.concat([pad, inputs], axis=1)
W = tf.get_variable(
name='weights',
initializer=tf.contrib.layers.variance_scaling_initializer(),
shape=[convolution_width, shape(inputs, 2), output_units]
)
z = tf.nn.convolution(inputs, W, padding='SAME', dilation_rate=dilation_rate)
if bias:
b = tf.get_variable(
name='biases',
initializer=tf.constant_initializer(),
shape=[output_units]
)
z = z + b
z = activation(z) if activation else z
z = tf.nn.dropout(z, dropout) if dropout is not None else z
z = z[:, :-shift, :] if causal 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):
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 dense_layer(inputs, output_units, bias=True, activation=None, batch_norm=None, dropout=None,
scope='dense-layer', reuse=False):
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 sequence_log_loss(y, y_hat, sequence_lengths, max_sequence_length, eps=1e-15):
y = tf.cast(y, tf.float32)
y_hat = tf.minimum(tf.maximum(y_hat, eps), 1.0 - eps)
log_losses = y*tf.log(y_hat) + (1.0 - y)*tf.log(1.0 - y_hat)
sequence_mask = tf.cast(tf.sequence_mask(sequence_lengths, maxlen=max_sequence_length), tf.float32)
avg_log_loss = -tf.reduce_sum(log_losses*sequence_mask) / tf.cast(tf.reduce_sum(sequence_lengths), tf.float32)
return avg_log_loss
def sequence_rmse(y, y_hat, sequence_lengths, max_sequence_length):
y = tf.cast(y, tf.float32)
squared_error = tf.square(y - y_hat)
sequence_mask = tf.cast(tf.sequence_mask(sequence_lengths, maxlen=max_sequence_length), tf.float32)
avg_squared_error = tf.reduce_sum(squared_error*sequence_mask) / tf.cast(tf.reduce_sum(sequence_lengths), tf.float32)
rmse = tf.sqrt(avg_squared_error)
return rmse
def log_loss(y, y_hat, eps=1e-15):
y = tf.cast(y, tf.float32)
y_hat = tf.minimum(tf.maximum(y_hat, eps), 1.0 - eps)
log_loss = -tf.reduce_mean(y*tf.log(y_hat) + (1.0 - y)*tf.log(1.0 - y_hat))
return log_loss
def rank(tensor):
return len(tensor.shape.as_list())
def shape(tensor, dim=None):
if dim is None:
return tensor.shape.as_list()
else:
return tensor.shape.as_list()[dim]