Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
a2256366
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a2256366
编写于
6月 10, 2021
作者:
L
liym27
提交者:
GitHub
6月 10, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[static getitem]Support index is list bool for getitem in static mode (#33298)
上级
11b57760
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
49 addition
and
3 deletion
+49
-3
python/paddle/fluid/tests/unittests/test_variable.py
python/paddle/fluid/tests/unittests/test_variable.py
+29
-0
python/paddle/fluid/variable_index.py
python/paddle/fluid/variable_index.py
+20
-3
未找到文件。
python/paddle/fluid/tests/unittests/test_variable.py
浏览文件 @
a2256366
...
@@ -245,6 +245,34 @@ class TestVariable(unittest.TestCase):
...
@@ -245,6 +245,34 @@ class TestVariable(unittest.TestCase):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
res
=
x
[[
1.2
,
0
]]
res
=
x
[[
1.2
,
0
]]
def
_test_slice_index_list_bool
(
self
,
place
):
data
=
np
.
random
.
rand
(
2
,
3
).
astype
(
"float32"
)
prog
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
prog
):
x
=
paddle
.
assign
(
data
)
idx0
=
[
True
,
False
]
idx1
=
[
False
,
True
]
idx2
=
[
False
,
False
]
idx3
=
[
True
,
True
]
out0
=
x
[
idx0
]
out1
=
x
[
idx1
]
out2
=
x
[
idx2
]
out3
=
x
[
idx3
]
exe
=
paddle
.
static
.
Executor
(
place
)
result
=
exe
.
run
(
prog
,
fetch_list
=
[
out0
,
out1
,
out2
,
out3
])
expected
=
[
data
[
idx0
],
data
[
idx1
],
data
[
idx2
],
data
[
idx3
]]
self
.
assertTrue
((
result
[
0
]
==
expected
[
0
]).
all
())
self
.
assertTrue
((
result
[
1
]
==
expected
[
1
]).
all
())
self
.
assertTrue
((
result
[
2
]
==
expected
[
2
]).
all
())
self
.
assertTrue
((
result
[
3
]
==
expected
[
3
]).
all
())
with
self
.
assertRaises
(
TypeError
):
res
=
x
[[
True
,
0
]]
def
test_slice
(
self
):
def
test_slice
(
self
):
places
=
[
fluid
.
CPUPlace
()]
places
=
[
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
if
core
.
is_compiled_with_cuda
():
...
@@ -255,6 +283,7 @@ class TestVariable(unittest.TestCase):
...
@@ -255,6 +283,7 @@ class TestVariable(unittest.TestCase):
self
.
_test_slice_index_tensor
(
place
)
self
.
_test_slice_index_tensor
(
place
)
self
.
_test_slice_index_list
(
place
)
self
.
_test_slice_index_list
(
place
)
self
.
_test_slice_index_ellipsis
(
place
)
self
.
_test_slice_index_ellipsis
(
place
)
self
.
_test_slice_index_list_bool
(
place
)
def
_tostring
(
self
):
def
_tostring
(
self
):
b
=
default_main_program
().
current_block
()
b
=
default_main_program
().
current_block
()
...
...
python/paddle/fluid/variable_index.py
浏览文件 @
a2256366
...
@@ -140,19 +140,36 @@ def _getitem_impl_(var, item):
...
@@ -140,19 +140,36 @@ def _getitem_impl_(var, item):
end
=
MAX_INTEGER
if
end
is
None
else
end
end
=
MAX_INTEGER
if
end
is
None
else
end
elif
isinstance
(
slice_item
,
list
):
elif
isinstance
(
slice_item
,
list
):
is_bool_list
=
False
for
i
in
slice_item
:
for
i
in
slice_item
:
if
not
isinstance
(
i
,
int
):
if
not
isinstance
(
i
,
(
int
,
bool
)):
raise
TypeError
(
"Only support int value in list"
)
raise
TypeError
(
"Only support int or bool in index list."
)
if
isinstance
(
i
,
bool
):
is_bool_list
=
True
break
if
len
(
item
)
!=
1
:
if
len
(
item
)
!=
1
:
raise
IndexError
(
raise
IndexError
(
"When index contains a list, its length must be 1, but received {}"
.
"When index contains a list, its length must be 1, but received {}"
.
format
(
len
(
item
)))
format
(
len
(
item
)))
if
is_bool_list
:
new_slice_item
=
[]
for
idx
,
ele
in
enumerate
(
slice_item
):
if
not
isinstance
(
ele
,
bool
):
raise
TypeError
(
"Mixed bool index with other types is not supported."
)
if
ele
is
True
:
new_slice_item
.
append
(
idx
)
slice_item
=
new_slice_item
from
.layers
import
assign
from
.layers
import
assign
from
..tensor
import
index_select
from
..tensor
import
index_select
idx
=
assign
(
np
.
array
(
slice_item
))
idx
=
assign
(
np
.
array
(
slice_item
)
.
astype
(
"int32"
)
)
return
index_select
(
var
,
index
=
idx
,
axis
=
0
)
return
index_select
(
var
,
index
=
idx
,
axis
=
0
)
elif
isinstance
(
slice_item
,
Variable
):
elif
isinstance
(
slice_item
,
Variable
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录