-
Notifications
You must be signed in to change notification settings - Fork 5.9k
【Hackathon 5th No.33】为 Paddle 新增 atleast_1d / atleast_2d / atleast_3d API -part #58323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b206320
6a7e2ed
b64f027
2395977
e85a8d8
4c8d828
84a0796
f2c82cd
309ab65
d336793
78d7788
dc09007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3886,6 +3886,193 @@ def reshape_(x, shape, name=None): | |
| return out | ||
|
|
||
|
|
||
| def atleast_1d(*inputs, name=None): | ||
| """ | ||
| Convert inputs to tensors and return the view with at least 1-dimension. Scalar inputs are converted, | ||
| one or high-dimensional inputs are preserved. | ||
|
|
||
| Args: | ||
| inputs (Tensor|list(Tensor)): One or more tensors. The data type is ``float16``, ``uint16``, ``float32``, ``float64``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16``. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Note: | ||
| ``int8``, ``uint8``, ``complex64``, ``complex128`` are not supported in static graph mode. | ||
|
|
||
| Returns: | ||
| One Tensor, if there is only one input. | ||
| List of Tensors, if there are more than one inputs. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
|
|
||
| >>> # one input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> out = paddle.atleast_1d(x) | ||
| >>> print(out) | ||
| Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [123]) | ||
|
|
||
| >>> # more than one inputs | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([1.23], dtype='float32') | ||
| >>> out = paddle.atleast_1d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [123]), Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [1.23000002])] | ||
|
|
||
| >>> # more than 1-D input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([[1.23]], dtype='float32') | ||
| >>> out = paddle.atleast_1d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [123]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [[1.23000002]])] | ||
| """ | ||
| out = [] | ||
| for tensor in inputs: | ||
| tensor = paddle.to_tensor(tensor) | ||
| if tensor.dim() == 0: | ||
| result = tensor.reshape((1,)) | ||
| else: | ||
| result = tensor | ||
| out.append(result) | ||
|
|
||
| if len(out) == 1: | ||
| return out[0] | ||
| else: | ||
| return out | ||
|
|
||
|
|
||
| def atleast_2d(*inputs, name=None): | ||
| """ | ||
| Convert inputs to tensors and return the view with at least 2-dimension. Two or high-dimensional inputs are preserved. | ||
|
|
||
| Args: | ||
| inputs (Tensor|list(Tensor)): One or more tensors. The data type is ``float16``, ``uint16``, ``float32``, ``float64``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16``. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Note: | ||
| ``int8``, ``uint8``, ``complex64``, ``complex128`` are not supported in static graph mode. | ||
|
|
||
| Returns: | ||
| One Tensor, if there is only one input. | ||
| List of Tensors, if there are more than one inputs. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
|
|
||
| >>> # one input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> out = paddle.atleast_2d(x) | ||
| >>> print(out) | ||
| Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[123]]) | ||
|
|
||
| >>> # more than one inputs | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([1.23], dtype='float32') | ||
| >>> out = paddle.atleast_2d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[123]]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [[1.23000002]])] | ||
|
|
||
| >>> # more than 2-D input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([[[1.23]]], dtype='float32') | ||
| >>> out = paddle.atleast_2d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[123]]), Tensor(shape=[1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [[[1.23000002]]])] | ||
| """ | ||
| out = [] | ||
| for tensor in inputs: | ||
| tensor = paddle.to_tensor(tensor) | ||
| if tensor.dim() == 0: | ||
| result = tensor.reshape((1, 1)) | ||
| elif tensor.dim() == 1: | ||
| result = paddle.unsqueeze(tensor, axis=0) | ||
| else: | ||
| result = tensor | ||
| out.append(result) | ||
|
|
||
| if len(out) == 1: | ||
| return out[0] | ||
| else: | ||
| return out | ||
|
|
||
|
|
||
| def atleast_3d(*inputs, name=None): | ||
| """ | ||
| Convert inputs to tensors and return the view with at least 3-dimension. Three or high-dimensional inputs are preserved. | ||
|
|
||
| Args: | ||
| inputs (Tensor|list(Tensor)): One or more tensors. The data type is ``float16``, ``uint16``, ``float32``, ``float64``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16``. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Note: | ||
| ``int8``, ``uint8``, ``complex64``, ``complex128`` are not supported in static graph mode. | ||
|
||
|
|
||
| Returns: | ||
| One Tensor, if there is only one input. | ||
| List of Tensors, if there are more than one inputs. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
|
|
||
| >>> # one input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> out = paddle.atleast_3d(x) | ||
| >>> print(out) | ||
| Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[[123]]]) | ||
|
|
||
| >>> # more than one inputs | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([1.23], dtype='float32') | ||
| >>> out = paddle.atleast_3d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[[123]]]), Tensor(shape=[1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [[[1.23000002]]])] | ||
|
|
||
| >>> # more than 3-D input | ||
| >>> x = paddle.to_tensor(123, dtype='int32') | ||
| >>> y = paddle.to_tensor([[[[1.23]]]], dtype='float32') | ||
| >>> out = paddle.atleast_3d(x, y) | ||
| >>> print(out) | ||
| [Tensor(shape=[1, 1, 1], dtype=int32, place=Place(cpu), stop_gradient=True, | ||
| [[[123]]]), Tensor(shape=[1, 1, 1, 1], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
| [[[[1.23000002]]]])] | ||
| """ | ||
| out = [] | ||
| for tensor in inputs: | ||
| tensor = paddle.to_tensor(tensor) | ||
| if tensor.dim() == 0: | ||
| result = tensor.reshape((1, 1, 1)) | ||
| elif tensor.dim() == 1: | ||
| result = paddle.unsqueeze(tensor, axis=[0, 2]) | ||
| elif tensor.dim() == 2: | ||
| result = paddle.unsqueeze(tensor, axis=2) | ||
| else: | ||
| result = tensor | ||
| out.append(result) | ||
|
|
||
| if len(out) == 1: | ||
| return out[0] | ||
| else: | ||
| return out | ||
|
|
||
|
|
||
| def gather_nd(x, index, name=None): | ||
| """ | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we need to support the usage of
x.atleast_1d(), x.atleast_2d(), x.atleast_3d()whenxis a tensor ? If so, add them to the list oftensor_method_funcbelow