Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
FluidDoc
提交
06e23377
F
FluidDoc
项目概览
PaddlePaddle
/
FluidDoc
通知
5
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
23
列表
看板
标记
里程碑
合并请求
111
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
FluidDoc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
23
Issue
23
列表
看板
标记
里程碑
合并请求
111
合并请求
111
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
06e23377
编写于
6月 18, 2020
作者:
C
Chen Weihang
提交者:
GitHub
6月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix example code error (#2226)
上级
a0ea5684
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
68 addition
and
69 deletion
+68
-69
doc/fluid/api_cn/layers_cn/double_buffer_cn.rst
doc/fluid/api_cn/layers_cn/double_buffer_cn.rst
+7
-7
doc/fluid/api_cn/layers_cn/py_reader_cn.rst
doc/fluid/api_cn/layers_cn/py_reader_cn.rst
+61
-62
未找到文件。
doc/fluid/api_cn/layers_cn/double_buffer_cn.rst
浏览文件 @
06e23377
...
@@ -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)
...
...
doc/fluid/api_cn/layers_cn/py_reader_cn.rst
浏览文件 @
06e23377
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录