Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e4017e5c
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e4017e5c
编写于
7月 15, 2020
作者:
W
wangchaochaohu
提交者:
GitHub
7月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine the index_select Op for API 2.0 test=develop (#25296)
上级
c10dcff1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
36 addition
and
32 deletion
+36
-32
python/paddle/fluid/tests/unittests/test_index_select_op.py
python/paddle/fluid/tests/unittests/test_index_select_op.py
+2
-2
python/paddle/tensor/search.py
python/paddle/tensor/search.py
+34
-30
未找到文件。
python/paddle/fluid/tests/unittests/test_index_select_op.py
浏览文件 @
e4017e5c
...
@@ -83,7 +83,7 @@ class TestIndexSelectAPI(unittest.TestCase):
...
@@ -83,7 +83,7 @@ class TestIndexSelectAPI(unittest.TestCase):
x
=
fluid
.
layers
.
data
(
name
=
'x'
,
shape
=
[
-
1
,
4
])
x
=
fluid
.
layers
.
data
(
name
=
'x'
,
shape
=
[
-
1
,
4
])
index
=
fluid
.
layers
.
data
(
index
=
fluid
.
layers
.
data
(
name
=
'index'
,
shape
=
[
3
],
dtype
=
'int32'
,
append_batch_size
=
False
)
name
=
'index'
,
shape
=
[
3
],
dtype
=
'int32'
,
append_batch_size
=
False
)
z
=
paddle
.
index_select
(
x
,
index
,
dim
=
1
)
z
=
paddle
.
index_select
(
x
,
index
,
axis
=
1
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
res
,
=
exe
.
run
(
feed
=
{
'x'
:
self
.
data_x
,
res
,
=
exe
.
run
(
feed
=
{
'x'
:
self
.
data_x
,
'index'
:
self
.
data_index
},
'index'
:
self
.
data_index
},
...
@@ -124,7 +124,7 @@ class TestIndexSelectAPI(unittest.TestCase):
...
@@ -124,7 +124,7 @@ class TestIndexSelectAPI(unittest.TestCase):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
self
.
data_x
)
x
=
fluid
.
dygraph
.
to_variable
(
self
.
data_x
)
index
=
fluid
.
dygraph
.
to_variable
(
self
.
data_index
)
index
=
fluid
.
dygraph
.
to_variable
(
self
.
data_index
)
z
=
paddle
.
index_select
(
x
,
index
,
dim
=
1
)
z
=
paddle
.
index_select
(
x
,
index
,
axis
=
1
)
np_z
=
z
.
numpy
()
np_z
=
z
.
numpy
()
expect_out
=
np
.
array
([[
1.0
,
2.0
,
2.0
],
[
5.0
,
6.0
,
6.0
],
expect_out
=
np
.
array
([[
1.0
,
2.0
,
2.0
],
[
5.0
,
6.0
,
6.0
],
[
9.0
,
10.0
,
10.0
]])
[
9.0
,
10.0
,
10.0
]])
...
...
python/paddle/tensor/search.py
浏览文件 @
e4017e5c
...
@@ -63,9 +63,9 @@ def argmax(input, axis=None, dtype=None, out=None, keepdims=False, name=None):
...
@@ -63,9 +63,9 @@ def argmax(input, axis=None, dtype=None, out=None, keepdims=False, name=None):
Variable that meets the requirements to store the result of operation.
Variable that meets the requirements to store the result of operation.
if out is None, a new Varibale will be create to store the result. Defalut is None.
if out is None, a new Varibale will be create to store the result. Defalut is None.
keepdims(bool, optional): Keep the axis that do the select max.
keepdims(bool, optional): Keep the axis that do the select max.
name(str, optional): The
name of output variable, normally there is no need for user to set this this property.
name(str, optional): The
default value is None. Normally there is no
Default value is None, the framework set the name of output variable.
need for user to set this property. For more information, please
refer to :ref:`api_guide_Name`.
Returns:
Returns:
Variable: A Tensor with data type int64.
Variable: A Tensor with data type int64.
...
@@ -135,7 +135,7 @@ def argmax(input, axis=None, dtype=None, out=None, keepdims=False, name=None):
...
@@ -135,7 +135,7 @@ def argmax(input, axis=None, dtype=None, out=None, keepdims=False, name=None):
return
out
return
out
def
index_select
(
input
,
index
,
dim
=
0
):
def
index_select
(
x
,
index
,
axis
=
0
,
name
=
None
):
"""
"""
:alias_main: paddle.index_select
:alias_main: paddle.index_select
:alias: paddle.index_select,paddle.tensor.index_select,paddle.tensor.search.index_select
:alias: paddle.index_select,paddle.tensor.index_select,paddle.tensor.search.index_select
...
@@ -146,56 +146,60 @@ def index_select(input, index, dim=0):
...
@@ -146,56 +146,60 @@ def index_select(input, index, dim=0):
size as the length of `index`; other dimensions have the same size as in the `input` tensor.
size as the length of `index`; other dimensions have the same size as in the `input` tensor.
Args:
Args:
input (Variable): The input tensor variable.
x (Variable): The input tensor variable.The dtype of x can be one of float32, float64, int32, int64.
index (Variable): The 1-D tensor containing the indices to index.
index (Variable): The 1-D tensor containing the indices to index.the dtype of index can be int32 or int64.
dim (int): The dimension in which we index.
axis (int, optional): The dimension in which we index. Default: if None, the axis 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:
Returns:
Variable: A Tensor with same data type as `input`.
Variable: A Tensor with same data type as `input`.
Raises:
TypeError: x must be a Variable and the dtype of x must be one of float32, float64, int32 and int64.
TypeError: index must be a Variable adn the dtype of index must be int32 or int64.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
import paddle
import paddle.fluid as fluid
import numpy as np
import numpy as np
paddle.enable_imperative() # Now we are in imperative mode
data = np.array([[1.0, 2.0, 3.0, 4.0],
data = np.array([[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0]])
[9.0, 10.0, 11.0, 12.0]])
data_index = np.array([0, 1, 1]).astype('int32')
data_index = np.array([0, 1, 1]).astype('int32')
with fluid.dygraph.guard():
x = paddle.imperative.to_variable(data)
x = fluid.dygraph.to_variable(data)
index = paddle.imperative.to_variable(data_index)
index = fluid.dygraph.to_variable(data_index)
out_z1 = paddle.index_select(x=x, index=index)
out_z1 = paddle.index_select(x, index)
#[[1. 2. 3. 4.]
print(out_z1.numpy())
# [5. 6. 7. 8.]
#[[1. 2. 3. 4.]
# [5. 6. 7. 8.]]
# [5. 6. 7. 8.]
out_z2 = paddle.index_select(x=x, index=index, axis=1)
# [5. 6. 7. 8.]]
#[[ 1. 2. 2.]
out_z2 = paddle.index_select(x, index, dim=1)
# [ 5. 6. 6.]
print(out_z2.numpy())
# [ 9. 10. 10.]]
#[[ 1. 2. 2.]
# [ 5. 6. 6.]
# [ 9. 10. 10.]]
"""
"""
helper
=
LayerHelper
(
"index_select"
,
**
locals
())
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
core
.
ops
.
index_select
(
input
,
index
,
'dim'
,
dim
)
return
core
.
ops
.
index_select
(
x
,
index
,
'dim'
,
axis
)
check_variable_and_dtype
(
input
,
'x'
,
helper
=
LayerHelper
(
"index_select"
,
**
locals
())
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'paddle.tensor.search.index_s
ample
'
)
'paddle.tensor.search.index_s
elect
'
)
check_variable_and_dtype
(
index
,
'index'
,
[
'int32'
,
'int64'
],
check_variable_and_dtype
(
index
,
'index'
,
[
'int32'
,
'int64'
],
'paddle.tensor.search.index_s
ample
'
)
'paddle.tensor.search.index_s
elect
'
)
out
=
helper
.
create_variable_for_type_inference
(
input
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
'index_select'
,
type
=
'index_select'
,
inputs
=
{
'X'
:
input
,
inputs
=
{
'X'
:
x
,
'Index'
:
index
},
'Index'
:
index
},
outputs
=
{
'Out'
:
out
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'dim'
:
dim
})
attrs
=
{
'dim'
:
axis
})
return
out
return
out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录