-
Notifications
You must be signed in to change notification settings - Fork 31
/
wdl_train_eval_test.py
239 lines (209 loc) · 10.7 KB
/
wdl_train_eval_test.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
"""
Copyright 2020 The OneFlow Authors. All rights reserved.
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.
"""
import argparse
import oneflow.compatible.single_client as flow
import datetime
import os
import glob
from sklearn.metrics import roc_auc_score
import numpy as np
import time
parser = argparse.ArgumentParser()
parser.add_argument('--train_data_dir', type=str, required=True)
parser.add_argument('--train_data_part_num', type=int, required=True)
parser.add_argument('--train_part_name_suffix_length', type=int, default=-1)
parser.add_argument('--train_data_num', type=int, default=36674623)
parser.add_argument('--eval_data_dir', type=str, required=True)
parser.add_argument('--eval_data_part_num', type=int, required=True)
parser.add_argument('--eval_part_name_suffix_length', type=int, default=-1)
parser.add_argument('--eval_data_num', type=int, default=4583478)
parser.add_argument('--test_data_dir', type=str, required=True)
parser.add_argument('--test_data_part_num', type=int, required=True)
parser.add_argument('--test_part_name_suffix_length', type=int, default=-1)
parser.add_argument('--test_data_num', type=int, default=4582516)
parser.add_argument('--batch_size', type=int, default=16384)
parser.add_argument('--learning_rate', type=float, default=1e-3)
parser.add_argument('--wide_vocab_size', type=int, default=3200000)
parser.add_argument('--deep_vocab_size', type=int, default=3200000)
parser.add_argument('--deep_embedding_vec_size', type=int, default=16)
parser.add_argument('--deep_dropout_rate', type=float, default=0.5)
parser.add_argument('--num_dense_fields', type=int, default=13)
parser.add_argument('--num_wide_sparse_fields', type=int, default=2)
parser.add_argument('--num_deep_sparse_fields', type=int, default=26)
parser.add_argument('--epoch_num', type=int, default=4)
parser.add_argument('--loss_print_every_n_iter', type=int, default=100)
parser.add_argument('--gpu_num', type=int, default=8)
parser.add_argument('--hidden_units_num', type=int, default=7)
parser.add_argument('--hidden_size', type=int, default=1024)
FLAGS = parser.parse_args()
#DEEP_HIDDEN_UNITS = [1024, 1024]#, 1024, 1024, 1024, 1024, 1024]
DEEP_HIDDEN_UNITS = [FLAGS.hidden_size for i in range(FLAGS.hidden_units_num)]
print(DEEP_HIDDEN_UNITS)
train_epoch_size = FLAGS.train_data_num // FLAGS.batch_size + 1
eval_epoch_size = FLAGS.eval_data_num // FLAGS.batch_size + 1
test_epoch_size = FLAGS.test_data_num // FLAGS.batch_size + 1
def _data_loader_ofrecord(data_dir, data_part_num, batch_size, part_name_suffix_length=-1,
shuffle=True):
ofrecord = flow.data.ofrecord_reader(data_dir,
batch_size=batch_size,
data_part_num=data_part_num,
part_name_suffix_length=part_name_suffix_length,
random_shuffle=shuffle,
shuffle_after_epoch=shuffle)
def _blob_decoder(bn, shape, dtype=flow.int32):
return flow.data.OFRecordRawDecoder(ofrecord, bn, shape=shape, dtype=dtype)
labels = _blob_decoder("labels", (1,))
dense_fields = _blob_decoder("dense_fields", (FLAGS.num_dense_fields,), flow.float)
wide_sparse_fields = _blob_decoder("wide_sparse_fields", (FLAGS.num_wide_sparse_fields,))
deep_sparse_fields = _blob_decoder("deep_sparse_fields", (FLAGS.num_deep_sparse_fields,))
return flow.identity_n([labels, dense_fields, wide_sparse_fields, deep_sparse_fields])
def _model(dense_fields, wide_sparse_fields, deep_sparse_fields):
wide_sparse_fields = flow.parallel_cast(wide_sparse_fields, distribute=flow.distribute.broadcast())
wide_embedding_table = flow.get_variable(
name='wide_embedding',
shape=(FLAGS.wide_vocab_size, 1),
initializer=flow.random_uniform_initializer(minval=-0.05, maxval=0.05),
distribute=flow.distribute.split(0),
)
wide_embedding = flow.gather(params=wide_embedding_table, indices=wide_sparse_fields)
wide_embedding = flow.reshape(wide_embedding, shape=(-1, wide_embedding.shape[-1] * wide_embedding.shape[-2]))
wide_scores = flow.math.reduce_sum(wide_embedding, axis=[1], keepdims=True)
wide_scores = flow.parallel_cast(wide_scores, distribute=flow.distribute.split(0),
gradient_distribute=flow.distribute.broadcast())
deep_sparse_fields = flow.parallel_cast(deep_sparse_fields, distribute=flow.distribute.broadcast())
deep_embedding_table = flow.get_variable(
name='deep_embedding',
shape=(FLAGS.deep_vocab_size, FLAGS.deep_embedding_vec_size),
initializer=flow.random_uniform_initializer(minval=-0.05, maxval=0.05),
distribute=flow.distribute.split(1),
)
deep_embedding = flow.gather(params=deep_embedding_table, indices=deep_sparse_fields)
deep_embedding = flow.parallel_cast(deep_embedding, distribute=flow.distribute.split(0),
gradient_distribute=flow.distribute.split(2))
deep_embedding = flow.reshape(deep_embedding, shape=(-1, deep_embedding.shape[-1] * deep_embedding.shape[-2]))
deep_features = flow.concat([deep_embedding, dense_fields], axis=1)
for idx, units in enumerate(DEEP_HIDDEN_UNITS):
deep_features = flow.layers.dense(
deep_features,
units=units,
kernel_initializer=flow.glorot_uniform_initializer(),
bias_initializer=flow.constant_initializer(0.0),
activation=flow.math.relu,
name='fc' + str(idx + 1)
)
deep_features = flow.nn.dropout(deep_features, rate=FLAGS.deep_dropout_rate)
deep_scores = flow.layers.dense(
deep_features,
units=1,
kernel_initializer=flow.glorot_uniform_initializer(),
bias_initializer=flow.constant_initializer(0.0),
name='fc' + str(len(DEEP_HIDDEN_UNITS) + 1)
)
scores = wide_scores + deep_scores
return scores
global_loss = 0.0
def _create_train_callback(epoch, step):
def nop(loss):
global global_loss
global_loss += loss.mean()
pass
def print_loss(loss):
global global_loss
global_loss += loss.mean()
print(epoch, step+1, 'time', datetime.datetime.now(), 'loss',
global_loss/FLAGS.loss_print_every_n_iter)
global_loss = 0.0
if (step + 1) % FLAGS.loss_print_every_n_iter == 0:
return print_loss
else:
return nop
def CreateOptimizer(args):
lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [args.learning_rate])
return flow.optimizer.LARS(lr_scheduler)
def _get_train_conf():
train_conf = flow.FunctionConfig()
train_conf.default_data_type(flow.float)
train_conf.indexed_slices_optimizer_conf(dict(include_op_names=dict(op_name=['wide_embedding', 'deep_embedding'])))
return train_conf
@flow.global_function('train', _get_train_conf())
def train_job():
labels, dense_fields, wide_sparse_fields, deep_sparse_fields = \
_data_loader_ofrecord(data_dir=FLAGS.train_data_dir,
data_part_num=FLAGS.train_data_part_num,
batch_size=FLAGS.batch_size,
part_name_suffix_length=FLAGS.train_part_name_suffix_length,
shuffle=True)
logits = _model(dense_fields, wide_sparse_fields, deep_sparse_fields)
loss = flow.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)
opt = CreateOptimizer(FLAGS)
opt.minimize(loss)
return loss
@flow.global_function()
def eval_job():
labels, dense_fields, wide_sparse_fields, deep_sparse_fields = \
_data_loader_ofrecord(data_dir=FLAGS.eval_data_dir,
data_part_num=FLAGS.eval_data_part_num,
batch_size=FLAGS.batch_size,
part_name_suffix_length=FLAGS.eval_part_name_suffix_length,
shuffle=False)
logits = _model(dense_fields, wide_sparse_fields, deep_sparse_fields)
loss = flow.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)
predict = flow.math.sigmoid(logits)
return loss, predict, labels
@flow.global_function()
def test_job():
labels, dense_fields, wide_sparse_fields, deep_sparse_fields = \
_data_loader_ofrecord(data_dir=FLAGS.test_data_dir,
data_part_num=FLAGS.test_data_part_num,
batch_size=FLAGS.batch_size,
part_name_suffix_length=FLAGS.test_part_name_suffix_length,
shuffle=False)
logits = _model(dense_fields, wide_sparse_fields, deep_sparse_fields)
loss = flow.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)
predict = flow.math.sigmoid(logits)
return loss, predict, labels
def main():
flow.config.gpu_device_num(FLAGS.gpu_num)
#flow.config.enable_numa_aware_cuda_malloc_host(True)
#flow.config.collective_boxing.enable_fusion(False)
check_point = flow.train.CheckPoint()
check_point.init()
global global_loss
for epoch in range(FLAGS.epoch_num):
global_loss = 0.0
for i in range(train_epoch_size):
train_job().async_get(_create_train_callback(epoch, i))
labels = np.array([[0]])
preds = np.array([[0]])
eval_loss = 0.0
for i in range(eval_epoch_size):
loss, pred, ref = eval_job().get()
label_ = ref.numpy().astype(np.float32)
labels = np.concatenate((labels, label_), axis=0)
preds = np.concatenate((preds, pred.numpy()), axis=0)
eval_loss += loss.mean()
auc = roc_auc_score(labels[1:], preds[1:])
print(epoch, "eval_loss", eval_loss/eval_epoch_size, "eval_auc", auc)
labels = np.array([[0]])
preds = np.array([[0]])
eval_loss = 0.0
for i in range(test_epoch_size):
loss, pred, ref = test_job().get()
label_ = ref.numpy().astype(np.float32)
labels = np.concatenate((labels, label_), axis=0)
preds = np.concatenate((preds, pred.numpy()), axis=0)
eval_loss += loss.mean()
auc = roc_auc_score(labels[1:], preds[1:])
print("test_loss", eval_loss/test_epoch_size, "eval_auc", auc)
if __name__ == '__main__':
main()