Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
FluidDoc
提交
2c0250b1
F
FluidDoc
项目概览
PaddlePaddle
/
FluidDoc
通知
4
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2c0250b1
编写于
9月 26, 2019
作者:
Z
Zhaolong Xing
提交者:
GitHub
9月 26, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix if else api doc (#1361)
test=develop
上级
83612847
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
55 addition
and
25 deletion
+55
-25
doc/fluid/api_cn/layers_cn/IfElse_cn.rst
doc/fluid/api_cn/layers_cn/IfElse_cn.rst
+55
-25
未找到文件。
doc/fluid/api_cn/layers_cn/IfElse_cn.rst
浏览文件 @
2c0250b1
...
...
@@ -5,42 +5,72 @@ IfElse
.. py:class:: paddle.fluid.layers.IfElse(cond, name=None)
if-else控制流。
该类用于实现IfElse分支控制功能, IfElse包含两个Block,true_block, false_block,IfElse会将满足True或False条件的数据分别放入不同的block运行。
参数:
- **cond** (Variable)-用于比较的条件
- **Name** (str,默认为空(None))-该层名称
cond是一个shape为[N, 1]、数据类型为bool的2-D tensor,表示输入数据对应部分的执行条件。
**代码示例**:
IfElse OP同其他的OP在使用上有一定的区别,可能会对一些用户造成一定的困惑,以下展示了一个
简单的样例对该OP进行说明。
.. code-block:: python
# 以下代码完成的功能:对x中大于0的数据减去10,对x中小于0的数据加上10
import numpy as np
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[4, 1], dtype='float32', append_batch_size=False)
y = fluid.layers.data(name='y', shape=[4, 1], dtype='float32', append_batch_size=False)
x_d = np.array([[3], [1], [-1], [-3]]).astype(np.float32)
y_d = np.zeros((4, 1)).astype(np.float32)
# 比较x, y对元素的大小,输出cond, cond是shape为[4, 1],数据类型为bool的2-D tensor。
# 根据输入数据x_d, y_d,可以推断出cond中的数据为[[true], [true], [false], [false]]
cond = fluid.layers.greater_than(x, y)
# 同其他常见OP不同的是,该OP返回的ie不是Variable而是一个IfElse OP的对象
ie = fluid.layers.IfElse(cond)
with ie.true_block():
# 在这个block中,根据cond条件,获取x中对应条件为true维度的数据,并减去10
out_1 = ie.input(x)
out_1 = out_1 - 10
ie.output(out_1)
with ie.false_block():
# 在这个block中,根据cond条件,获取x中对应条件为false维度的数据,并加上10
out_1 = ie.input(x)
out_1 = out_1 + 10
ie.output(out_1)
# 根据cond条件将两个block中处理后的数据进行合并,此处的output为输出的Variable
output = ie()
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
res = exe.run(fluid.default_main_program(), feed={"x":x_d, "y":y_d}, fetch_list=[output[0]])
print res
# [array([[-7.],
# [-9.],
# [ 9.],
# [ 7.]], dtype=float32)]
参数:
- **cond** (Variable)- cond是一个shape为[N, 1]、数据类型为bool的2-D tensor,表示N个输入数据的对应的执行条件。数据类型为bool。
- **Name** (str,可选)- 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` , 默认值为None。
import paddle.fluid as fluid
image = fluid.layers.data(name="X", shape=[2, 5, 5], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
limit = fluid.layers.fill_constant_batch_size_like(
input=label, dtype='int64', shape=[1], value=5.0)
cond = fluid.layers.less_than(x=label, y=limit)
ie = fluid.layers.IfElse(cond)
with ie.true_block():
true_image = ie.input(image)
hidden = fluid.layers.fc(input=true_image, size=100, act='tanh')
prob = fluid.layers.fc(input=hidden, size=10, act='softmax')
ie.output(prob)
**返回:**
with ie.false_block():
false_image = ie.input(image)
hidden = fluid.layers.fc(
input=false_image, size=200, act='tanh')
prob = fluid.layers.fc(input=hidden, size=10, act='softmax')
ie.output(prob)
prob = ie()
同其他常见OP不同的是:该OP调用返回一个IfElse OP对象(如例子中的 ie)而非Variable,需要调用对象内部函数对不同cond下的输入进行处理。
**内部函数:**
通过调用对象中的 ``with ie.true_block()`` 函数构建block,将条件为true下的计算逻辑放入此block中。如果没有构建相应的block,则对应条件维度下的输入数据不做改变。
``out = ie.input(x)`` 会将x中对应条件维度的数据获取出来放入到out中,支持block内部处理多个输入。
``ie.output(out)`` 会将结果写入对应条件的输出中。
对象内部有 ``__call__()`` 函数,即通过对 ``output = ie()`` 的调用,将条件分别为True,False的block内部所有的输出进行融合作为整体的输出,输出的类型为列表,列表中每个元素的类型为Variable。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录