未验证 提交 06e23377 编写于 作者: C Chen Weihang 提交者: GitHub

fix example code error (#2226)

上级 a0ea5684
...@@ -24,13 +24,13 @@ double_buffer ...@@ -24,13 +24,13 @@ double_buffer
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
reader = fluid.layers.open_files(filenames=['mnist.recordio'], reader = fluid.layers.py_reader(capacity=64,
shapes=[[-1, 784], [-1, 1]], shapes=[(-1, 1, 28, 28), (-1, 1)],
lod_levels=[0, 0], dtypes=['float32', 'int64'],
dtypes=['float32', 'int64']) use_double_buffer=False)
reader = fluid.layers.double_buffer(reader) reader = fluid.layers.double_buffer(reader)
img, label = fluid.layers.read_file(reader) image, label = fluid.layers.read_file(reader)
......
...@@ -73,70 +73,69 @@ py_reader ...@@ -73,70 +73,69 @@ py_reader
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.dataset.mnist as mnist import paddle.dataset.mnist as mnist
def network(reader): def network(reader):
img, label = fluid.layers.read_file(reader) img, label = fluid.layers.read_file(reader)
# 用户自定义网络,此处以softmax回归为例 # 用户自定义网络,此处以softmax回归为例
predict = fluid.layers.fc(input=img, size=10, act='softmax') predict = fluid.layers.fc(input=img, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=predict, label=label) loss = fluid.layers.cross_entropy(input=predict, label=label)
return fluid.layers.mean(loss) return fluid.layers.mean(loss)
# 新建 train_main_prog 和 train_startup_prog # 新建 train_main_prog 和 train_startup_prog
train_main_prog = fluid.Program() train_main_prog = fluid.Program()
train_startup_prog = fluid.Program() train_startup_prog = fluid.Program()
with fluid.program_guard(train_main_prog, train_startup_prog): with fluid.program_guard(train_main_prog, train_startup_prog):
# 使用 fluid.unique_name.guard() 实现与test program的参数共享 # 使用 fluid.unique_name.guard() 实现与test program的参数共享
with fluid.unique_name.guard(): with fluid.unique_name.guard():
train_reader = fluid.layers.py_reader(capacity=64, train_reader = fluid.layers.py_reader(capacity=64,
shapes=[(-1, 1, 28, 28), (-1, 1)], shapes=[(-1, 1, 28, 28), (-1, 1)],
dtypes=['float32', 'int64'], dtypes=['float32', 'int64'],
name='train_reader') name='train_reader')
train_reader.decorate_paddle_reader( train_reader.decorate_paddle_reader(
paddle.reader.shuffle(paddle.batch(mnist.train(), paddle.reader.shuffle(paddle.batch(mnist.train(),
batch_size=5), batch_size=5),
buf_size=500)) buf_size=500))
train_loss = network(train_reader) # 一些网络定义 train_loss = network(train_reader) # 一些网络定义
adam = fluid.optimizer.Adam(learning_rate=0.01) adam = fluid.optimizer.Adam(learning_rate=0.01)
adam.minimize(train_loss) adam.minimize(train_loss)
# Create test_main_prog and test_startup_prog # Create test_main_prog and test_startup_prog
test_main_prog = fluid.Program() test_main_prog = fluid.Program()
test_startup_prog = fluid.Program() test_startup_prog = fluid.Program()
with fluid.program_guard(test_main_prog, test_startup_prog): with fluid.program_guard(test_main_prog, test_startup_prog):
# 使用 fluid.unique_name.guard() 实现与train program的参数共享 # 使用 fluid.unique_name.guard() 实现与train program的参数共享
with fluid.unique_name.guard(): with fluid.unique_name.guard():
test_reader = fluid.layers.py_reader(capacity=32, test_reader = fluid.layers.py_reader(capacity=32,
shapes=[(-1, 1, 28, 28), (-1, 1)], shapes=[(-1, 1, 28, 28), (-1, 1)],
dtypes=['float32', 'int64'], dtypes=['float32', 'int64'],
name='test_reader') name='test_reader')
test_reader.decorate_paddle_reader(paddle.batch(mnist.test(), 512)) test_reader.decorate_paddle_reader(paddle.batch(mnist.test(), 512))
test_loss = network(test_reader)
test_loss = network(test_reader)
fluid.Executor(fluid.CUDAPlace(0)).run(train_startup_prog)
fluid.Executor(fluid.CUDAPlace(0)).run(train_startup_prog) fluid.Executor(fluid.CUDAPlace(0)).run(test_startup_prog)
fluid.Executor(fluid.CUDAPlace(0)).run(test_startup_prog)
train_exe = fluid.ParallelExecutor(use_cuda=True,
train_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=train_loss.name, main_program=train_main_prog)
loss_name=train_loss.name, main_program=train_main_prog) test_exe = fluid.ParallelExecutor(use_cuda=True,
test_exe = fluid.ParallelExecutor(use_cuda=True, loss_name=test_loss.name, main_program=test_main_prog)
loss_name=test_loss.name, main_program=test_main_prog) for epoch_id in range(10):
for epoch_id in range(10): train_reader.start()
train_reader.start() try:
try: while True:
while True: train_exe.run(fetch_list=[train_loss.name])
train_exe.run(fetch_list=[train_loss.name]) except fluid.core.EOFException:
except fluid.core.EOFException: train_reader.reset()
train_reader.reset()
test_reader.start()
test_reader.start() try:
try: while True:
while True: test_exe.run(fetch_list=[test_loss.name])
test_exe.run(fetch_list=[test_loss.name]) except fluid.core.EOFException:
except fluid.core.EOFException: test_reader.reset()
test_reader.reset()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册