Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8beb1707
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看板
未验证
提交
8beb1707
编写于
4月 24, 2021
作者:
Z
zhiboniu
提交者:
GitHub
4月 24, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add tensor.tolist() support (#32366)
上级
7d4998ac
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
82 addition
and
1 deletion
+82
-1
python/paddle/__init__.py
python/paddle/__init__.py
+1
-0
python/paddle/fluid/tests/unittests/test_tensor_to_list.py
python/paddle/fluid/tests/unittests/test_tensor_to_list.py
+44
-0
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+37
-1
未找到文件。
python/paddle/__init__.py
浏览文件 @
8beb1707
...
...
@@ -150,6 +150,7 @@ 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.manipulation
import
tolist
#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_tensor_to_list.py
0 → 100644
浏览文件 @
8beb1707
# Copyright (c) 2019 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.
import
paddle.fluid
as
fluid
import
unittest
import
numpy
as
np
import
six
import
paddle
class
TensorToListTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
shape
=
[
11
,
25
,
32
,
43
]
def
test_tensor_tolist
(
self
):
places
=
[
fluid
.
CPUPlace
()]
if
fluid
.
core
.
is_compiled_with_cuda
():
places
.
append
(
fluid
.
CUDAPlace
(
0
))
places
.
append
(
fluid
.
CUDAPinnedPlace
())
for
p
in
places
:
np_arr
=
np
.
reshape
(
np
.
array
(
six
.
moves
.
range
(
np
.
prod
(
self
.
shape
))),
self
.
shape
)
expectlist
=
np_arr
.
tolist
()
t
=
paddle
.
to_tensor
(
np_arr
,
place
=
p
)
tensorlist
=
t
.
tolist
()
self
.
assertEqual
(
tensorlist
,
expectlist
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/tensor/manipulation.py
浏览文件 @
8beb1707
...
...
@@ -16,7 +16,7 @@ from __future__ import print_function
from
..fluid.layers
import
core
from
..fluid.layer_helper
import
LayerHelper
from
..fluid.framework
import
Variable
,
OpProtoHolder
,
in_dygraph_mode
,
convert_np_dtype_to_dtype_
,
device_guard
from
..fluid.framework
import
Variable
,
OpProtoHolder
,
in_dygraph_mode
,
convert_np_dtype_to_dtype_
,
device_guard
,
dygraph_only
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
...
...
@@ -76,6 +76,42 @@ def _print_warning_in_static_mode(api_name):
format
(
api_name
,
api_name
))
@
dygraph_only
def
tolist
(
x
):
"""
**Notes**:
**This API is ONLY available in Dygraph mode**
This function translate the paddle.Tensor to python list.
Args:
x(Tensor): ``x`` is the Tensor we want to translate to list
Returns:
list: A list that contain the same value of current Tensor.
Returns type:
list: dtype is same as current Tensor
Examples:
.. code-block:: python
import paddle
t = paddle.to_tensor([0,1,2,3,4])
expectlist = t.tolist()
print(expectlist) #[0, 1, 2, 3, 4]
expectlist = paddle.tolist(t)
print(expectlist) #[0, 1, 2, 3, 4]
"""
return
x
.
numpy
().
tolist
()
setattr
(
core
.
VarBase
,
'tolist'
,
tolist
)
def
concat
(
x
,
axis
=
0
,
name
=
None
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录