-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdo_tensorflow.py
executable file
·155 lines (124 loc) · 4.83 KB
/
do_tensorflow.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
#!/usr/bin/env python
"""
Train a gradient boosting classifier on the airlines dataset using
TensorFlow's Boosted Trees.
References:
https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html
https://www.tensorflow.org/programmers_guide/datasets#consuming_numpy_arrays
"""
import argparse
import os
import sys
from IPython import embed
import numpy as np
import tensorflow as tf
from tensorflow.contrib.boosted_trees.estimator_batch.estimator import GradientBoostedDecisionTreeClassifier
from tensorflow.contrib.boosted_trees.proto import learner_pb2
from tensorflow.contrib.layers.python.layers import feature_column
from tensorflow.contrib.learn import learn_runner
FLAGS = None
def _get_tfbt(output_dir, feature_cols):
"""Configures TF Boosted Trees estimator based on flags."""
learner_config = learner_pb2.LearnerConfig()
learner_config.learning_rate_tuner.fixed.learning_rate = FLAGS.learning_rate
learner_config.regularization.l1 = 0.0
# Set the regularization per instance in such a way that
# regularization for the full training data is equal to l2 flag.
learner_config.regularization.l2 = FLAGS.l2 / FLAGS.batch_size
learner_config.constraints.max_tree_depth = FLAGS.depth
learner_config.growing_mode = learner_pb2.LearnerConfig.LAYER_BY_LAYER
run_config = tf.contrib.learn.RunConfig(save_checkpoints_secs=30)
# Create a TF Boosted trees regression estimator.
estimator = GradientBoostedDecisionTreeClassifier(
learner_config=learner_config,
examples_per_layer=FLAGS.examples_per_layer,
n_classes=2,
num_trees=FLAGS.num_trees,
feature_columns=feature_cols,
model_dir=output_dir,
config=run_config,
center_bias=False)
return estimator
def _matrix_to_dict(matrix, col_names):
return {
feat_name: matrix[:, feat_idx, np.newaxis]
for feat_idx, feat_name in enumerate(col_names)}
def _make_input_fn(which_set):
data = np.load('airlines_data.npz')
feature_names = data['feature_names']
feature_columns = [feature_column.real_valued_column(
k) for k in feature_names]
if which_set == 'train':
return feature_columns, tf.estimator.inputs.numpy_input_fn(
x=_matrix_to_dict(data['X_train'], feature_names),
y=data['y_train'],
batch_size=32,
num_epochs=None,
shuffle=True)
elif which_set == 'test':
return feature_columns, tf.estimator.inputs.numpy_input_fn(
x=_matrix_to_dict(data['X_test'], feature_names),
y=data['y_test'],
num_epochs=1,
shuffle=False)
else:
raise NotImplementedError()
def _make_experiment_fn(output_dir):
feature_columns, train_input_fn = _make_input_fn('train')
feature_columns, test_input_fn = _make_input_fn('test')
return tf.contrib.learn.Experiment(
estimator=_get_tfbt(output_dir, feature_columns),
train_input_fn=train_input_fn,
eval_input_fn=test_input_fn,
train_steps=None,
eval_metrics=None,
eval_steps=None, # Run through the test data once
)
def main(unused_argv):
learn_runner.run(
experiment_fn=_make_experiment_fn,
output_dir=FLAGS.output_dir,
schedule='train_and_evaluate')
# Run inference on the test dataset
feature_columns, test_input_fn = _make_input_fn('test')
estimator = _get_tfbt(FLAGS.output_dir, feature_columns)
results = estimator.predict(input_fn=test_input_fn)
y_predict = np.array([r['probabilities'][1] for r in results])
np.save(os.path.join(FLAGS.output_dir, 'pred_tf.npy'), y_predict)
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(
"--batch_size",
type=int,
default=10000,
help="The batch size for reading data.")
parser.add_argument(
"--depth",
type=int,
default=6,
help="Maximum depth of weak learners.")
parser.add_argument(
"--l2",
type=float,
default=1.0,
help="l2 regularization per batch.")
parser.add_argument(
"--learning_rate",
type=float,
default=0.1,
help="Learning rate (shrinkage weight) with which each new tree is added.")
parser.add_argument(
"--examples_per_layer",
type=int,
default=5000,
help="Number of examples to accumulate stats for per layer.")
parser.add_argument(
"--num_trees",
type=int,
default=10,
help="Number of trees to grow before stopping.")
FLAGS, unparsed = parser.parse_known_args()
FLAGS.output_dir = 'outputs/tf_t{:03d}_d{:02d}_ex{:05d}'.format(
FLAGS.num_trees, FLAGS.depth, FLAGS.examples_per_layer)
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)