forked from isthegoal/recsys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcn.py
251 lines (203 loc) · 10.1 KB
/
dcn.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
# Auther : wangruichen
# Date : 2019-06-26
# Description : Deep and Cross Network (Using the same code template as xdeepfm)
# Refers :
# Returns :
import tensorflow as tf
from tensorflow_estimator import estimator
import numpy as np
from time import time
import sys
print(tf.__version__)
print("GPU Available: ", tf.test.is_gpu_available())
FLAGS = tf.app.flags.FLAGS
# Model
tf.app.flags.DEFINE_integer("embedding_size", 16, "Embedding size")
tf.app.flags.DEFINE_float("learning_rate", 0.001, "Learning rate")
tf.app.flags.DEFINE_float("dropout", 0.5, "Dropout rate")
tf.app.flags.DEFINE_string("task_type", 'train', "Task type {train, infer, eval, export}")
tf.app.flags.DEFINE_integer("num_epochs", 10, "Number of epochs")
tf.app.flags.DEFINE_string("deep_layers", '100,100', "deep layers")
tf.app.flags.DEFINE_integer("cross_layers", 4, "cross layers")
# Dataset
tf.app.flags.DEFINE_string("train_path", '/home/wangrc/criteo_data/train/', "Data path")
tf.app.flags.DEFINE_integer("train_parts", 150, "Tfrecord counts")
tf.app.flags.DEFINE_integer("eval_parts", 5, "Eval tfrecord")
tf.app.flags.DEFINE_string("test_path", '/home/wangrc/criteo_data/test/', "Test path")
tf.app.flags.DEFINE_integer("test_parts", 15, "Tfrecord counts")
# Config
tf.app.flags.DEFINE_string("export_path", './export/', "Model export path")
tf.app.flags.DEFINE_integer("batch_size", 256, "Number of batch size")
tf.app.flags.DEFINE_integer("log_steps", 100, "Log_step_count_steps")
tf.app.flags.DEFINE_integer("save_checkpoints_steps", 2000, "save_checkpoints_steps")
tf.app.flags.DEFINE_integer("num_parallel", 8, "Number of batch size")
tf.app.flags.DEFINE_boolean("mirror", True, "Mirrored Strategy")
cont_feature = ['_c{0}'.format(i) for i in range(0, 14)]
cat_feature = ['_c{0}'.format(i) for i in range(14, 40)]
# Not setting default value for continuous feature. filled with mean.
feature_description = {k: tf.FixedLenFeature(dtype=tf.float32, shape=1) for k in cont_feature}
feature_description.update({k: tf.FixedLenFeature(dtype=tf.string, shape=1, default_value='NULL') for k in cat_feature})
def build_feature_columns(embedding_size):
cont_feature.remove('_c0')
linear_feature_columns = []
embedding_feature_columns = []
# sorted(list(set(df.approxQuantile("a", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], 0.01))))
c1 = [0.0, 1.0, 2.0, 3.0, 5.0, 12.0]
c2 = [0.0, 1.0, 2.0, 4.0, 10.0, 28.0, 76.0, 301.0]
c3 = [1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 16.0, 24.0, 54.0]
c4 = [1.0, 2.0, 3.0, 5.0, 6.0, 9.0, 13.0, 20.0]
c5 = [20.0, 155.0, 1087.0, 1612.0, 2936.0, 5064.0, 8622.0, 16966.0, 39157.0]
c6 = [3.0, 7.0, 13.0, 24.0, 36.0, 53.0, 85.0, 154.0, 411.0]
c7 = [0.0, 1.0, 2.0, 4.0, 6.0, 10.0, 17.0, 43.0]
c8 = [1.0, 2.0, 4.0, 6.0, 8.0, 12.0, 17.0, 25.0, 37.0]
c9 = [4.0, 8.0, 16.0, 28.0, 41.0, 63.0, 109.0, 147.0, 321.0]
c10 = [0.0, 1.0, 2.0]
c11 = [0.0, 1.0, 2.0, 3.0, 4.0, 8.0]
c12 = [0.0, 1.0, 2.0]
c13 = [1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 14.0, 22.0]
buckets_cont = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13]
buckets_cat = [1460, 583, 10131226, 2202607, 305, 23, 12517, 633, 3, 93145, 5683, 8351592, 3194, 27, 14992, 5461305,
10, 5652, 2172, 3, 7046546, 17, 15, 286180, 104, 142571]
buckets_cat = [1460, 583, 100000, 100000, 305, 23, 12517, 633, 3, 93145, 5683, 100000, 3194, 27, 14992, 100000,
10, 5652, 2172, 3, 100000, 17, 15, 100000, 104, 100000]
for i, j in zip(cont_feature, buckets_cont):
f_num = tf.feature_column.numeric_column(i, normalizer_fn=lambda x: tf.log(x + 1.0))
if i == '_c2':
f_num = tf.feature_column.numeric_column(i, normalizer_fn=lambda x: tf.log(x + 4.0))
f_bucket = tf.feature_column.bucketized_column(f_num, j)
f_embedding = tf.feature_column.embedding_column(f_bucket, embedding_size)
# TODO: With duplicated one-hot or not?
# linear_feature_columns.append(tf.feature_column.indicator_column(f_bucket))
linear_feature_columns.append(f_num)
embedding_feature_columns.append(f_embedding)
for i, j in zip(cat_feature, buckets_cat):
f_cat = tf.feature_column.categorical_column_with_hash_bucket(key=i, hash_bucket_size=j)
f_ind = tf.feature_column.indicator_column(f_cat)
f_embedding = tf.feature_column.embedding_column(f_cat, embedding_size)
# According to the paper, Sparse feature is used only as embeddings.
# linear_feature_columns.append(f_ind)
embedding_feature_columns.append(f_embedding)
return linear_feature_columns, embedding_feature_columns
def _parse_examples(serial_exmp):
features = tf.parse_single_example(serial_exmp, features=feature_description)
labels = features.pop('_c0')
return features, labels
def input_fn(filenames, batch_size, num_epochs=-1, need_shuffle=False):
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(_parse_examples, num_parallel_calls=FLAGS.num_parallel).batch(batch_size)
if need_shuffle:
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.prefetch(buffer_size=1000).repeat(num_epochs)
return dataset
def model_fn(features, labels, mode, params):
layers = list(map(int, params["deep_layers"].split(',')))
# Not following the paper using dense feature here.
# Due to numerical stability
linear_net = tf.feature_column.input_layer(features, params['linear_feature_columns'])
embedding_net = tf.feature_column.input_layer(features, params['embedding_feature_columns'])
x0 = embedding_net
cross_dim = x0.shape[1]
# with tf.name_scope('linear_net'):
# linear_y = tf.layers.dense(linear_net, 1, activation=tf.nn.relu)
with tf.variable_scope('cross_layers'):
xl = x0
for i in range(FLAGS.cross_layers):
# wl = tf.reshape(cross_weight[i], shape=[-1, 1]) # (dim * 1)
# xlw = tf.matmul(xl, wl) # (? * 1)
# xl = x0 * xlw + xl + cross_bias[i] # (? * dim)
with tf.variable_scope('cross_{}'.format(i)):
w = tf.get_variable("weight", [cross_dim], initializer=tf.glorot_normal_initializer())
b = tf.get_variable("bias", [cross_dim], initializer=tf.glorot_normal_initializer())
xw = tf.tensordot(tf.reshape(xl, [-1, 1, cross_dim]), w, 1)
xl = xw * x0 + xl + b
with tf.variable_scope('deep_layers'):
dnn_net = x0
for i in layers:
dnn_net = tf.layers.dense(dnn_net, i, activation=tf.nn.relu)
dnn_net = tf.layers.batch_normalization(dnn_net, training=(mode == estimator.ModeKeys.TRAIN))
dnn_net = tf.layers.dropout(dnn_net, rate=params['dropout'], training=(mode == estimator.ModeKeys.TRAIN))
logits = tf.concat([dnn_net, xl], axis=-1)
logits = tf.layers.dense(logits, units=1, activation=None)
pred = tf.sigmoid(logits)
predictions = {"prob": pred}
export_outputs = {
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: estimator.export.PredictOutput(
predictions)}
if mode == estimator.ModeKeys.PREDICT:
return estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
export_outputs=export_outputs)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=logits,
labels=tf.cast(labels, tf.float32))
)
eval_metric_ops = {
"AUC": tf.metrics.auc(labels, pred),
'Accuracy': tf.metrics.accuracy(labels, predictions=tf.round(pred))
}
if mode == estimator.ModeKeys.EVAL:
return estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=loss,
eval_metric_ops=eval_metric_ops)
optimizer = tf.train.AdamOptimizer(learning_rate=params['learning_rate'])
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
if mode == estimator.ModeKeys.TRAIN:
return estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op)
def main(_):
data_dir = FLAGS.train_path
data_files = []
for i in range(FLAGS.train_parts):
data_files.append(data_dir + 'part-r-{:0>5}'.format(i))
train_files = data_files[:-FLAGS.eval_parts]
eval_files = data_files[-FLAGS.eval_parts:]
linear_feature_columns, embedding_feature_columns = build_feature_columns(FLAGS.embedding_size)
distribute_strategy = None
if FLAGS.mirror:
distribute_strategy = tf.distribute.MirroredStrategy()
config = estimator.RunConfig(
save_checkpoints_steps=FLAGS.save_checkpoints_steps,
keep_checkpoint_max=5,
log_step_count_steps=FLAGS.log_steps,
save_summary_steps=200,
train_distribute=distribute_strategy,
eval_distribute=distribute_strategy
)
model_params = {
'linear_feature_columns': linear_feature_columns,
'embedding_feature_columns': embedding_feature_columns,
'embedding_size': FLAGS.embedding_size,
"learning_rate": FLAGS.learning_rate,
"dropout": FLAGS.dropout,
"deep_layers": FLAGS.deep_layers
}
dcn = estimator.Estimator(
model_fn=model_fn,
model_dir='./model/',
params=model_params,
config=config
)
if FLAGS.task_type == 'train':
train_spec = estimator.TrainSpec(input_fn=lambda: input_fn(
train_files,
num_epochs=FLAGS.num_epochs,
batch_size=FLAGS.batch_size,
need_shuffle=True))
eval_spec = estimator.EvalSpec(input_fn=lambda: input_fn(
eval_files,
num_epochs=-1,
batch_size=FLAGS.batch_size,
need_shuffle=True), steps=200, start_delay_secs=1, throttle_secs=5)
estimator.train_and_evaluate(dcn, train_spec, eval_spec)
elif FLAGS.task_type == 'eval':
dcn.evaluate(input_fn=lambda: input_fn(eval_files, num_epochs=1, batch_size=FLAGS.batch_size), steps=200)
if __name__ == '__main__':
# tf.enable_eager_execution()
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)