未验证 提交 8beb1707 编写于 作者: Z zhiboniu 提交者: GitHub

add tensor.tolist() support (#32366)

上级 7d4998ac
......@@ -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
......
# 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()
......@@ -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.
先完成此消息的编辑!
想要评论请 注册