-
Notifications
You must be signed in to change notification settings - Fork 777
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
【Hackathon 5th No.33】为 Paddle 新增 atleast_1d / atleast_2d / atleast_3d API #6317
Changes from 6 commits
4bb8da3
dff577b
9509523
47e76f2
bb172f9
5826df2
7de1ef9
38cd108
48c671e
02b45cb
a05cbe9
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _cn_api_paddle_atleast_1d: | ||
|
||
atleast_1d | ||
------------------------------- | ||
|
||
.. py:function:: paddle.atleast_1d(*inputs, name=None) | ||
|
||
将输入转换为张量并返回至少为 ``1`` 维的视图。标量输入会被转换, ``1`` 维或更高维的输入则会被保留。 | ||
|
||
参数 | ||
:::::::::::: | ||
|
||
- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
返回 | ||
:::::::::::: | ||
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。 | ||
|
||
代码示例 | ||
:::::::::::: | ||
|
||
COPY-FROM: paddle.atleast_1d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _cn_api_paddle_atleast_2d: | ||
|
||
atleast_2d | ||
------------------------------- | ||
|
||
.. py:function:: paddle.atleast_2d(*inputs, name=None) | ||
|
||
将输入转换为张量并返回至少为 ``2`` 维的视图。 ``2`` 维或更高维的输入会被保留。 | ||
|
||
参数 | ||
:::::::::::: | ||
|
||
- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
返回 | ||
:::::::::::: | ||
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。 | ||
|
||
代码示例 | ||
:::::::::::: | ||
|
||
COPY-FROM: paddle.atleast_2d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _cn_api_paddle_atleast_3d: | ||
|
||
atleast_3d | ||
------------------------------- | ||
|
||
.. py:function:: paddle.atleast_3d(*inputs, name=None) | ||
|
||
将输入转换为张量并返回至少为 ``3`` 维的视图。 ``3`` 维或更高维的输入会被保留。 | ||
|
||
参数 | ||
:::::::::::: | ||
|
||
- **inputs** (Tensor|list(Tensor)) - 一个或多个 Tensor,数据类型为: ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` 或 ``bool``。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
返回 | ||
:::::::::::: | ||
Tensor 或者由 Tensor 组成的 list。当只有一个输入的时候返回一个 Tensor,当有多个输入的时候返回由 Tensor 组成的 list。 | ||
|
||
代码示例 | ||
:::::::::::: | ||
|
||
COPY-FROM: paddle.atleast_3d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## [ 参数不一致 ]torch.atleast_1d | ||
|
||
### [torch.atleast_1d](https://pytorch.org/docs/stable/generated/torch.atleast_1d.html#torch-atleast-1d) | ||
|
||
```python | ||
torch.atleast_1d(*tensors) | ||
``` | ||
|
||
### [paddle.atleast_1d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_1d_cn.html#atleast_1d) | ||
|
||
```python | ||
paddle.atleast_1d(*inputs, name=None) | ||
``` | ||
|
||
PyTorch 与 Paddle 参数不一致,具体如下: | ||
|
||
### 参数映射 | ||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,参数不一致。 | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 输入的 Tensor,仅当torch输入为 |
||
|
||
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里不是多个Tensor吧,写不一致的输入case情形,其他情况两者一致。 |
||
|
||
### 转写示例 | ||
|
||
#### 多个 Tensor | ||
```python | ||
# Pytorch 写法 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 写不一致的case就行,这里写伪代码,不用写多了 |
||
x = torch.tensor(0.3) | ||
y = torch.tensor(0.4) | ||
# 可以写为多个输入的方式 | ||
torch.atleast_1d(x, y) | ||
# 或者组合为一个 tuple,这两种方式是等价的 | ||
torch.atleast_1d((x, y)) | ||
|
||
# Paddle 写法 | ||
x = paddle.to_tensor(0.3) | ||
y = paddle.to_tensor(0.4) | ||
# 这里只能分别传入 | ||
paddle.atleast_1d(x, y) | ||
# 下面这种方式需要 x, y 具有一样的 shape | ||
paddle.atleast_1d((x, y)) | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## [ 参数不一致 ]torch.atleast_2d | ||
|
||
### [torch.atleast_2d](https://pytorch.org/docs/stable/generated/torch.atleast_2d.html#torch-atleast-2d) | ||
|
||
```python | ||
torch.atleast_2d(*tensors) | ||
``` | ||
|
||
### [paddle.atleast_2d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_2d_cn.html#atleast_2d) | ||
|
||
```python | ||
paddle.atleast_2d(*inputs, name=None) | ||
``` | ||
|
||
PyTorch 与 Paddle 参数不一致,具体如下: | ||
|
||
### 参数映射 | ||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,参数不一致。 | | ||
|
||
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。 | ||
|
||
### 转写示例 | ||
|
||
#### 多个 Tensor | ||
```python | ||
# Pytorch 写法 | ||
x = torch.tensor(0.3) | ||
y = torch.tensor(0.4) | ||
# 可以写为多个输入的方式 | ||
torch.atleast_2d(x, y) | ||
# 或者组合为一个 tuple,这两种方式是等价的 | ||
torch.atleast_2d((x, y)) | ||
|
||
# Paddle 写法 | ||
x = paddle.to_tensor(0.3) | ||
y = paddle.to_tensor(0.4) | ||
# 这里只能分别传入 | ||
paddle.atleast_2d(x, y) | ||
# 下面这种方式需要 x, y 具有一样的 shape | ||
paddle.atleast_2d((x, y)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 目前是 参数不一致,因为涉及到某些case的用法不同 但有点疑问:API针对这种情况为什么不和torch一致呢?broadcast 后作为单个输入是更合理的行为吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PaddlePaddle/Paddle#58323 (comment) 这里是与 numpy 对齐 ~ torch 的这种处理方式至少在 python 的编程习惯上存在歧义 ~ paddle.atleast_1d(*inputs) 这里本身就是处理多个输入的情况,而在 python 中, 后者 In [1]: import numpy as np
In [2]: np.atleast_1d((123, 234)) # 一个 tuple 输入
Out[2]: array([123, 234])
In [3]: np.atleast_1d((np.array(123), np.array(234))) # 一个 tuple 输入,包含两个 array
Out[3]: array([123, 234])
In [4]: np.atleast_1d(*(np.array(123), np.array(234))) # 将 tuple 展开
Out[4]: [array([123]), array([234])]
In [5]: np.atleast_1d(123, 234) # 两个输入
Out[5]: [array([123]), array([234])] torch 之所以这么做,个人感觉是因为,这几个 api 是直接用底层 c++ 处理的,上层封装没有进行区分 ... ... 因此
个人感觉,不是 ~ 因为这种输入本身就不合理 ~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PaddlePaddle/Paddle#58323 (comment) 这里是与 numpy 对齐 ~ torch 的这种处理方式至少在 python 的编程习惯上存在歧义 ~ paddle.atleast_1d(*inputs) 这里本身就是处理多个输入的情况,而在 python 中, 后者 In [1]: import numpy as np
In [2]: np.atleast_1d((123, 234)) # 一个 tuple 输入
Out[2]: array([123, 234])
In [3]: np.atleast_1d((np.array(123), np.array(234))) # 一个 tuple 输入,包含两个 array
Out[3]: array([123, 234])
In [4]: np.atleast_1d(*(np.array(123), np.array(234))) # 将 tuple 展开
Out[4]: [array([123]), array([234])]
In [5]: np.atleast_1d(123, 234) # 两个输入
Out[5]: [array([123]), array([234])] torch 之所以这么做,个人感觉是因为,这几个 api 是直接用底层 c++ 处理的,上层封装没有进行区分 ... ... 因此
个人感觉,不是 ~ 因为这种输入本身就不合理 ~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是broadcast为单个输入,还是concat为单个输入? |
||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## [ 参数不一致 ]torch.atleast_3d | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个算 仅参数不一致 嘛,还是 参数不一致,不确定捏,孙师傅快来 @sunzhongkai588 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗯 很纠结 不知道该咋写 😂 ~~~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@zhwesky2010 麻烦看一下, |
||
|
||
### [torch.atleast_3d](https://pytorch.org/docs/stable/generated/torch.atleast_3d.html#torch-atleast-3d) | ||
|
||
```python | ||
torch.atleast_3d(*tensors) | ||
``` | ||
|
||
### [paddle.atleast_3d](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/atleast_3d_cn.html#atleast_3d) | ||
|
||
```python | ||
paddle.atleast_3d(*inputs, name=None) | ||
``` | ||
|
||
PyTorch 与 Paddle 参数不一致,具体如下: | ||
|
||
### 参数映射 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| <font color='red'> tensors </font> | <font color='red'> inputs </font> | 输入的 Tensor ,参数不一致。 | | ||
|
||
PyTorch 与 Paddle 功能一致,但对于多个 Tensor 输入的处理方式略有不同,具体请看转写示例。 | ||
|
||
### 转写示例 | ||
|
||
#### 多个 Tensor | ||
```python | ||
# Pytorch 写法 | ||
x = torch.tensor(0.3) | ||
y = torch.tensor(0.4) | ||
# 可以写为多个输入的方式 | ||
torch.atleast_3d(x, y) | ||
# 或者组合为一个 tuple,这两种方式是等价的 | ||
torch.atleast_3d((x, y)) | ||
|
||
# Paddle 写法 | ||
x = paddle.to_tensor(0.3) | ||
y = paddle.to_tensor(0.4) | ||
# 这里只能分别传入 | ||
paddle.atleast_3d(x, y) | ||
# 下面这种方式需要 x, y 具有一样的 shape | ||
paddle.atleast_3d((x, y)) | ||
|
||
``` |
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.
有个疑问,这种多输入的Tensor,为什么会有类成员函数