-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathfibinet.py
More file actions
328 lines (274 loc) · 15.6 KB
/
Copy pathfibinet.py
File metadata and controls
328 lines (274 loc) · 15.6 KB
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
"""
[1] Tongwen Huang, Zhiqi Zhang, and Junlin Zhang. 2019.
FiBiNET: Combining Feature Importance and Bilinear Feature Interaction for Click-through Rate Prediction.
In ACM Conference on Recommender Systems (RecSys). 169--177.
"""
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
from typing import List, Tuple, Any
import pandas as pd
import tensorflow as tf
from tensorflow import feature_column as fc
from utils import train_input_fn, eval_input_fn
from senet import senet
from bilinear_interaction_layer import bilinear_interaction_layer
# 定义输入参数
flags = tf.app.flags
# 训练参数
flags.DEFINE_string("model_dir", "./model_dir", "Directory where model parameters, graph, etc are saved")
flags.DEFINE_string("output_dir", "./output_dir", "Directory where pb file are saved")
flags.DEFINE_string("train_data", "../../dataset/wechat_algo_data1/tfrecord/train.tfrecord", "Path to the train data")
flags.DEFINE_string("eval_data", "../../dataset/wechat_algo_data1/tfrecord/test.tfrecord",
"Path to the evaluation data")
flags.DEFINE_string("vocabulary_dir", "../../dataset/wechat_algo_data1/vocabulary/",
"Folder where the vocabulary file is stored")
flags.DEFINE_integer("num_epochs", 1, "Epoch of training phase")
flags.DEFINE_integer("train_steps", 10000, "Number of (global) training steps to perform")
flags.DEFINE_integer("shuffle_buffer_size", 10000, "Dataset shuffle buffer size")
flags.DEFINE_integer("num_parallel_readers", -1, "Number of parallel readers for training data")
flags.DEFINE_integer("save_checkpoints_steps", 1000, "Save checkpoints every this many steps")
# 模型参数
flags.DEFINE_integer("batch_size", 1024, "Training batch size")
flags.DEFINE_float("learning_rate", 0.005, "Learning rate")
flags.DEFINE_string("hidden_units", "512,256,128",
"Comma-separated list of number of units in each hidden layer of the deep part")
flags.DEFINE_boolean("batch_norm", True, "Perform batch normalization (True or False)")
flags.DEFINE_float("dropout_rate", 0.1, "Dropout rate")
flags.DEFINE_integer("embedding_dim", 8, "Embedding dimension")
flags.DEFINE_integer("reduction_ratio", 2, "Reduction ratio defined in SENET, must be greater than 1, smaller than number of category features")
flags.DEFINE_enum("bilinear_interaction_type", "all", ["all", "each", "interaction"], "Bilinear interaction type")
FLAGS = flags.FLAGS
def create_feature_columns() -> Tuple[list, list, list]:
"""
Returns:
dense_feature_columns (list): 连续特征的feature_columns
category_feature_columns (list): 类别特征的feature_columns
label_feature_columns (list): 因变量的feature_columns
"""
category_feature_columns, dense_feature_columns = [], []
label_feature_columns = []
# 连续特征
videoplayseconds = fc.numeric_column('videoplayseconds', default_value=0.0)
u_read_comment_7d_sum = fc.numeric_column('u_read_comment_7d_sum', default_value=0.0)
u_like_7d_sum = fc.numeric_column('u_like_7d_sum', default_value=0.0)
u_click_avatar_7d_sum = fc.numeric_column('u_click_avatar_7d_sum', default_value=0.0)
u_forward_7d_sum = fc.numeric_column('u_forward_7d_sum', default_value=0.0)
u_comment_7d_sum = fc.numeric_column('u_comment_7d_sum', default_value=0.0)
u_follow_7d_sum = fc.numeric_column('u_follow_7d_sum', default_value=0.0)
u_favorite_7d_sum = fc.numeric_column('u_favorite_7d_sum', default_value=0.0)
i_read_comment_7d_sum = fc.numeric_column('i_read_comment_7d_sum', default_value=0.0)
i_like_7d_sum = fc.numeric_column('i_like_7d_sum', default_value=0.0)
i_click_avatar_7d_sum = fc.numeric_column('i_click_avatar_7d_sum', default_value=0.0)
i_forward_7d_sum = fc.numeric_column('i_forward_7d_sum', default_value=0.0)
i_comment_7d_sum = fc.numeric_column('i_comment_7d_sum', default_value=0.0)
i_follow_7d_sum = fc.numeric_column('i_follow_7d_sum', default_value=0.0)
i_favorite_7d_sum = fc.numeric_column('i_favorite_7d_sum', default_value=0.0)
c_user_author_read_comment_7d_sum = fc.numeric_column('c_user_author_read_comment_7d_sum', default_value=0.0)
dense_feature_columns += [videoplayseconds, u_read_comment_7d_sum, u_like_7d_sum, u_click_avatar_7d_sum,
u_forward_7d_sum, u_comment_7d_sum, u_follow_7d_sum, u_favorite_7d_sum,
i_read_comment_7d_sum, i_like_7d_sum, i_click_avatar_7d_sum, i_forward_7d_sum,
i_comment_7d_sum, i_follow_7d_sum, i_favorite_7d_sum,
c_user_author_read_comment_7d_sum]
# 类别特征
userid = fc.categorical_column_with_vocabulary_file('userid', os.path.join(FLAGS.vocabulary_dir, 'userid.txt'))
feedid = fc.categorical_column_with_vocabulary_file('feedid', os.path.join(FLAGS.vocabulary_dir, 'feedid.txt'))
device = fc.categorical_column_with_vocabulary_file('device', os.path.join(FLAGS.vocabulary_dir, 'device.txt'))
authorid = fc.categorical_column_with_vocabulary_file('authorid',
os.path.join(FLAGS.vocabulary_dir, 'authorid.txt'))
bgm_song_id = fc.categorical_column_with_vocabulary_file('bgm_song_id',
os.path.join(FLAGS.vocabulary_dir, 'bgm_song_id.txt'))
bgm_singer_id = fc.categorical_column_with_vocabulary_file('bgm_singer_id',
os.path.join(FLAGS.vocabulary_dir, 'bgm_singer_id.txt'))
manual_tag_list = fc.categorical_column_with_vocabulary_file('manual_tag_list',
os.path.join(FLAGS.vocabulary_dir,
'manual_tag_id.txt'))
his_read_comment_7d_seq = fc.categorical_column_with_vocabulary_file('his_read_comment_7d_seq',
os.path.join(FLAGS.vocabulary_dir,
'feedid.txt'))
userid_emb = fc.embedding_column(userid, FLAGS.embedding_dim)
feedid_emb = fc.shared_embedding_columns([feedid, his_read_comment_7d_seq], FLAGS.embedding_dim, combiner='mean')
device_emb = fc.embedding_column(device, FLAGS.embedding_dim)
authorid_emb = fc.embedding_column(authorid, FLAGS.embedding_dim)
bgm_song_id_emb = fc.embedding_column(bgm_song_id, FLAGS.embedding_dim)
bgm_singer_id_emb = fc.embedding_column(bgm_singer_id, FLAGS.embedding_dim)
manual_tag_id_emb = fc.embedding_column(manual_tag_list, FLAGS.embedding_dim, combiner='mean')
category_feature_columns += [userid_emb, device_emb, authorid_emb, bgm_song_id_emb, bgm_singer_id_emb,
manual_tag_id_emb]
category_feature_columns += feedid_emb # feedid_emb是list
# label
read_comment = fc.numeric_column("read_comment", default_value=0.0)
label_feature_columns += [read_comment]
return dense_feature_columns, category_feature_columns, label_feature_columns
def example_parser(serialized_example):
"""
批量解析Example
Args:
serialized_example:
Returns:
features, labels
"""
fea_columns = total_feature_columns
label_columns = label_feature_columns
feature_spec = tf.feature_column.make_parse_example_spec(fea_columns + label_columns)
features = tf.parse_example(serialized_example, features=feature_spec)
read_comment = features.pop("read_comment")
return features, {"read_comment": read_comment}
def fibinet_model_fn(features, labels, mode, params):
"""
fibinet模型的model_fn
Args:
features (dict): input_fn的第一个返回值, 模型输入样本特征
labels (dict): input_fn的第二个返回值, 样本标签
mode: tf.estimator.ModeKeys
params (dict): 模型超参数
Returns:
tf.estimator.EstimatorSpec
"""
# 连续特征
with tf.variable_scope("dense_input"):
dense_input = fc.input_layer(features, params["dense_feature_columns"])
# 类别特征
with tf.variable_scope("category_input"):
category_input = fc.input_layer(features, params["category_feature_columns"]) # (batch, F*K)
category_input = tf.reshape(category_input, shape=(-1, len(params["category_feature_columns"]), FLAGS.embedding_dim)) # (batch, F, K)
# 线性部分
with tf.variable_scope("linear_part"):
# linear_vec = tf.concat([dense_input, category_input], axis=-1)
linear_logit = tf.layers.dense(dense_input, 1, activation=None, use_bias=True) # (batch, 1)
# senet
with tf.variable_scope("senet_part"):
senet_output = senet(category_input,
embedding_dim=params["embedding_dim"],
reduction_ratio=params["reduction_ratio"]) # (batch, F, K)
# bilinear interaction
with tf.variable_scope("bilinear_interaction_part"):
bi_interction_orginal = bilinear_interaction_layer(category_input,
embedding_dim=params["embedding_dim"],
type=params["bilinear_interaction_type"],
name="orginal") # (batch, F*(F-1)/2, K)
bi_interction_senet = bilinear_interaction_layer(senet_output,
embedding_dim=params["embedding_dim"],
type=params["bilinear_interaction_type"],
name="senet") # (batch, F*(F-1)/2, K)
bi_interction_total = tf.concat([bi_interction_orginal, bi_interction_senet], axis=-1) # # (batch, F*(F-1)/2, 2K)
bi_interction_total = tf.layers.flatten(bi_interction_total) # (batch, F*(F-1)*K)
with tf.variable_scope("dnn_part"):
net = bi_interction_total
for unit in params["hidden_units"]:
net = tf.layers.dense(net, unit, activation=tf.nn.relu)
if "dropout_rate" in params and 0.0 < params["dropout_rate"] < 1.0:
net = tf.layers.dropout(net, params["dropout_rate"], training=(mode == tf.estimator.ModeKeys.TRAIN))
if params["batch_norm"]:
net = tf.layers.batch_normalization(net, training=(mode == tf.estimator.ModeKeys.TRAIN))
fibinet_logit = tf.layers.dense(net, 1) # (batch, 1)
total_logit = linear_logit + fibinet_logit
# -----定义PREDICT阶段行为-----
prediction = tf.sigmoid(total_logit, name="prediction")
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
"logit": total_logit,
'probabilities': prediction,
}
saved_model_output = {
'probabilities': prediction,
}
export_outputs = {
'prediction': tf.estimator.export.PredictOutput(saved_model_output)
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions, export_outputs=export_outputs)
# -----定义完毕-----
y = labels["read_comment"]
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=total_logit), name="loss")
accuracy = tf.metrics.accuracy(labels=y, predictions=tf.to_float(tf.greater_equal(prediction, 0.5)))
auc = tf.metrics.auc(labels=y, predictions=prediction)
# -----定义EVAL阶段行为-----
metrics = {"eval_accuracy": accuracy, "eval_auc": auc}
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)
# -----定义完毕-----
optimizer = tf.train.AdamOptimizer(learning_rate=params["learning_rate"], beta1=0.9,
beta2=0.999, epsilon=1e-8)
update_ops = tf.compat.v1.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
# -----定义TRAIN阶段行为-----
assert mode == tf.estimator.ModeKeys.TRAIN
# tensorboard收集
tf.summary.scalar("train_accuracy", accuracy[1])
tf.summary.scalar("train_auc", auc[1])
# 训练log打印
log_hook = tf.train.LoggingTensorHook(
{
"train_loss": loss,
"train_auc": auc[1],
"linear_logit": linear_logit,
"fibinet_logit": fibinet_logit,
},
every_n_iter=100
)
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op, training_hooks=[log_hook])
# -----定义完毕-----
def main(unused_argv):
"""训练入口"""
global total_feature_columns, label_feature_columns
dense_feature_columns, category_feature_columns, label_feature_columns = create_feature_columns()
total_feature_columns = dense_feature_columns + category_feature_columns
params = {
"category_feature_columns": category_feature_columns,
"dense_feature_columns": dense_feature_columns,
"hidden_units": FLAGS.hidden_units.split(','),
"dropout_rate": FLAGS.dropout_rate,
"batch_norm": FLAGS.batch_norm,
"learning_rate": FLAGS.learning_rate,
"embedding_dim": FLAGS.embedding_dim,
"reduction_ratio": FLAGS.reduction_ratio,
"bilinear_interaction_type": FLAGS.bilinear_interaction_type,
}
print(params)
estimator = tf.estimator.Estimator(
model_fn=fibinet_model_fn,
params=params,
config=tf.estimator.RunConfig(model_dir=FLAGS.model_dir,
save_checkpoints_steps=FLAGS.save_checkpoints_steps)
)
train_spec = tf.estimator.TrainSpec(
input_fn=lambda: train_input_fn(filepath=FLAGS.train_data, example_parser=example_parser,
batch_size=FLAGS.batch_size, num_epochs=FLAGS.num_epochs,
shuffle_buffer_size=FLAGS.shuffle_buffer_size),
max_steps=FLAGS.train_steps
)
feature_spec = tf.feature_column.make_parse_example_spec(total_feature_columns)
serving_input_receiver_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
exporters = [
tf.estimator.BestExporter(
name="best_exporter",
serving_input_receiver_fn=serving_input_receiver_fn,
exports_to_keep=5)
]
eval_spec = tf.estimator.EvalSpec(
input_fn=lambda: eval_input_fn(filepath=FLAGS.eval_data, example_parser=example_parser,
batch_size=FLAGS.batch_size),
throttle_secs=600,
steps=None,
exporters=exporters
)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
# Evaluate Metrics.
metrics = estimator.evaluate(
input_fn=lambda: eval_input_fn(filepath=FLAGS.eval_data, example_parser=example_parser,
batch_size=FLAGS.batch_size))
for key in sorted(metrics):
print('%s: %s' % (key, metrics[key]))
results = estimator.predict(
input_fn=lambda: eval_input_fn(filepath=FLAGS.eval_data, example_parser=example_parser,
batch_size=FLAGS.batch_size))
predicts_df = pd.DataFrame.from_dict(results)
predicts_df['probabilities'] = predicts_df['probabilities'].apply(lambda x: x[0])
test_df = pd.read_csv("../../dataset/wechat_algo_data1/dataframe/test.csv")
predicts_df['read_comment'] = test_df['read_comment']
predicts_df.to_csv("predictions.csv")
print("after evaluate")
if __name__ == "__main__":
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main=main)