Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b5cd67e7
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
b5cd67e7
编写于
11月 28, 2022
作者:
2
201716010711
提交者:
GitHub
11月 28, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
delete strided_slice api (#48395)
上级
1a92098a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
7 addition
and
231 deletion
+7
-231
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+0
-222
python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py
...le/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py
+2
-4
python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py
...le/fluid/tests/unittests/npu/test_strided_slice_op_npu.py
+4
-4
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+1
-1
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
b5cd67e7
...
...
@@ -121,7 +121,6 @@ __all__ = [
'gaussian_random_batch_size_like'
,
'sum'
,
'slice'
,
'strided_slice'
,
'shape'
,
'size'
,
'clip'
,
...
...
@@ -7530,227 +7529,6 @@ def slice(input, axes, starts, ends):
return
out
@
deprecated
(
since
=
'2.0.0'
,
update_to
=
"paddle.strided_slice"
)
def
strided_slice
(
input
,
axes
,
starts
,
ends
,
strides
):
"""
:alias_main: paddle.strided_slice
:alias: paddle.strided_slice,paddle.tensor.strided_slice,paddle.tensor.manipulation.strided_slice
:old_api: paddle.fluid.layers.strided_slice
This operator produces a slice of ``input`` along multiple axes. Similar to numpy:
https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
Slice uses ``axes``, ``starts`` and ``ends`` attributes to specify the start and
end dimension for each axis in the list of axes and Slice uses this information
to slice the input data tensor. If a negative value is passed to
``starts`` or ``ends`` such as :math:`-i`, it represents the reverse position of the
axis :math:`i-1` th(here 0 is the initial position). The ``strides`` represents steps of
slicing and if the ``strides`` is negative, slice operation is in the opposite direction.
If the value passed to ``starts`` or ``ends`` is greater than n
(the number of elements in this dimension), it represents n.
For slicing to the end of a dimension with unknown size, it is recommended
to pass in INT_MAX. The size of ``axes`` must be equal to ``starts`` , ``ends`` and ``strides``.
Following examples will explain how strided_slice works:
.. code-block:: text
Case1:
Given:
data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
axes = [0, 1]
starts = [1, 0]
ends = [2, 3]
strides = [1, 1]
Then:
result = [ [5, 6, 7], ]
Case2:
Given:
data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
axes = [0, 1]
starts = [0, 1]
ends = [2, 0]
strides = [1, -1]
Then:
result = [ [8, 7, 6], ]
Case3:
Given:
data = [ [1, 2, 3, 4], [5, 6, 7, 8], ]
axes = [0, 1]
starts = [0, 1]
ends = [-1, 1000]
strides = [1, 3]
Then:
result = [ [2], ]
Args:
input (Variable): An N-D ``Tensor`` or ``LoDTensor`` . The data type is ``bool``, ``float32``, ``float64``, ``int32`` or ``int64``.
axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to.
It's optional. If it is not provides, it will be treated as :math:`[0,1,...,len(starts)-1]`.
starts (list|tuple|Variable): The data type is ``int32`` . If ``starts`` is a list or tuple, the elements of
it should be integers or Tensors with shape [1]. If ``starts`` is an Variable, it should be an 1-D Tensor.
It represents starting indices of corresponding axis in ``axes``.
ends (list|tuple|Variable): The data type is ``int32`` . If ``ends`` is a list or tuple, the elements of
it should be integers or Tensors with shape [1]. If ``ends`` is an Variable, it should be an 1-D Tensor .
It represents ending indices of corresponding axis in ``axes``.
strides (list|tuple|Variable): The data type is ``int32`` . If ``strides`` is a list or tuple, the elements of
it should be integers or Tensors with shape [1]. If ``strides`` is an Variable, it should be an 1-D Tensor .
It represents slice step of corresponding axis in ``axes``.
Returns:
Variable: A ``Tensor`` or ``LoDTensor`` with the same dimension as ``input``. The data type is same as ``input``.
Raises:
TypeError: The type of ``starts`` must be list, tuple or Variable.
TypeError: The type of ``ends`` must be list, tuple or Variable.
TypeError: The type of ``strides`` must be list, tuple or Variable.
Examples:
.. code-block:: python
import paddle.fluid as fluid
import paddle
paddle.enable_static()
input = fluid.data(
name="input", shape=[3, 4, 5, 6], dtype='float32')
# example 1:
# attr starts is a list which doesn't contain tensor Variable.
axes = [0, 1, 2]
starts = [-3, 0, 2]
ends = [3, 2, 4]
strides_1 = [1, 1, 1]
strides_2 = [1, 1, 2]
sliced_1 = fluid.layers.strided_slice(input, axes=axes, starts=starts, ends=ends, strides=strides_1)
# sliced_1 is input[:, 0:3:1, 0:2:1, 2:4:1].
# example 2:
# attr starts is a list which contain tensor Variable.
minus_3 = fluid.layers.fill_constant([1], "int32", -3)
sliced_2 = fluid.layers.strided_slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2)
# sliced_2 is input[:, 0:3:1, 0:2:1, 2:4:2].
"""
if
in_dygraph_mode
():
return
_C_ops
.
strided_slice
(
input
,
axes
,
starts
,
ends
,
strides
)
helper
=
LayerHelper
(
'strided_slice'
,
**
locals
())
check_variable_and_dtype
(
input
,
'input'
,
[
'bool'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'strided_slice'
,
)
check_type
(
axes
,
'axes'
,
(
list
,
tuple
),
'strided_slice'
)
check_type
(
starts
,
'starts'
,
(
list
,
tuple
,
Variable
),
'strided_slice'
)
check_type
(
ends
,
'ends'
,
(
list
,
tuple
,
Variable
),
'strided_slice'
)
check_type
(
strides
,
'strides'
,
(
list
,
tuple
,
Variable
),
'strided_slice'
)
def
check_list_elements_dtype
(
list_input
,
input_name
):
if
isinstance
(
list_input
,
Variable
):
check_dtype
(
list_input
.
dtype
,
input_name
,
[
'int32'
],
'strided_slice'
)
else
:
for
i
,
var
in
enumerate
(
list_input
):
var_name
=
input_name
+
'['
+
str
(
i
)
+
']'
if
isinstance
(
var
,
Variable
):
check_dtype
(
var
.
dtype
,
var_name
,
[
'int32'
],
'strided_slice'
)
check_list_elements_dtype
(
axes
,
'axes'
)
check_list_elements_dtype
(
starts
,
'starts'
)
check_list_elements_dtype
(
ends
,
'ends'
)
check_list_elements_dtype
(
strides
,
'strides'
)
def
get_new_list_tensor
(
old_list
):
new_list_tensor
=
[]
for
dim
in
old_list
:
if
isinstance
(
dim
,
Variable
):
dim
.
stop_gradient
=
True
new_list_tensor
.
append
(
dim
)
else
:
assert
isinstance
(
dim
,
int
)
temp_out
=
helper
.
create_variable_for_type_inference
(
'int32'
)
fill_constant
([
1
],
'int32'
,
dim
,
force_cpu
=
True
,
out
=
temp_out
)
new_list_tensor
.
append
(
temp_out
)
return
new_list_tensor
inputs
=
{
'Input'
:
input
}
attrs
=
{
'axes'
:
axes
}
infer_flags
=
list
(
1
for
i
in
range
(
len
(
axes
)))
if
_non_static_mode
():
inputs
=
{
'Input'
:
input
}
attrs
=
{
'axes'
:
axes
,
'starts'
:
starts
,
'ends'
:
ends
,
'strides'
:
strides
,
'infer_flags'
:
infer_flags
,
}
else
:
# starts
if
isinstance
(
starts
,
Variable
):
starts
.
stop_gradient
=
True
inputs
[
'StartsTensor'
]
=
starts
elif
isinstance
(
starts
,
(
list
,
tuple
)):
attrs
[
'starts'
]
=
[]
if
utils
.
_contain_var
(
starts
):
inputs
[
'StartsTensorList'
]
=
get_new_list_tensor
(
starts
)
for
i
,
dim
in
enumerate
(
starts
):
if
isinstance
(
dim
,
Variable
):
attrs
[
'starts'
].
append
(
-
1
)
infer_flags
[
i
]
=
-
1
else
:
attrs
[
'starts'
].
append
(
dim
)
else
:
attrs
[
'starts'
]
=
starts
# ends
if
isinstance
(
ends
,
Variable
):
ends
.
stop_gradient
=
True
inputs
[
'EndsTensor'
]
=
ends
elif
isinstance
(
ends
,
(
list
,
tuple
)):
attrs
[
'ends'
]
=
[]
if
utils
.
_contain_var
(
ends
):
inputs
[
'EndsTensorList'
]
=
get_new_list_tensor
(
ends
)
for
i
,
dim
in
enumerate
(
ends
):
if
isinstance
(
dim
,
Variable
):
attrs
[
'ends'
].
append
(
-
1
)
infer_flags
[
i
]
=
-
1
else
:
attrs
[
'ends'
].
append
(
dim
)
else
:
attrs
[
'ends'
]
=
ends
# strides
if
isinstance
(
strides
,
Variable
):
strides
.
stop_gradient
=
True
inputs
[
'StridesTensor'
]
=
strides
elif
isinstance
(
strides
,
(
list
,
tuple
)):
attrs
[
'strides'
]
=
[]
if
utils
.
_contain_var
(
strides
):
inputs
[
'StridesTensorList'
]
=
get_new_list_tensor
(
strides
)
for
i
,
dim
in
enumerate
(
strides
):
if
isinstance
(
dim
,
Variable
):
attrs
[
'strides'
].
append
(
-
1
)
infer_flags
[
i
]
=
-
1
else
:
attrs
[
'strides'
].
append
(
dim
)
else
:
attrs
[
'strides'
]
=
strides
attrs
[
'infer_flags'
]
=
infer_flags
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
helper
.
input_dtype
(
'input'
)
)
helper
.
append_op
(
type
=
'strided_slice'
,
inputs
=
inputs
,
attrs
=
attrs
,
outputs
=
{
'Out'
:
out
}
)
return
out
def
shape
(
input
):
"""
:alias_main: paddle.shape
...
...
python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py
浏览文件 @
b5cd67e7
...
...
@@ -52,7 +52,7 @@ class TestBase(IPUOpTest):
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
strided_slice
(
x
,
**
self
.
attrs
)
out
=
paddle
.
strided_slice
(
x
,
**
self
.
attrs
)
self
.
fetch_list
=
[
out
.
name
]
def
run_model
(
self
,
exec_mode
):
...
...
@@ -128,9 +128,7 @@ class TestCase3(TestBase):
ends
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
2
],
shape
=
self
.
feed_shape
[
2
],
dtype
=
'int32'
)
out
=
paddle
.
fluid
.
layers
.
strided_slice
(
x
,
starts
=
starts
,
ends
=
ends
,
**
self
.
attrs
)
out
=
paddle
.
strided_slice
(
x
,
starts
=
starts
,
ends
=
ends
,
**
self
.
attrs
)
self
.
fetch_list
=
[
out
.
name
]
...
...
python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py
浏览文件 @
b5cd67e7
...
...
@@ -596,28 +596,28 @@ class TestStridedSliceAPI(unittest.TestCase):
append_batch_size
=
False
,
dtype
=
"float64"
,
)
out_1
=
fluid
.
layers
.
strided_slice
(
out_1
=
paddle
.
strided_slice
(
x
,
axes
=
[
0
,
1
,
2
],
starts
=
[
-
3
,
0
,
2
],
ends
=
[
3
,
100
,
-
1
],
strides
=
[
1
,
1
,
1
],
)
out_2
=
fluid
.
layers
.
strided_slice
(
out_2
=
paddle
.
strided_slice
(
x
,
axes
=
[
0
,
1
,
3
],
starts
=
[
minus_3
,
0
,
2
],
ends
=
[
3
,
100
,
-
1
],
strides
=
[
1
,
1
,
1
],
)
out_3
=
fluid
.
layers
.
strided_slice
(
out_3
=
paddle
.
strided_slice
(
x
,
axes
=
[
0
,
1
,
3
],
starts
=
[
minus_3
,
0
,
2
],
ends
=
[
3
,
100
,
minus_1
],
strides
=
[
1
,
1
,
1
],
)
out_4
=
fluid
.
layers
.
strided_slice
(
out_4
=
paddle
.
strided_slice
(
x
,
axes
=
[
0
,
1
,
2
],
starts
=
starts
,
ends
=
ends
,
strides
=
strides
)
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
b5cd67e7
...
...
@@ -3846,7 +3846,7 @@ class TestBook(LayerTest):
strides
=
[
1
,
1
,
1
]
with
self
.
static_graph
():
x
=
layers
.
data
(
name
=
"x"
,
shape
=
[
245
,
30
,
30
],
dtype
=
"float32"
)
out
=
layers
.
strided_slice
(
out
=
paddle
.
strided_slice
(
x
,
axes
=
axes
,
starts
=
starts
,
ends
=
ends
,
strides
=
strides
)
return
out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录