-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdeep_fm.py
209 lines (182 loc) · 8.58 KB
/
deep_fm.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
import shutil
from argparse import ArgumentParser
import tensorflow as tf
from trainers.conf_utils import get_run_config, get_train_spec, get_exporter, get_eval_spec
from trainers.ml_100k import get_feature_columns, get_input_fn, serving_input_fn
from trainers.model_utils import layer_summary, get_optimizer
def model_fn(features, labels, mode, params):
# feature columns
categorical_columns = params.get("categorical_columns", [])
numeric_columns = params.get("numeric_columns", [])
# structure components
use_linear = params.get("use_linear", True)
use_mf = params.get("use_mf", True)
use_dnn = params.get("use_dnn", True)
# structure params
embedding_size = params.get("embedding_size", 4)
hidden_units = params.get("hidden_units", [16, 16])
activation_fn = params.get("activation", tf.nn.relu)
dropout = params.get("dropout", 0)
# training params
optimizer = params.get("optimizer", "Adam")
learning_rate = params.get("learning_rate", 0.001)
# check params
categorical_dim = len(categorical_columns)
numeric_dim = len(numeric_columns)
if (categorical_dim + numeric_dim) == 0:
raise ValueError("At least 1 feature column of categorical_columns or numeric_columns must be specified.")
if not (use_linear or use_mf or use_dnn):
raise ValueError("At least 1 of linear, mf or dnn component must be used.")
logits = 0
if use_linear:
with tf.variable_scope("linear"):
linear_logit = tf.feature_column.linear_model(features, categorical_columns + numeric_columns)
# [None, 1]
with tf.name_scope("linear"):
layer_summary(linear_logit)
logits += linear_logit
# [None, 1]
if use_mf or use_dnn:
with tf.variable_scope("input_layer"):
# categorical input
categorical_dim = len(categorical_columns)
if categorical_dim > 0:
embedding_columns = [tf.feature_column.embedding_column(col, embedding_size)
for col in categorical_columns]
embedding_inputs = tf.feature_column.input_layer(features, embedding_columns)
# [None, c_d * embedding_size]
input_layer = embedding_inputs
# [None, c_d * embedding_size]
# numeric input
numeric_dim = len(numeric_columns)
if numeric_dim > 0:
numeric_inputs = tf.expand_dims(tf.feature_column.input_layer(features, numeric_columns), -1)
# [None, n_d, 1]
numeric_embeddings = tf.get_variable("numeric_embeddings", [1, numeric_dim, embedding_size])
# [1, n_d, embedding_size]
numeric_embedding_inputs = tf.reshape(numeric_embeddings * numeric_inputs,
[-1, numeric_dim * embedding_size])
# [None, n_d * embedding_size]
input_layer = numeric_embedding_inputs
# [None, n_d * embedding_size]
if categorical_dim > 0:
input_layer = tf.concat([embedding_inputs, numeric_embedding_inputs], 1)
# [None, d * embedding_size]
if use_mf:
with tf.variable_scope("mf"):
# reshape flat embedding input layer to matrix
embedding_mat = tf.reshape(input_layer, [-1, categorical_dim + numeric_dim, embedding_size])
# [None, d, embedding_size]
sum_square = tf.square(tf.reduce_sum(embedding_mat, 1))
# [None, embedding_size]
square_sum = tf.reduce_sum(tf.square(embedding_mat), 1)
# [None, embedding_size]
with tf.name_scope("logits"):
mf_logit = 0.5 * tf.reduce_sum(sum_square - square_sum, 1, keepdims=True)
# [None, 1]
layer_summary(mf_logit)
logits += mf_logit
# [None, 1]
if use_dnn:
with tf.variable_scope("dnn/dnn"):
net = input_layer
# [None, d * embedding_size]
for i, hidden_size in enumerate(hidden_units):
with tf.variable_scope("hiddenlayer_%s" % i):
net = tf.layers.dense(net, hidden_size, activation=activation_fn)
# [None, hidden_size]
if dropout > 0 and mode == tf.estimator.ModeKeys.TRAIN:
net = tf.layers.dropout(net, rate=dropout, training=True)
# [None, hidden_size]
layer_summary(net)
with tf.variable_scope('logits'):
dnn_logit = tf.layers.dense(net, 1)
# [None, 1]
layer_summary(dnn_logit)
logits += dnn_logit
# [None, 1]
with tf.name_scope("deep_fm/logits"):
layer_summary(logits)
optimizer = get_optimizer(optimizer, learning_rate)
head = tf.contrib.estimator.binary_classification_head()
return head.create_estimator_spec(
features=features,
mode=mode,
labels=labels,
optimizer=optimizer,
logits=logits
)
def train_and_evaluate(args):
# paths
train_csv = args.train_csv
test_csv = args.test_csv
job_dir = args.job_dir
restore = args.restore
# model
use_linear = not args.exclude_linear,
use_mf = not args.exclude_mf,
use_dnn = not args.exclude_dnn,
embedding_size = args.embedding_size
hidden_units = args.hidden_units
dropout = args.dropout
# training
batch_size = args.batch_size
train_steps = args.train_steps
# init
tf.logging.set_verbosity(tf.logging.INFO)
if not restore:
shutil.rmtree(job_dir, ignore_errors=True)
# estimator
feature_columns = get_feature_columns(embedding_size=embedding_size)
run_config = get_run_config()
estimator = tf.estimator.Estimator(
model_fn=model_fn,
model_dir=job_dir,
config=run_config,
params={
"categorical_columns": feature_columns["linear"],
"use_linear": use_linear,
"use_mf": use_mf,
"use_dnn": use_dnn,
"embedding_size": embedding_size,
"hidden_units": hidden_units,
"dropout": dropout,
}
)
# train spec
train_input_fn = get_input_fn(train_csv, batch_size=batch_size)
train_spec = get_train_spec(train_input_fn, train_steps)
# eval spec
eval_input_fn = get_input_fn(test_csv, tf.estimator.ModeKeys.EVAL, batch_size=batch_size)
exporter = get_exporter(serving_input_fn)
eval_spec = get_eval_spec(eval_input_fn, exporter)
# train and evaluate
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--train-csv", default="data/ml-100k/train.csv",
help="path to the training csv data (default: %(default)s)")
parser.add_argument("--test-csv", default="data/ml-100k/test.csv",
help="path to the test csv data (default: %(default)s)")
parser.add_argument("--job-dir", default="checkpoints/deep_fm",
help="job directory (default: %(default)s)")
parser.add_argument("--restore", action="store_true",
help="whether to restore from job_dir")
parser.add_argument("--exclude-linear", action="store_true",
help="flag to exclude linear component (default: %(default)s)")
parser.add_argument("--exclude-mf", action="store_true",
help="flag to exclude mf component (default: %(default)s)")
parser.add_argument("--exclude-dnn", action="store_true",
help="flag to exclude dnn component (default: %(default)s)")
parser.add_argument("--embedding-size", type=int, default=4,
help="embedding size (default: %(default)s)")
parser.add_argument("--hidden-units", type=int, nargs='+', default=[16, 16],
help="hidden layer specification (default: %(default)s)")
parser.add_argument("--dropout", type=float, default=0.1,
help="dropout rate (default: %(default)s)")
parser.add_argument("--batch-size", type=int, default=32,
help="batch size (default: %(default)s)")
parser.add_argument("--train-steps", type=int, default=20000,
help="number of training steps (default: %(default)s)")
args = parser.parse_args()
train_and_evaluate(args)