未验证 提交 be6cf7ed 编写于 作者: D Dong Daxiang 提交者: GitHub

Update cluster_quick_start.rst

上级 fb6d1b5c
...@@ -7,120 +7,100 @@ ...@@ -7,120 +7,100 @@
为了让读者快速上手,我们采用点击率预估任务作为示例,相关的源码可以参考 xxxx 为了让读者快速上手,我们采用点击率预估任务作为示例,相关的源码可以参考 xxxx
单机训练代码 为了方便学习,这里给出的示例是单机与多机混合的代码,用户可以通过不同的启动命令进行单机或多机任务的启动。
.. code:: python .. code:: python
def train(): from __future__ import print_function
from args import parse_args
import os
import paddle.fluid as fluid
import sys
from network_conf import ctr_dnn_model_dataset
import paddle.fluid.incubate.fleet.base.role_maker as role_maker
from paddle.fluid.incubate.fleet.parameter_server.distribute_transpiler import fleet
from paddle.fluid.transpiler.distribute_transpiler import DistributeTranspilerConfig
dense_feature_dim = 13
sparse_feature_dim = 10000001
batch_size = 100
thread_num = 10
embedding_size = 10
args = parse_args() args = parse_args()
if not os.path.isdir(args.model_output_dir):
os.mkdir(args.model_output_dir)
def main_function(is_local):
dense_input = fluid.layers.data( dense_input = fluid.layers.data(
name="dense_input", shape=[dense_feature_dim], dtype='float32') name="dense_input", shape=[dense_feature_dim], dtype='float32')
sparse_input_ids = [ sparse_input_ids = [
fluid.layers.data(name="C" + str(i), shape=[1], lod_level=1, dtype="int64") fluid.layers.data(name="C" + str(i), shape=[1], lod_level=1,
for i in range(1, 27)] dtype="int64") for i in range(1, 27)]
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.layers.data(name="label", shape=[1], dtype="int64")
loss, auc_var, batch_auc_var = ctr_dnn_model_dataset(dense_input, sparse_input_ids, label,
args.embedding_size, args.sparse_feature_dim)
optimizer = fluid.optimizer.SGD(learning_rate=1e-4)
optimizer.minimize(loss)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
dataset = fluid.DatasetFactory().create_dataset() dataset = fluid.DatasetFactory().create_dataset()
dataset.set_use_var([dense_input] + sparse_input_ids + [label]) dataset.set_use_var([dense_input] + sparse_input_ids + [label])
pipe_command = "python criteo_reader.py %d" % args.sparse_feature_dim pipe_command = "python criteo_reader.py %d" % sparse_feature_dim
dataset.set_pipe_command(pipe_command) dataset.set_pipe_command(pipe_command)
dataset.set_batch_size(100) dataset.set_batch_size(batch_size)
thread_num = 10
dataset.set_thread(thread_num) dataset.set_thread(thread_num)
whole_filelist = ["raw_data/part-%d" % x for x in range(len(os.listdir("raw_data")))] whole_filelist = ["raw_data/part-%d" % x
for x in range(len(os.listdir("raw_data")))]
dataset.set_filelist(whole_filelist)
loss, auc_var, batch_auc_var = ctr_dnn_model_dataset(
dense_input, sparse_input_ids, label, embedding_size,
sparse_feature_dim)
epochs = 20 exe = fluid.Executor(fluid.CPUPlace())
for i in range(epochs):
dataset.set_filelist(whole_filelist[:int(0.8*len(whole_filelist))]) def train_loop(epoch=20):
for i in range(epoch):
exe.train_from_dataset(program=fluid.default_main_program(), exe.train_from_dataset(program=fluid.default_main_program(),
dataset=dataset, dataset=dataset,
fetch_list=[auc_var], fetch_list=[auc_var],
fetch_info=["auc"], fetch_info=["auc"],
debug=False) debug=False)
model_dir = args.model_output_dir + '/epoch' + str(i + 1) + ".model"
sys.stderr.write("epoch%d finished" % (i + 1))
fluid.io.save_inference_model(model_dir, [dense_input.name] + [x.name for x in sparse_input_ids] + [label.name], [loss, auc_var], exe)
使用FleetAPI进行训练的代码
.. code:: python def local_train(optimizer):
optimizer = fluid.optimizer.SGD(learning_rate=1e-4)
import paddle.fluid.incubate.fleet.base.role_maker as role_maker optimizer.minimize(loss)
from paddle.fluid.incubate.fleet.parameter_server.distribute_transpiler import fleet exe.run(fluid.default_startup_program())
train_loop()
def train():
args = parse_args()
if not os.path.isdir(args.model_output_dir):
os.mkdir(args.model_output_dir)
dense_input = fluid.layers.data(
name="dense_input", shape=[dense_feature_dim], dtype='float32')
sparse_input_ids = [
fluid.layers.data(name="C" + str(i), shape=[1], lod_level=1, dtype="int64")
for i in range(1, 27)]
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
loss, auc_var, batch_auc_var = ctr_dnn_model_dataset(dense_input, sparse_input_ids, label,
args.embedding_size, args.sparse_feature_dim)
def dist_train(optimizer):
role = role_maker.PaddleCloudRoleMaker() role = role_maker.PaddleCloudRoleMaker()
exe = fluid.Executor(fluid.CPUPlace())
fleet.init(role) fleet.init(role)
optimizer = fluid.optimizer.SGD(learning_rate=1e-4)
strategy = DistributeTranspilerConfig() strategy = DistributeTranspilerConfig()
strategy.sync_mode = False strategy.sync_mode = False
optimizer = fluid.optimizer.SGD(learning_rate=1e-4)
optimizer = fleet.distributed_optimizer(optimizer, strategy) optimizer = fleet.distributed_optimizer(optimizer, strategy)
optimizer.minimize(loss) optimizer.minimize(loss)
if fleet.is_server(): if fleet.is_server():
fleet.init_server() fleet.init_server()
fleet.run_server() fleet.run_server()
elif fleet.is_worker(): elif fleet.is_worker():
fleet.init_worker() fleet.init_worker()
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
dataset = fluid.DatasetFactory().create_dataset() train_loop()
dataset.set_use_var([dense_input] + sparse_input_ids + [label])
pipe_command = "python criteo_reader.py %d" % args.sparse_feature_dim
dataset.set_pipe_command(pipe_command)
dataset.set_batch_size(100)
thread_num = 10
dataset.set_thread(thread_num)
whole_filelist = ["raw_data/part-%d" % x for x in range(len(os.listdir("raw_data")))]
epochs = 20 if is_local:
for i in range(epochs): local_train(optimizer)
dataset.set_filelist(whole_filelist[:int(0.8*len(whole_filelist))]) else:
exe.train_from_dataset(program=fluid.default_main_program(), dist_train(optimizer)
dataset=dataset,
fetch_list=[auc_var],
fetch_info=["auc"],
debug=False)
if fleet.worker_index() == 0: if __name__ == '__main__':
model_dir = args.model_output_dir + '/epoch' + str(i + 1) + ".model" main_function(args.is_local)
sys.stderr.write("epoch%d finished" % (i + 1))
fluid.io.save_inference_model(model_dir,
[dense_input.name] + [x.name for x in sparse_input_ids] + [label.name], [loss, auc_var], exe)
单机训练启动命令
.. code:: python
启动命令 python train.py --is_local 1
在单机模拟多机训练的启动命令,这里我们用到了paddle内置的一个启动器launch_ps,用户可以指定worker和server的数量进行参数服务器任务的启动
.. code:: python .. code:: python
python -m paddle.distributed.launch_ps --worker_num 2 --server_num 2 dist_train.py python -m paddle.distributed.launch_ps --worker_num 2 --server_num 2 train.py
运行日志 运行日志
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册