提交 918ed9f7 编写于 作者: Q Qiao Longfei

fix infer

上级 5f1fd923
...@@ -61,14 +61,14 @@ def infer(): ...@@ -61,14 +61,14 @@ def infer():
startup_program = fluid.framework.Program() startup_program = fluid.framework.Program()
test_program = fluid.framework.Program() test_program = fluid.framework.Program()
with fluid.framework.program_guard(test_program, startup_program): with fluid.framework.program_guard(test_program, startup_program):
loss, data_list, auc_var, batch_auc_var = ctr_dnn_model(args.embedding_size, args.sparse_feature_dim) loss, auc_var, batch_auc_var, _, data_list = ctr_dnn_model(args.embedding_size, args.sparse_feature_dim, False)
exe = fluid.Executor(place) exe = fluid.Executor(place)
feeder = fluid.DataFeeder(feed_list=data_list, place=place) feeder = fluid.DataFeeder(feed_list=data_list, place=place)
with fluid.scope_guard(inference_scope): fluid.io.load_persistables(executor=exe, dirname=args.model_path,
[inference_program, _, fetch_targets] = fluid.io.load_inference_model(args.model_path, exe) main_program=fluid.default_main_program())
def set_zero(var_name): def set_zero(var_name):
param = inference_scope.var(var_name).get_tensor() param = inference_scope.var(var_name).get_tensor()
...@@ -80,9 +80,9 @@ def infer(): ...@@ -80,9 +80,9 @@ def infer():
set_zero(name) set_zero(name)
for batch_id, data in enumerate(test_reader()): for batch_id, data in enumerate(test_reader()):
loss_val, auc_val = exe.run(inference_program, loss_val, auc_val = exe.run(test_program,
feed=feeder.feed(data), feed=feeder.feed(data),
fetch_list=fetch_targets) fetch_list=[loss, auc_var])
if batch_id % 100 == 0: if batch_id % 100 == 0:
logger.info("TEST --> batch: {} loss: {} auc: {}".format(batch_id, loss_val/args.batch_size, auc_val)) logger.info("TEST --> batch: {} loss: {} auc: {}".format(batch_id, loss_val/args.batch_size, auc_val))
......
...@@ -104,7 +104,7 @@ def ctr_deepfm_model(factor_size, sparse_feature_dim, dense_feature_dim, sparse_ ...@@ -104,7 +104,7 @@ def ctr_deepfm_model(factor_size, sparse_feature_dim, dense_feature_dim, sparse_
return avg_cost, auc_var, batch_auc_var, py_reader return avg_cost, auc_var, batch_auc_var, py_reader
def ctr_dnn_model(embedding_size, sparse_feature_dim): def ctr_dnn_model(embedding_size, sparse_feature_dim, use_py_reader=True):
def embedding_layer(input): def embedding_layer(input):
return fluid.layers.embedding( return fluid.layers.embedding(
...@@ -126,13 +126,15 @@ def ctr_dnn_model(embedding_size, sparse_feature_dim): ...@@ -126,13 +126,15 @@ def ctr_dnn_model(embedding_size, sparse_feature_dim):
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.layers.data(name='label', shape=[1], dtype='int64')
datas = [dense_input] + sparse_input_ids + [label] words = [dense_input] + sparse_input_ids + [label]
py_reader = fluid.layers.create_py_reader_by_data(capacity=64, py_reader = None
feed_list=datas, if use_py_reader:
name='py_reader', py_reader = fluid.layers.create_py_reader_by_data(capacity=64,
use_double_buffer=True) feed_list=words,
words = fluid.layers.read_file(py_reader) name='py_reader',
use_double_buffer=True)
words = fluid.layers.read_file(py_reader)
sparse_embed_seq = list(map(embedding_layer, words[1:-1])) sparse_embed_seq = list(map(embedding_layer, words[1:-1]))
concated = fluid.layers.concat(sparse_embed_seq + words[0:1], axis=1) concated = fluid.layers.concat(sparse_embed_seq + words[0:1], axis=1)
...@@ -156,4 +158,4 @@ def ctr_dnn_model(embedding_size, sparse_feature_dim): ...@@ -156,4 +158,4 @@ def ctr_dnn_model(embedding_size, sparse_feature_dim):
auc_var, batch_auc_var, auc_states = \ auc_var, batch_auc_var, auc_states = \
fluid.layers.auc(input=predict, label=words[-1], num_thresholds=2 ** 12, slide_steps=20) fluid.layers.auc(input=predict, label=words[-1], num_thresholds=2 ** 12, slide_steps=20)
return avg_cost, auc_var, batch_auc_var, py_reader return avg_cost, auc_var, batch_auc_var, py_reader, words
...@@ -163,7 +163,8 @@ def train_loop(args, train_program, py_reader, loss, auc_var, batch_auc_var, ...@@ -163,7 +163,8 @@ def train_loop(args, train_program, py_reader, loss, auc_var, batch_auc_var,
if batch_id % 1000 == 0 and batch_id != 0: if batch_id % 1000 == 0 and batch_id != 0:
model_dir = args.model_output_dir + '/batch-' + str(batch_id) model_dir = args.model_output_dir + '/batch-' + str(batch_id)
if args.trainer_id == 0: if args.trainer_id == 0:
fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe) fluid.io.save_persistables(executor=exe, dirname=model_dir,
main_program=fluid.default_main_program())
batch_id += 1 batch_id += 1
except fluid.core.EOFException: except fluid.core.EOFException:
py_reader.reset() py_reader.reset()
...@@ -171,7 +172,8 @@ def train_loop(args, train_program, py_reader, loss, auc_var, batch_auc_var, ...@@ -171,7 +172,8 @@ def train_loop(args, train_program, py_reader, loss, auc_var, batch_auc_var,
model_dir = args.model_output_dir + '/pass-' + str(pass_id) model_dir = args.model_output_dir + '/pass-' + str(pass_id)
if args.trainer_id == 0: if args.trainer_id == 0:
fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe) fluid.io.save_persistables(executor=exe, dirname=model_dir,
main_program=fluid.default_main_program())
def train(): def train():
...@@ -180,7 +182,7 @@ def train(): ...@@ -180,7 +182,7 @@ def train():
if not os.path.isdir(args.model_output_dir): if not os.path.isdir(args.model_output_dir):
os.mkdir(args.model_output_dir) os.mkdir(args.model_output_dir)
loss, auc_var, batch_auc_var, py_reader = ctr_dnn_model(args.embedding_size, args.sparse_feature_dim) loss, auc_var, batch_auc_var, py_reader, _ = ctr_dnn_model(args.embedding_size, args.sparse_feature_dim)
optimizer = fluid.optimizer.Adam(learning_rate=1e-4) optimizer = fluid.optimizer.Adam(learning_rate=1e-4)
optimizer.minimize(loss) optimizer.minimize(loss)
if args.cloud_train: if args.cloud_train:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册