Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
74803f51
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
74803f51
编写于
4月 29, 2020
作者:
myq406450149
提交者:
GitHub
4月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unbind python api (#24141)
* add unbind pyhon api. test=develp
上级
3e2bc871
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
160 addition
and
2 deletion
+160
-2
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+55
-0
python/paddle/fluid/tests/unittests/test_unbind_op.py
python/paddle/fluid/tests/unittests/test_unbind_op.py
+48
-1
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+1
-0
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+56
-1
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
74803f51
...
...
@@ -186,6 +186,7 @@ __all__ = [
'hard_swish',
'gather_tree',
'uniform_random',
'unbind',
]
...
...
@@ -14344,3 +14345,57 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0):
outputs={"Out": out})
return helper.append_activation(out)
def unbind(input, axis=0):
"""
Removes a tensor dimension, then split the input tensor into multiple sub-Tensors.
Args:
input (Variable): The input variable which is an N-D Tensor, data type being float32, float64, int32 or int64.
axis (int32|int64, optional): A scalar with type ``int32|int64`` shape [1]. The dimension along which to unbind. If :math:`axis < 0`, the
dimension to unbind along is :math:`rank(input) + axis`. Default is 0.
Returns:
list(Variable): The list of segmented Tensor variables.
Example:
.. code-block:: python
import paddle
# input is a variable which shape is [3, 4, 5]
input = paddle.fluid.data(
name="input", shape=[3, 4, 5], dtype="float32")
[x0, x1, x2] = paddle.tensor.unbind(input, axis=0)
# x0.shape [4, 5]
# x1.shape [4, 5]
# x2.shape [4, 5]
[x0, x1, x2, x3] = paddle.tensor.unbind(input, axis=1)
# x0.shape [3, 5]
# x1.shape [3, 5]
# x2.shape [3, 5]
# x3.shape [3, 5]
"""
helper = LayerHelper("unbind", **locals())
check_type(input, 'input', (Variable), 'unbind')
dtype = helper.input_dtype()
check_dtype(dtype, 'unbind', ['float32', 'float64', 'int32', 'int64'],
'unbind')
if not isinstance(axis, (int)):
raise TypeError("The type of 'axis' must be int, but received %s." %
(type(axis)))
if isinstance(axis, np.generic):
axis = np.asscalar(axis)
input_shape = input.shape
axis_ = axis if axis >= 0 else len(input_shape) + axis
num = input_shape[axis_]
outs = [
helper.create_variable_for_type_inference(dtype=helper.input_dtype())
for i in range(num)
]
helper.append_op(
type="unbind",
inputs={"X": input},
outputs={"Out": outs},
attrs={"axis": axis})
return outs
python/paddle/fluid/tests/unittests/test_unbind_op.py
浏览文件 @
74803f51
...
...
@@ -18,9 +18,46 @@ import unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.fluid
as
fluid
import
paddle.tensor
as
tensor
from
paddle.fluid
import
compiler
,
Program
,
program_guard
,
core
class
TestUnbind
(
unittest
.
TestCase
):
def
test_unbind
(
self
):
x_1
=
fluid
.
data
(
shape
=
[
2
,
3
],
dtype
=
'float32'
,
name
=
'x_1'
)
[
out_0
,
out_1
]
=
tensor
.
unbind
(
input
=
x_1
,
axis
=
0
)
input_1
=
np
.
random
.
random
([
2
,
3
]).
astype
(
"float32"
)
axis
=
fluid
.
data
(
shape
=
[
1
],
dtype
=
'int32'
,
name
=
'axis'
)
exe
=
fluid
.
Executor
(
place
=
fluid
.
CPUPlace
())
[
res_1
,
res_2
]
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"x_1"
:
input_1
,
"axis"
:
0
},
fetch_list
=
[
out_0
,
out_1
])
assert
np
.
array_equal
(
res_1
,
input_1
[
0
,
0
:
100
])
assert
np
.
array_equal
(
res_2
,
input_1
[
1
,
0
:
100
])
class
TestLayersUnbind
(
unittest
.
TestCase
):
def
test_layers_unbind
(
self
):
x_1
=
fluid
.
data
(
shape
=
[
2
,
3
],
dtype
=
'float32'
,
name
=
'x_1'
)
[
out_0
,
out_1
]
=
fluid
.
layers
.
unbind
(
input
=
x_1
,
axis
=
0
)
input_1
=
np
.
random
.
random
([
2
,
3
]).
astype
(
"float32"
)
axis
=
fluid
.
data
(
shape
=
[
1
],
dtype
=
'int32'
,
name
=
'axis'
)
exe
=
fluid
.
Executor
(
place
=
fluid
.
CPUPlace
())
[
res_1
,
res_2
]
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"x_1"
:
input_1
,
"axis"
:
0
},
fetch_list
=
[
out_0
,
out_1
])
assert
np
.
array_equal
(
res_1
,
input_1
[
0
,
0
:
100
])
assert
np
.
array_equal
(
res_2
,
input_1
[
1
,
0
:
100
])
class
TestUnbindOp
(
OpTest
):
def
initParameters
(
self
):
pass
...
...
@@ -37,7 +74,6 @@ class TestUnbindOp(OpTest):
self
.
axis
=
0
self
.
num
=
3
self
.
initParameters
()
#x = np.random.random((3, 2, 2)).astype(self.dtype)
x
=
np
.
arange
(
12
).
reshape
(
3
,
2
,
2
).
astype
(
self
.
dtype
)
self
.
out
=
np
.
split
(
x
,
self
.
num
,
self
.
axis
)
self
.
outReshape
()
...
...
@@ -118,5 +154,16 @@ class TestUnbindOp4(TestUnbindOp):
self
.
out
[
1
]
=
self
.
out
[
1
].
reshape
((
3
,
2
))
class
TestUnbindAxisError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
x
=
fluid
.
data
(
shape
=
[
2
,
3
],
dtype
=
'float32'
,
name
=
'x'
)
def
test_table_Variable
():
tensor
.
unbind
(
input
=
x
,
axis
=
2.0
)
self
.
assertRaises
(
TypeError
,
test_table_Variable
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
74803f51
...
...
@@ -151,6 +151,7 @@ from .math import erf #DEFINE_ALIAS
from
.math
import
addcmul
#DEFINE_ALIAS
from
.math
import
addmm
#DEFINE_ALIAS
from
.math
import
clamp
#DEFINE_ALIAS
from
.manipulation
import
unbind
#DEFINE_ALIAS
from
.math
import
trace
#DEFINE_ALIAS
from
.math
import
kron
#DEFINE_ALIAS
# from .random import gaussin #DEFINE_ALIAS
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
74803f51
...
...
@@ -20,6 +20,7 @@ from ..fluid.framework import Variable, OpProtoHolder, in_dygraph_mode, convert_
from
..fluid.data_feeder
import
convert_dtype
,
check_variable_and_dtype
,
check_type
,
check_dtype
from
..fluid.layers.tensor
import
fill_constant
from
..fluid.layers
import
utils
import
numpy
as
np
# TODO: define functions to manipulate a tensor
from
..fluid.layers
import
cast
#DEFINE_ALIAS
from
..fluid.layers
import
concat
#DEFINE_ALIAS
...
...
@@ -60,7 +61,7 @@ __all__ = [
'unsqueeze'
,
'unstack'
,
'flip'
,
#
'unbind',
'unbind'
,
'roll'
]
...
...
@@ -657,3 +658,57 @@ def gather(input, index, overwrite=True):
outputs
=
{
"Out"
:
out
},
attrs
=
{
'overwrite'
:
overwrite
})
return
out
def
unbind
(
input
,
axis
=
0
):
"""
Removes a tensor dimension, then split the input tensor into multiple sub-Tensors.
Args:
input (Variable): The input variable which is an N-D Tensor, data type being float32, float64, int32 or int64.
axis (int32|int64, optional): A scalar with type ``int32|int64`` shape [1]. The dimension along which to unbind. If :math:`axis < 0`, the
dimension to unbind along is :math:`rank(input) + axis`. Default is 0.
Returns:
list(Variable): The list of segmented Tensor variables.
Example:
.. code-block:: python
import paddle
# input is a variable which shape is [3, 4, 5]
input = paddle.fluid.data(
name="input", shape=[3, 4, 5], dtype="float32")
[x0, x1, x2] = paddle.tensor.unbind(input, axis=0)
# x0.shape [4, 5]
# x1.shape [4, 5]
# x2.shape [4, 5]
[x0, x1, x2, x3] = paddle.tensor.unbind(input, axis=1)
# x0.shape [3, 5]
# x1.shape [3, 5]
# x2.shape [3, 5]
# x3.shape [3, 5]
"""
helper
=
LayerHelper
(
"unbind"
,
**
locals
())
check_type
(
input
,
'input'
,
(
Variable
),
'unbind'
)
dtype
=
helper
.
input_dtype
()
check_dtype
(
dtype
,
'unbind'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'unbind'
)
if
not
isinstance
(
axis
,
(
int
)):
raise
TypeError
(
"The type of 'axis' must be int, but received %s."
%
(
type
(
axis
)))
if
isinstance
(
axis
,
np
.
generic
):
axis
=
np
.
asscalar
(
axis
)
input_shape
=
input
.
shape
axis_
=
axis
if
axis
>=
0
else
len
(
input_shape
)
+
axis
num
=
input_shape
[
axis_
]
outs
=
[
helper
.
create_variable_for_type_inference
(
dtype
=
helper
.
input_dtype
())
for
i
in
range
(
num
)
]
helper
.
append_op
(
type
=
"unbind"
,
inputs
=
{
"X"
:
input
},
outputs
=
{
"Out"
:
outs
},
attrs
=
{
"axis"
:
axis
})
return
outs
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录