-
Notifications
You must be signed in to change notification settings - Fork 714
/
model.py
358 lines (277 loc) · 11.6 KB
/
model.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from __future__ import absolute_import, division, print_function
import tensorflow as tf
def full_connect(inputs,
weights_shape,
biases_shape,
is_train=True,
FLAGS=None):
"""
Define full-connect layer with reused Variables.
"""
weights = tf.get_variable(
"weights", weights_shape, initializer=tf.random_normal_initializer())
biases = tf.get_variable(
"biases", biases_shape, initializer=tf.random_normal_initializer())
layer = tf.matmul(inputs, weights) + biases
if FLAGS.enable_bn and is_train:
mean, var = tf.nn.moments(layer, axes=[0])
scale = tf.get_variable(
"scale", biases_shape, initializer=tf.random_normal_initializer())
shift = tf.get_variable(
"shift", biases_shape, initializer=tf.random_normal_initializer())
layer = tf.nn.batch_normalization(layer, mean, var, shift, scale,
FLAGS.bn_epsilon)
return layer
def full_connect_relu(inputs,
weights_shape,
biases_shape,
is_train=True,
FLAGS=None):
"""
Define full-connect layer and activation function with reused Variables.
"""
layer = full_connect(inputs, weights_shape, biases_shape, is_train, FLAGS)
layer = tf.nn.relu(layer)
return layer
def customized_inference(inputs,
input_units,
output_units,
is_train=True,
FLAGS=None):
"""
Define the customed model.
"""
hidden1_units = 128
hidden2_units = 32
hidden3_units = 8
with tf.variable_scope("input_layer"):
layer = full_connect_relu(inputs, [input_units, hidden1_units],
[hidden1_units], is_train, FLAGS)
with tf.variable_scope("layer_0"):
layer = full_connect_relu(layer, [hidden1_units, hidden2_units],
[hidden2_units], is_train, FLAGS)
with tf.variable_scope("layer_1"):
layer = full_connect_relu(layer, [hidden2_units, hidden3_units],
[hidden3_units], is_train, FLAGS)
if FLAGS.enable_dropout and is_train:
layer = tf.nn.dropout(layer, FLAGS.dropout_keep_prob)
with tf.variable_scope("output_layer"):
layer = full_connect(layer, [hidden3_units, output_units], [output_units],
is_train, FLAGS)
return layer
def dnn_inference(inputs, input_units, output_units, is_train=True,
FLAGS=None):
"""
Define the DNN model.
"""
# Example: [128, 64, 32, 16]
model_network_hidden_units = [int(i) for i in FLAGS.dnn_struct.split()]
with tf.variable_scope("input_layer"):
layer = full_connect_relu(inputs,
[input_units, model_network_hidden_units[0]],
[model_network_hidden_units[0]], is_train, FLAGS)
for i in range(len(model_network_hidden_units) - 1):
with tf.variable_scope("layer_{}".format(i)):
layer = full_connect_relu(layer, [
model_network_hidden_units[i], model_network_hidden_units[i + 1]
], [model_network_hidden_units[i + 1]], is_train, FLAGS)
with tf.variable_scope("output_layer"):
layer = full_connect(layer, [model_network_hidden_units[-1], output_units],
[output_units], is_train, FLAGS)
return layer
def lr_inference(inputs, input_units, output_units, is_train=True, FLAGS=None):
"""
Define the linear regression model.
"""
with tf.variable_scope("lr"):
layer = full_connect(inputs, [input_units, output_units], [output_units],
FLAGS)
return layer
def wide_and_deep_inference(inputs,
input_units,
output_units,
is_train=True,
FLAGS=None):
"""
Define the wide-and-deep model.
"""
return lr_inference(inputs, input_units,
output_units, is_train, FLAGS) + dnn_inference(
inputs, input_units, output_units, is_train, FLAGS)
def cnn_inference(inputs, input_units, output_units, is_train=True,
FLAGS=None):
"""
Define the CNN model.
"""
# [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3, 1]
inputs = tf.reshape(inputs, [-1, 3, 3, 1])
# [BATCH_SIZE, 3, 3, 1] -> [BATCH_SIZE, 3, 3, 8]
with tf.variable_scope("conv_0"):
weights = tf.get_variable(
"weights", [3, 3, 1, 8], initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [8], initializer=tf.random_normal_initializer())
layer = tf.nn.conv2d(inputs, weights, strides=[1, 1, 1, 1], padding="SAME")
layer = tf.nn.bias_add(layer, bias)
layer = tf.nn.relu(layer)
# [BATCH_SIZE, 3, 3, 8] -> [BATCH_SIZE, 3 * 3 * 8]
layer = tf.reshape(layer, [-1, 3 * 3 * 8])
# [BATCH_SIZE, 3 * 3 * 8] -> [BATCH_SIZE, LABEL_SIZE]
with tf.variable_scope("output_layer"):
weights = tf.get_variable(
"weights", [3 * 3 * 8, FLAGS.label_size],
initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [FLAGS.label_size], initializer=tf.random_normal_initializer())
layer = tf.add(tf.matmul(layer, weights), bias)
return layer
def customized_cnn_inference(inputs,
input_units,
output_units,
is_train=True,
FLAGS=None):
"""
Define the CNN model.
"""
# TODO: Change if validate_batch_size is different
# [BATCH_SIZE, 512 * 512 * 1] -> [BATCH_SIZE, 512, 512, 1]
inputs = tf.reshape(inputs, [FLAGS.train_batch_size, 512, 512, 1])
# [BATCH_SIZE, 512, 512, 1] -> [BATCH_SIZE, 128, 128, 8]
with tf.variable_scope("conv0"):
weights = tf.get_variable(
"weights", [3, 3, 1, 8], initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [8], initializer=tf.random_normal_initializer())
layer = tf.nn.conv2d(inputs, weights, strides=[1, 1, 1, 1], padding="SAME")
layer = tf.nn.bias_add(layer, bias)
layer = tf.nn.relu(layer)
layer = tf.nn.max_pool(
layer, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding="SAME")
# [BATCH_SIZE, 128, 128, 8] -> [BATCH_SIZE, 32, 32, 8]
with tf.variable_scope("conv1"):
weights = tf.get_variable(
"weights", [3, 3, 8, 8], initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [8], initializer=tf.random_normal_initializer())
layer = tf.nn.conv2d(layer, weights, strides=[1, 1, 1, 1], padding="SAME")
layer = tf.nn.bias_add(layer, bias)
layer = tf.nn.relu(layer)
layer = tf.nn.max_pool(
layer, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding="SAME")
# [BATCH_SIZE, 32, 32, 8] -> [BATCH_SIZE, 8, 8, 8]
with tf.variable_scope("conv2"):
weights = tf.get_variable(
"weights", [3, 3, 8, 8], initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [8], initializer=tf.random_normal_initializer())
layer = tf.nn.conv2d(layer, weights, strides=[1, 1, 1, 1], padding="SAME")
layer = tf.nn.bias_add(layer, bias)
layer = tf.nn.relu(layer)
layer = tf.nn.max_pool(
layer, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding="SAME")
# [BATCH_SIZE, 8, 8, 8] -> [BATCH_SIZE, 8 * 8 * 8]
layer = tf.reshape(layer, [-1, 8 * 8 * 8])
# [BATCH_SIZE, 8 * 8 * 8] -> [BATCH_SIZE, LABEL_SIZE]
with tf.variable_scope("output"):
weights = tf.get_variable(
"weights", [8 * 8 * 8, FLAGS.label_size],
initializer=tf.random_normal_initializer())
bias = tf.get_variable(
"bias", [FLAGS.label_size], initializer=tf.random_normal_initializer())
layer = tf.add(tf.matmul(layer, weights), bias)
return layer
def lstm_inference(inputs,
input_units,
output_units,
is_train=True,
FLAGS=None):
RNN_HIDDEN_UNITS = 128
timesteps = 3
number_input = 3
weights = tf.Variable(tf.random_normal([RNN_HIDDEN_UNITS, output_units]))
biases = tf.Variable(tf.random_normal([output_units]))
# [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3]
x = tf.reshape(inputs, [-1, timesteps, number_input])
# [BATCH_SIZE, 3, 3] -> 3 * [BATCH_SIZE, 3]
x = tf.unstack(x, timesteps, 1)
# output size is 128, state size is (c=128, h=128)
lstm_cell = tf.contrib.rnn.BasicLSTMCell(RNN_HIDDEN_UNITS, forget_bias=1.0)
# outputs is array of 3 * [BATCH_SIZE, 3]
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
# outputs[-1] is [BATCH_SIZE, 3]
layer = tf.matmul(outputs[-1], weights) + biases
return layer
def bidirectional_lstm_inference(inputs,
input_units,
output_units,
is_train=True,
FLAGS=None):
RNN_HIDDEN_UNITS = 128
timesteps = 3
number_input = 3
weights = tf.Variable(tf.random_normal([RNN_HIDDEN_UNITS, output_units]))
biases = tf.Variable(tf.random_normal([output_units]))
# [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3]
x = tf.reshape(inputs, [-1, timesteps, number_input])
# [BATCH_SIZE, 3, 3] -> 3 * [BATCH_SIZE, 3]
x = tf.unstack(x, timesteps, 1)
# Update the hidden units for bidirection-rnn
fw_lstm_cell = tf.contrib.rnn.BasicLSTMCell(
RNN_HIDDEN_UNITS / 2, forget_bias=1.0)
bw_lstm_cell = tf.contrib.rnn.BasicLSTMCell(
RNN_HIDDEN_UNITS / 2, forget_bias=1.0)
outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(
fw_lstm_cell, bw_lstm_cell, x, dtype=tf.float32)
# outputs[-1] is [BATCH_SIZE, 3]
layer = tf.matmul(outputs[-1], weights) + biases
return layer
def gru_inference(inputs, input_units, output_units, is_train=True,
FLAGS=None):
RNN_HIDDEN_UNITS = 128
timesteps = 3
number_input = 3
weights = tf.Variable(tf.random_normal([RNN_HIDDEN_UNITS, output_units]))
biases = tf.Variable(tf.random_normal([output_units]))
# [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3]
x = tf.reshape(inputs, [-1, timesteps, number_input])
# [BATCH_SIZE, 3, 3] -> 3 * [BATCH_SIZE, 3]
x = tf.unstack(x, timesteps, 1)
# output size is 128, state size is (c=128, h=128)
lstm_cell = tf.contrib.rnn.GRUCell(RNN_HIDDEN_UNITS)
# outputs is array of 3 * [BATCH_SIZE, 3]
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
# outputs[-1] is [BATCH_SIZE, 3]
layer = tf.matmul(outputs[-1], weights) + biases
return layer
def compute_softmax_and_accuracy(logits, labels):
"""
Compute the softmax and accuracy of the logits and labels.
Args:
logits: The logits from the model.
labels: The labels.
Return:
The softmax op and accuracy op.
"""
softmax_op = tf.nn.softmax(logits)
correct_prediction_op = tf.equal(tf.argmax(softmax_op, 1), labels)
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction_op, tf.float32))
return softmax_op, accuracy_op
def compute_auc(softmax_op, label_op, label_size):
"""
Compute the auc of the softmax result and labels.
Args:
softmax_op: The softmax op.
label_op: The label op.
label_size: The label size.
Return:
The auc op.
"""
batch_labels = tf.cast(label_op, tf.int32)
sparse_labels = tf.reshape(batch_labels, [-1, 1])
derived_size = tf.shape(batch_labels)[0]
indices = tf.reshape(tf.range(0, derived_size, 1), [-1, 1])
concated = tf.concat(axis=1, values=[indices, sparse_labels])
outshape = tf.stack([derived_size, label_size])
new_batch_labels = tf.sparse_to_dense(concated, outshape, 1.0, 0.0)
_, auc_op = tf.contrib.metrics.streaming_auc(softmax_op, new_batch_labels)
return auc_op