Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
22032e49
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
22032e49
编写于
1月 22, 2018
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add python wrapper for multiplex operator.
上级
29603cf3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
66 addition
and
4 deletion
+66
-4
doc/api/v2/fluid/layers.rst
doc/api/v2/fluid/layers.rst
+5
-0
python/paddle/v2/fluid/layers/nn.py
python/paddle/v2/fluid/layers/nn.py
+51
-4
python/paddle/v2/fluid/tests/test_layers.py
python/paddle/v2/fluid/tests/test_layers.py
+10
-0
未找到文件。
doc/api/v2/fluid/layers.rst
浏览文件 @
22032e49
...
...
@@ -509,3 +509,8 @@ sequence_reshape
----------------
.. autofunction:: paddle.v2.fluid.layers.sequence_reshape
:noindex:
multiplex
---------
.. autofunction:: paddle.v2.fluid.layers.multiplex
:noindex:
python/paddle/v2/fluid/layers/nn.py
浏览文件 @
22032e49
...
...
@@ -28,7 +28,7 @@ __all__ = [
'batch_norm'
,
'beam_search_decode'
,
'conv2d_transpose'
,
'sequence_expand'
,
'lstm_unit'
,
'reduce_sum'
,
'reduce_mean'
,
'reduce_max'
,
'reduce_min'
,
'sequence_first_step'
,
'sequence_last_step'
,
'dropout'
,
'split'
,
'l2_normalize'
,
'matmul'
,
'warpctc'
,
'sequence_reshape'
'l2_normalize'
,
'matmul'
,
'warpctc'
,
'sequence_reshape'
,
'multiplex'
]
...
...
@@ -1813,11 +1813,11 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
- If both are 2-D, they are multiplied like conventional matrices.
- If either is n-D, it is treated as a stack of matrices residing in the
last two dimensions and a batched matrix multiply supporting broadcast
last two dimensions and a batched matrix multiply supporting broadcast
applies on the two tensors.
Also note that if the raw tensor :math:`x` or :math:`y` is rank-1 and
nontransposed, the prepended or appended dimension :math:`1` will be
Also note that if the raw tensor :math:`x` or :math:`y` is rank-1 and
nontransposed, the prepended or appended dimension :math:`1` will be
removed after matrix multiplication.
Args:
...
...
@@ -1971,3 +1971,50 @@ def sequence_reshape(input, new_dim):
outputs
=
{
'Out'
:
[
out
]},
attrs
=
{
'new_dim'
:
new_dim
})
return
out
def
multiplex
(
inputs
,
index
):
"""
**Multiplex Layer**
Referring to the given index variable, this layer gathers from the input
variables to output a multiplex variable. Assuming that there are :math:`m`
input variables and let :math:`I_i` represents the i-th input variable and i
is in [0, :math:`m`). All input variables are tensors with same shape
[:math:`d_0`, :math:`d_1`, ..., :math:`d_R`]. Please note that rank of the
input tensor should be at least 2. Each input variable will be viewed as a
2-D matrix with shape [:math:`M`, :math:`N`] where :math:`M` for :math:`d_0`
and :math:`N` for :math:`d_1` * :math:`d_2` * ... * :math:`d_R`. Let
:math:`I_i[j]` be the j-th row of the i-th input variable. The given index
variable should be a 2-D tensor with shape [:math:`M`, 1]. Let `ID[i]` be
the i-th index value of index variable. Then the output variable will be a
tensor with shape [:math:`d_0`, :math:`d_1`, ..., :math:`d_R`]. If we view
the output tensor as a 2-D matrix with shape [:math:`M`, :math:`N`] and let
:math:`O[i]` be the i-th row of the matrix, then values of `O[i]` come from
:math:`I_{ID[i]}[i]`.
Args:
inputs (list): Input variables which are tensors with same shape and the
rank is at least 2.
index (Variable): Tensor<int>, index variable which is a 2-D tensor with
shape [M, 1] where M for batch size.
Returns:
Variable: Multiplex variable gathered from input variables.
Examples:
.. code-block:: python
x1 = fluid.layers.data(name='x1', shape=[4], dtype='float32')
x2 = fluid.layers.data(name='x2', shape=[4], dtype='float32')
index = fluid.layers.data(name='index', shape=[1], dtype='int32')
out = fluid.layers.multiplex(inputs=[x1, x2], index=index)
"""
helper
=
LayerHelper
(
'multiplex'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'multiplex'
,
inputs
=
{
'X'
:
inputs
,
'Ids'
:
index
},
outputs
=
{
'Out'
:
[
out
]})
return
out
python/paddle/v2/fluid/tests/test_layers.py
浏览文件 @
22032e49
...
...
@@ -225,6 +225,16 @@ class TestBook(unittest.TestCase):
self
.
assertIsNotNone
(
out
)
print
(
str
(
program
))
def
test_multiplex
(
self
):
program
=
Program
()
with
program_guard
(
program
):
x1
=
layers
.
data
(
name
=
'x1'
,
shape
=
[
4
],
dtype
=
'float32'
)
x2
=
layers
.
data
(
name
=
'x2'
,
shape
=
[
4
],
dtype
=
'float32'
)
index
=
layers
.
data
(
name
=
'index'
,
shape
=
[
1
],
dtype
=
'int32'
)
out
=
layers
.
multiplex
(
inputs
=
[
x1
,
x2
],
index
=
index
)
self
.
assertIsNotNone
(
out
)
print
(
str
(
program
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录