Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
2101dfd2
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看板
未验证
提交
2101dfd2
编写于
8月 19, 2020
作者:
W
wangchaochaohu
提交者:
GitHub
8月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【API2.0】add Chunk API (#26314)
上级
abfdffa0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
188 addition
and
0 deletion
+188
-0
python/paddle/__init__.py
python/paddle/__init__.py
+1
-0
python/paddle/fluid/tests/unittests/test_chunk_op.py
python/paddle/fluid/tests/unittests/test_chunk_op.py
+138
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+1
-0
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+48
-0
未找到文件。
python/paddle/__init__.py
浏览文件 @
2101dfd2
...
...
@@ -126,6 +126,7 @@ from .tensor.manipulation import unstack #DEFINE_ALIAS
from
.tensor.manipulation
import
flip
#DEFINE_ALIAS
from
.tensor.manipulation
import
unbind
#DEFINE_ALIAS
from
.tensor.manipulation
import
roll
#DEFINE_ALIAS
from
.tensor.manipulation
import
chunk
#DEFINE_ALIAS
from
.tensor.math
import
abs
#DEFINE_ALIAS
from
.tensor.math
import
acos
#DEFINE_ALIAS
from
.tensor.math
import
asin
#DEFINE_ALIAS
...
...
python/paddle/fluid/tests/unittests/test_chunk_op.py
0 → 100644
浏览文件 @
2101dfd2
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
numpy
as
np
from
paddle.fluid
import
Program
,
program_guard
from
paddle
import
fluid
import
paddle
class
TestChunkOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
# The type of axis in chunk_op should be int or Variable.
def
test_axis_type
():
x1
=
paddle
.
data
(
shape
=
[
4
],
dtype
=
'float16'
,
name
=
'x3'
)
paddle
.
chunk
(
x
=
x1
,
chunks
=
2
,
axis
=
3.2
)
self
.
assertRaises
(
TypeError
,
test_axis_type
)
# The type of axis in chunk op should be int or Variable.
def
test_axis_variable_type
():
x2
=
paddle
.
data
(
shape
=
[
4
],
dtype
=
'float16'
,
name
=
'x9'
)
x3
=
paddle
.
data
(
shape
=
[
1
],
dtype
=
'float16'
,
name
=
'x10'
)
paddle
.
chunk
(
input
=
x2
,
chunks
=
2
,
axis
=
x3
)
self
.
assertRaises
(
TypeError
,
test_axis_variable_type
)
# The type of num_or_sections in chunk_op should be int, tuple or list.
def
test_chunks_type
():
x4
=
paddle
.
data
(
shape
=
[
4
],
dtype
=
'float16'
,
name
=
'x4'
)
paddle
.
chunk
(
input
=
x4
,
chunks
=
2.1
,
axis
=
3
)
self
.
assertRaises
(
TypeError
,
test_chunks_type
)
def
test_axis_type_tensor
():
x5
=
paddle
.
data
(
shape
=
[
4
],
dtype
=
'float16'
,
name
=
'x6'
)
paddle
.
chunk
(
input
=
x5
,
chunks
=
2
,
axis
=
3.2
)
self
.
assertRaises
(
TypeError
,
test_axis_type_tensor
)
class
API_TestChunk
(
unittest
.
TestCase
):
def
test_out
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data1
=
paddle
.
data
(
'data1'
,
shape
=
[
4
,
6
,
6
],
dtype
=
'float64'
)
data2
=
paddle
.
data
(
'data2'
,
shape
=
[
1
],
dtype
=
'int32'
)
x0
,
x1
,
x2
=
paddle
.
chunk
(
data1
,
chunks
=
3
,
axis
=
data2
)
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
input1
=
np
.
random
.
random
([
4
,
6
,
6
]).
astype
(
'float64'
)
input2
=
np
.
array
([
2
]).
astype
(
'int32'
)
r0
,
r1
,
r2
,
=
exe
.
run
(
feed
=
{
"data1"
:
input1
,
"data2"
:
input2
},
fetch_list
=
[
x0
,
x1
,
x2
])
ex_x0
,
ex_x1
,
ex_x2
=
np
.
array_split
(
input1
,
3
,
axis
=
2
)
self
.
assertTrue
(
np
.
allclose
(
ex_x0
,
r0
))
self
.
assertTrue
(
np
.
allclose
(
ex_x1
,
r1
))
self
.
assertTrue
(
np
.
allclose
(
ex_x2
,
r2
))
class
API_TestChunk1
(
unittest
.
TestCase
):
def
test_out
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
data1
=
paddle
.
data
(
'data1'
,
shape
=
[
4
,
6
,
6
],
dtype
=
'float64'
)
x0
,
x1
,
x2
=
paddle
.
chunk
(
data1
,
chunks
=
3
,
axis
=
2
)
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
input1
=
np
.
random
.
random
([
4
,
6
,
6
]).
astype
(
'float64'
)
r0
,
r1
,
r2
,
=
exe
.
run
(
feed
=
{
"data1"
:
input1
},
fetch_list
=
[
x0
,
x1
,
x2
])
ex_x0
,
ex_x1
,
ex_x2
=
np
.
array_split
(
input1
,
3
,
axis
=
2
)
self
.
assertTrue
(
np
.
allclose
(
ex_x0
,
r0
))
self
.
assertTrue
(
np
.
allclose
(
ex_x1
,
r1
))
self
.
assertTrue
(
np
.
allclose
(
ex_x2
,
r2
))
class
API_TestDygraphChunk
(
unittest
.
TestCase
):
def
test_out1
(
self
):
with
fluid
.
dygraph
.
guard
():
input_1
=
np
.
random
.
random
([
4
,
6
,
6
]).
astype
(
"int32"
)
# input is a variable which shape is [4, 6, 6]
input
=
fluid
.
dygraph
.
to_variable
(
input_1
)
x0
,
x1
,
x2
=
paddle
.
chunk
(
input
,
chunks
=
3
,
axis
=
1
)
x0_out
=
x0
.
numpy
()
x1_out
=
x1
.
numpy
()
x2_out
=
x2
.
numpy
()
ex_x0
,
ex_x1
,
ex_x2
=
np
.
array_split
(
input_1
,
3
,
axis
=
1
)
self
.
assertTrue
(
np
.
allclose
(
ex_x0
,
x0_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x1
,
x1_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x2
,
x2_out
))
def
test_out2
(
self
):
with
fluid
.
dygraph
.
guard
():
input_1
=
np
.
random
.
random
([
4
,
6
,
6
]).
astype
(
"bool"
)
# input is a variable which shape is [4, 6, 6]
input
=
fluid
.
dygraph
.
to_variable
(
input_1
)
x0
,
x1
,
x2
=
paddle
.
chunk
(
input
,
chunks
=
3
,
axis
=
1
)
x0_out
=
x0
.
numpy
()
x1_out
=
x1
.
numpy
()
x2_out
=
x2
.
numpy
()
ex_x0
,
ex_x1
,
ex_x2
=
np
.
array_split
(
input_1
,
3
,
axis
=
1
)
self
.
assertTrue
(
np
.
allclose
(
ex_x0
,
x0_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x1
,
x1_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x2
,
x2_out
))
def
test_axis_tensor_input
(
self
):
with
fluid
.
dygraph
.
guard
():
input_1
=
np
.
random
.
random
([
4
,
6
,
6
]).
astype
(
"int32"
)
# input is a variable which shape is [4, 6, 6]
input
=
fluid
.
dygraph
.
to_variable
(
input_1
)
num1
=
paddle
.
full
(
shape
=
[
1
],
fill_value
=
1
,
dtype
=
'int32'
)
x0
,
x1
,
x2
=
paddle
.
chunk
(
input
,
chunks
=
3
,
axis
=
num1
)
x0_out
=
x0
.
numpy
()
x1_out
=
x1
.
numpy
()
x2_out
=
x2
.
numpy
()
ex_x0
,
ex_x1
,
ex_x2
=
np
.
array_split
(
input_1
,
3
,
axis
=
1
)
self
.
assertTrue
(
np
.
allclose
(
ex_x0
,
x0_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x1
,
x1_out
))
self
.
assertTrue
(
np
.
allclose
(
ex_x2
,
x2_out
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
2101dfd2
...
...
@@ -99,6 +99,7 @@ from .manipulation import unstack #DEFINE_ALIAS
from
.manipulation
import
flip
#DEFINE_ALIAS
from
.manipulation
import
unbind
#DEFINE_ALIAS
from
.manipulation
import
roll
#DEFINE_ALIAS
from
.manipulation
import
chunk
#DEFINE_ALIAS
from
.math
import
abs
#DEFINE_ALIAS
from
.math
import
acos
#DEFINE_ALIAS
from
.math
import
asin
#DEFINE_ALIAS
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
2101dfd2
...
...
@@ -56,6 +56,7 @@ __all__ = [
'shard_index'
,
'slice'
,
'split'
,
'chunk'
'squeeze'
,
'stack'
,
'strided_slice'
,
...
...
@@ -789,6 +790,53 @@ def unbind(input, axis=0):
return
outs
def
chunk
(
x
,
chunks
,
axis
=
0
,
name
=
None
):
"""
Split the input tensor into multiple sub-Tensors.
Args:
x (Tensor): A N-D Tensor. The data type is bool, float16, float32, float64, int32 or int64.
chunks(int): The number of tensor to be split along the certain axis.
axis (int|Tensor, optional): The axis along which to split, it can be a scalar with type
``int`` or a ``Tensor`` with shape [1] and data type ``int32`` or ``int64``.
If :math::`axis < 0`, the axis to split along is :math:`rank(x) + axis`. Default is 0.
name (str, optional): The default value is None. Normally there is no need for user to set this property.
For more information, please refer to :ref:`api_guide_Name` .
Returns:
list(Tensor): The list of segmented Tensors.
Raises:
TypeError: The data type of ``x`` must be one of bool, float16, float32, float64, int32, int64.
TypeError: ``chunks`` is not int.
TypeError: ``axis`` is not int or Tensor. the data type of ``axis`` must be int32 or int64 when it's a Tensor.
Example:
.. code-block:: python
import numpy as np
import paddle
paddle.disable_static()
# x is a Tensor which shape is [3, 9, 5]
x_np = np.random.random([3, 9, 5]).astype("int32")
x = paddle.to_variable(x_np)
out0, out1, out22 = paddle.chunk(x, chunks=3, axis=1)
# out0.shape [3, 3, 5]
# out1.shape [3, 3, 5]
# out2.shape [3, 3, 5]
# axis is negative, the real axis is (rank(x) + axis) which real
# value is 1.
out0, out1, out2 = paddle.chunk(x, chunks=3, axis=-2)
# out0.shape [3, 3, 5]
# out1.shape [3, 3, 5]
# out2.shape [3, 3, 5]
"""
check_type
(
chunks
,
'chunks'
,
(
int
),
'chunk'
)
return
paddle
.
fluid
.
layers
.
split
(
input
=
x
,
num_or_sections
=
chunks
,
dim
=
axis
,
name
=
name
)
def
tile
(
x
,
repeat_times
,
name
=
None
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录