-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtf_app.py
executable file
·380 lines (308 loc) · 11.3 KB
/
tf_app.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
"""
This is a TensorFlow rapid binary classifier model prototyping tool intended
to be very easy to set up.
"""
# 2016-07-19 M. Okun
# Copyright 2016 M. Okun
# All Rights Reserved
# License follows for original TensorFlow Wide & Deep Tutorial, from which this
# code was adapted, and which is described at
# https://www.tensorflow.org/versions/r0.9/tutorials/wide_and_deep/index.html
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# future
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# system
# import subprocess
import os
import tempfile
# utils
# import webbrowser
import logging
import yaml
# pandas
import pandas as pd
# tensorflow
import tensorflow as tf
from tensorflow.contrib.learn import monitors
from tensorflow.contrib.layers import (
real_valued_column,
sparse_column_with_hash_bucket,
sparse_column_with_integerized_feature,
# sparse_column_with_keys,
# sparse_feature_cross,
embedding_column,
# crossed_column,
)
# sklearn
# from sklearn import metrics
# from sklearn import cross_validation
from sklearn.cross_validation import train_test_split
# from sklearn.linear_model import LogisticRegression
# from sklearn.cross_validation import cross_val_score
# from sklearn.metrics import roc_curve, auc, roc_auc_score
####################
# FLAGS
####################
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_bool(
"verbose",
True,
"Show INFO and WARN messages")
# --- Input and config
tf.app.flags.DEFINE_string(
"model_conf_file",
"MODEL_CONF.yaml",
"Path to the Model Config Yaml file, default:{}")
tf.app.flags.DEFINE_string(
"data_conf_file",
"DATA_CONF.yaml",
"Path to the Data Config Yaml file, default:{}")
tf.app.flags.DEFINE_string(
"train_n_test_file",
None,
"Path to the combined training and testing data (will be split).")
# --- Output
tf.app.flags.DEFINE_string(
"model_dir",
None,
"Base directory for output models.")
tf.app.flags.DEFINE_string(
"weights_file",
None,
"File path for serialized model weights output.")
# --- Model
tf.app.flags.DEFINE_string(
"model_type",
None,
"Valid model types: {'wide', 'deep', 'wide_n_deep'}.")
tf.app.flags.DEFINE_integer(
"train_steps",
None,
"Number of training steps.")
tf.app.flags.DEFINE_integer(
"learn_rate",
None,
"Learning Rate")
########################
# Load Model Config Yaml
########################
with open(FLAGS.model_conf_file) as modelfile:
MODEL_CONF = yaml.load(modelfile)
##########################
# Load Feature Config Yaml
##########################
with open(FLAGS.data_conf_file) as datafile:
DATA_CONF = yaml.load(datafile)
##############################################################
# Get settings from conf files if not overidden with cli flags
##############################################################
# Get a model directory
MODEL_DIR = tempfile.mkdtemp() if not FLAGS.model_dir else FLAGS.model_dir
TRAIN_N_TEST_FILE = os.path.abspath(DATA_CONF['train_n_test_file']) \
if not FLAGS.train_n_test_file else FLAGS.train_n_test_file
MODEL_TYPE = MODEL_CONF['model_type']\
if not FLAGS.model_type else FLAGS.model_type
WEIGHTS_FILE = MODEL_CONF['weights_file']\
if not FLAGS.weights_file else FLAGS.weights_file
TRAIN_STEPS = MODEL_CONF['train_steps']\
if not FLAGS.train_steps else FLAGS.train_steps
LEARN_RATE = MODEL_CONF['learn_rate']\
if not FLAGS.learn_rate else FLAGS.learn_rate
# deep params
HIDDEN_UNITS = MODEL_CONF['hidden_units']
EMBEDDING_DIMENSION = MODEL_CONF['embedding_dimension']
#################################################################
# Features and labels
#################################################################
LABEL_COLUMN = DATA_CONF['LABEL_COLUMN']
MULTI_CATEGORY_COLUMNS = DATA_CONF['MULTI_CATEGORY_COLUMNS']
BINARY_COLUMNS = DATA_CONF['BINARY_COLUMNS']
CONTINUOUS_COLUMNS = DATA_CONF['CONTINUOUS_COLUMNS']
# setup exponential decay function
# (from https://terrytangyuan.github.io/2016/03/14/scikit-flow-intro/)
GLOBAL_STEP = tf.Variable(0, name="global_step", trainable=False)
def exp_decay(global_step):
return tf.train.exponential_decay(
learning_rate=LEARN_RATE, global_step=global_step,
decay_steps=100, decay_rate=0.001)
#################################################################
# General-purpose code
#################################################################
def build_estimator(model_dir=MODEL_DIR):
"""
Build an estimator using
CONTINTUOUS_COLUMNS, BINARY_COLUMNS and MULTI_CATEGORY_COLUMNS.
"""
bucketized_columns = \
[sparse_column_with_hash_bucket(col, 1000)
for col in MULTI_CATEGORY_COLUMNS] + \
[sparse_column_with_integerized_feature(col, bucket_size=2)
for col in BINARY_COLUMNS]
real_valued_columns = \
[real_valued_column(col) for col in CONTINUOUS_COLUMNS]
crossed_columns = \
[]
# Wide columns and deep columns.
wide_columns = \
bucketized_columns + \
real_valued_columns + \
crossed_columns
# embedding columns for hash_bucket columns
deep_columns = \
[embedding_column(col, dimension=EMBEDDING_DIMENSION)
for col in bucketized_columns] + \
real_valued_columns + \
crossed_columns
if MODEL_TYPE == "wide":
print('Creating wide LinearClassifier model...\n')
model = tf.contrib.learn.LinearClassifier(
model_dir=model_dir,
n_classes=2,
feature_columns=wide_columns,
# optimizer=tf.train.GradientDescentOptimizer(
# learning_rate=FLAGS.learn_rate)
# optimizer=tf.train.FtrlOptimizer(
# learning_rate=LEARN_RATE,
# l1_regularization_strength=0.0,
# l2_regularization_strength=0.0),
)
elif MODEL_TYPE == "deep":
print('Creating deep DNNClassifier model...\n')
model = tf.contrib.learn.DNNClassifier(
model_dir=model_dir,
n_classes=2,
feature_columns=deep_columns,
hidden_units=HIDDEN_UNITS,
# optimizer=tf.train.FtrlOptimizer(
# learning_rate=LEARN_RATE,
# l1_regularization_strength=0.0,
# l2_regularization_strength=0.0),
)
else:
print('Creating deepNwide DNNLinearCombinedClassifier model...\n')
model = tf.contrib.learn.DNNLinearCombinedClassifier(
model_dir=model_dir,
n_classes=2,
linear_feature_columns=wide_columns,
dnn_feature_columns=deep_columns,
dnn_hidden_units=HIDDEN_UNITS,
# optimizer=tf.train.FtrlOptimizer(
# learning_rate=LEARN_RATE,
# l1_regularization_strength=0.0,
# l2_regularization_strength=0.0),
)
return model
def input_fn(df):
"""Input builder function."""
# Creates a dictionary mapping from each continuous feature column name
# (k) to the values of that column stored in a constant Tensor.
continuous_cols = {k: tf.constant(df[k].values.astype(float))
for k in CONTINUOUS_COLUMNS}
# Creates a dictionary mapping from each categorical feature column name
# (k) to the values of that column stored in a tf.SparseTensor.
multi_category_cols = {k: tf.SparseTensor(
indices=[[i, 0] for i in range(df[k].size)],
values=df[k].values.astype(str),
dense_shape=[df[k].size, 1])
for k in MULTI_CATEGORY_COLUMNS}
binary_cols = {k: tf.SparseTensor(
indices=[[i, 0] for i in range(df[k].size)],
values=df[k].values.astype(int),
dense_shape=[df[k].size, 1])
for k in BINARY_COLUMNS}
# DEBUG
print('multi_category_cols:')
print({k: df[k].dtype for k in multi_category_cols})
print('binary_cols:')
print({k: df[k].dtype for k in binary_cols})
print('continuous_cols:')
print({k: df[k].dtype for k in continuous_cols})
feature_cols = dict(continuous_cols)
feature_cols.update(binary_cols)
feature_cols.update(multi_category_cols)
# Converts the label column into a constant Tensor.
label = tf.constant(df[LABEL_COLUMN].astype(int).values)
# Returns the feature columns and the label.
return feature_cols, label
def train_and_eval():
"""Train and evaluate the model"""
# Read data
df_train_test = pd.read_csv(
tf.gfile.Open(TRAIN_N_TEST_FILE),
low_memory=False)
# Split training and testing data
(df_train, df_test) = train_test_split(df_train_test, test_size=0.25)
# Build the estimator
print("\nBuilding Estimator...")
model = build_estimator(MODEL_DIR)
# Create a validation monitor
val_mon = monitors.ValidationMonitor(
input_fn=lambda: input_fn(df_test),
every_n_steps=10, early_stopping_rounds=100)
# Fit and evaluate
print("\nFitting with {} steps".format(TRAIN_STEPS))
model.fit(
input_fn=lambda: input_fn(df_train),
steps=TRAIN_STEPS,
# monitors=[val_mon]) # why doesn't this work when it's passed in?
)
print("\nEvaluating...")
results = model.evaluate(
input_fn=lambda: input_fn(df_test),
steps=1)
return model, results
# --- Helpers
def print_info(results):
"""
Print some helpful output, given a results objects
"""
print("model directory = {}".format(MODEL_DIR))
print("\n***************")
print("Model Type: {}".format(MODEL_TYPE))
print("Steps: {}".format(TRAIN_STEPS))
for key in sorted(results):
print("%s: %s" % (key, results[key]))
print("***************\n")
print("\n#####################################")
print("To open Tensorboard with this model:")
print("python -m webbrowser http://0.0.0.0:6006")
print("tensorboard --logdir {}".format(MODEL_DIR))
print("#####################################\n")
def main(_):
""" Main Functionality """
config = tf.contrib.learn.RunConfig()
print("model directory = {}".format(MODEL_DIR))
# Set verbosity
if FLAGS.verbose:
logging.getLogger().setLevel(logging.INFO)
else:
logging.getLogger().setLevel(logging.ERROR)
# --- Run
model, results = train_and_eval()
# --- Final Output
print_info(results)
# DNNLinearCombinedClassifier doesn't have m.weights_
# if MODEL_TYPE in ["deep", "wide"]:
# print("weights:\n{}".format(model.weights_))
# if WEIGHTS_FILE:
# with open(_WEIGHTS_FILE, 'wb') as weightfile:
# # todo we can make a nicer weights file
# weightfile.write(str(model.weights_))
if __name__ == "__main__":
# tf.app.run()
# init = tf.initialize_all_variables()
tf.app.run()