diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 6f8229e6f18f52058bf957d55d31ddabe73ebfdd..129a03811255566cd8c9ceb15de6c1442ef262ec 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -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 diff --git a/python/paddle/fluid/tests/unittests/test_tensor_to_list.py b/python/paddle/fluid/tests/unittests/test_tensor_to_list.py new file mode 100644 index 0000000000000000000000000000000000000000..73b91297e6fd62e083b340dffa72ddddaf16863c --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_tensor_to_list.py @@ -0,0 +1,44 @@ +# 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() diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 696775434b9671cd7d7a414e1b8e66c5b9aa61a6..669225d813641176a35ed270c9b13a13de00ce0e 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -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): """