Skip to content
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

【映射文档】新增部分映射文档 #6512

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [组合替代实现]torch.nn.functional._Reduction.get_enum

### [torch.nn.functional._Reduction.get_enum](https://github.com/pytorch/pytorch/blob/3045b16488f14c9d941d33d63417e6ea52fb2544/torch/nn/_reduction.py#L7)

```python
torch.nn.functional._Reduction.get_enum(reduction)
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
torch.nn.functional._Reduction.get_enum(reduction)

# Paddle 写法
def get_enum(reduction: str) -> int:
if reduction == 'none':
ret = 0
elif reduction == 'mean':
ret = 1
elif reduction == 'elementwise_mean':
warnings.warn("reduction='elementwise_mean' is deprecated, please use reduction='mean' instead.")
ret = 1
elif reduction == 'sum':
ret = 2
else:
ret = -1
raise ValueError("{} is not a valid value for reduction".format(reduction))
return ret

get_enum(reduction)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [ torch 参数更多 ]torch.nn.functional.alpha_dropout

### [torch.nn.functional.alpha\_dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.alpha_dropout.html)

```python
torch.nn.functional.alpha_dropout(input, p=0.5, training=False, inplace=False)
```

### [paddle.nn.functional.alpha\_dropout](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/alpha_dropout_cn.html#alpha-dropout)

```python
paddle.nn.functional.alpha_dropout(x, p=0.5, training=True, name=None)
```

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| -------- | ------------ | -- |
| input | x | 输入的多维 Tensor,仅参数名不一致。 |
| p | p | 将输入节点置 0 的概率。 |
| training | training | 标记是否为训练阶段,PyTorch 默认值为 False,paddle 默认值为 True。 |
| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [ torch 参数更多 ]torch.nn.functional.binary_cross_entropy

### [torch.nn.functional.binary\_cross\_entropy](https://pytorch.org/docs/stable/generated/torch.nn.functional.binary_cross_entropy.html)

```python
torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=None, reduce=None, reduction='mean')
```

### [paddle.nn.functional.binary\_cross\_entropy](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/binary_cross_entropy_cn.html#binary-cross-entropy)

```python
paddle.nn.functional.binary_cross_entropy(input, label, weight=None, reduction='mean', name=None)
```

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | -- |
| input | input | 输入 Tensor。 |
| target | label | 标签 Tensor。 |
| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 |
| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduction | reduction | 指定应用于输出结果的计算方式。 |

### 转写示例

#### size_average、reduce
```python
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数
if size_average is None:
size_average = True
if reduce is None:
reduce = True

if size_average and reduce:
reduction = 'mean'
elif reduce:
reduction = 'sum'
else:
reduction = 'none'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## [ torch 参数更多 ]torch.nn.functional.dropout

### [torch.nn.functional.dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.dropout.html)

```python
torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False)
```

### [paddle.nn.functional.dropout](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/dropout_cn.html#dropout)

```python
paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode='upscale_in_train', name=None)
```

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
| -------- | ------------ | -- |
| input | x | 输入 Tensor,仅参数名不一致。 |
| p | p | 将输入节点置 0 的概率。 |
| - | axis | 指定对输入 Tensor 进行 dropout 操作的轴,PyTorch 无此参数,Paddle 保持默认即可。 |
| training | training | 标记是否为训练阶段。 |
| - | mode | 丢弃单元的方式,PyTorch 无此参数,Paddle 保持默认即可。 |
| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## [torch 参数]torch.nn.functional.embedding
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch参数更多

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

追加,该文件因为没有出现在主目录中,所以未作映射类别检查,后续考虑继续完善


### [torch.nn.functional.embedding](https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html)

```python
torch.nn.functional.embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False)
```

### [paddle.nn.functional.embedding](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/embedding_cn.html#embedding)

```python
paddle.nn.functional.embedding(x, weight, padding_idx=None, sparse=False, name=None)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------------ | ------------------ | -- |
| input | x | 输入 Tensor,仅参数名不同。 |
| weight | weight | 嵌入矩阵权重。 |
| padding_idx | padding_idx | 视为填充的下标,参数完全一致。 |
| max_norm | - | 重新归一化的最大范数,参数不一致,暂无转写方式。 |
| norm_type | - | Paddle 无此参数,参数不一致,暂无转写方式。 |
| scale_grad_by_freq | - | 按词频进行梯度缩放的比例,参数不一致,暂无转写方式。 |
| sparse | sparse | 是否使用稀疏更新。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [ 仅 paddle 参数更多 ]torch.nn.functional.hardsigmoid

### [torch.nn.functional.hardsigmoid](https://pytorch.org/docs/stable/generated/torch.nn.functional.hardsigmoid.html)

```python
torch.nn.functional.hardsigmoid(input, inplace=False)
```

### [paddle.nn.functional.hardsigmoid](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/hardsigmoid_cn.html#hardsigmoid)

```python
paddle.nn.functional.hardsigmoid(x, slope=0.1666667, offset=0.5, name=None)
```

其中 PyTorch 和 Paddle 功能一致,仅 paddle 参数更多,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
| ------- | ------------ | -- |
| input | x | 输入 Tensor,仅参数名不一致。 |
| - | slope | hardsigmoid 的斜率,PyTorch 无此参数,Paddle 保持默认即可。|
| - | offset | hardsigmoid 的截距,PyTorch 无此参数,Paddle 保持默认即可。|
| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## [ torch 参数更多 ]torch.nn.functional.multi_margin_loss

### [torch.nn.functional.multi\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multi_margin_loss.html)

```python
torch.nn.functional.multi_margin_loss(input, target, p=1, margin=1, weight=None, size_average=None, reduce=None, reduction='mean')
```

### [paddle.nn.functional.multi\_margin\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/multi_margin_loss_cn.html#multi-margin-loss)

```python
paddle.nn.functional.multi_margin_loss(input, label, p=1, margin=1.0, weight=None, reduction='mean', name=None)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | -- |
| input | input | 输入 Tensor。 |
| target | label | 标签 Tensor,仅参数名不一致。 |
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
| p | p | 手动指定范数。|
| margin | margin | 手动指定间距。 |
| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 |
| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduction | reduction | 指定应用于输出结果的计算方式。 |

### 转写示例

#### size_average、reduce
```python
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数
if size_average is None:
size_average = True
if reduce is None:
reduce = True

if size_average and reduce:
reduction = 'mean'
elif reduce:
reduction = 'sum'
else:
reduction = 'none'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## [ torch 参数更多 ]torch.nn.functional.multilabel_soft_margin_loss

### [torch.nn.functional.multilabel\_soft\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multilabel_soft_margin_loss.html)

```python
torch.nn.functional.multilabel_soft_margin_loss(input, target, weight=None, size_average=None, reduce=None, reduction='mean')
```

### [paddle.nn.functional.multi\_label\_soft\_margin\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/multi_label_soft_margin_loss_cn.html#multi-label-soft-margin-loss)

```python
paddle.nn.functional.multi_label_soft_margin_loss(input, label, weight=None, reduction='mean', name=None)
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | -- |
| input | input | 输入 Tensor。 |
| target | label | 标签 Tensor,仅参数名不一致。 |
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 |
| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduction | reduction | 指定应用于输出结果的计算方式。 |

### 转写示例

#### size_average、reduce
```python
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数
if size_average is None:
size_average = True
if reduce is None:
reduce = True

if size_average and reduce:
reduction = 'mean'
elif reduce:
reduction = 'sum'
else:
reduction = 'none'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 仅参数名不一致 ]torch.nn.functional.pairwise_distance

### [torch.nn.functional.pairwise\_distance](https://pytorch.org/docs/stable/generated/torch.nn.functional.pairwise_distance.html)

```python
torch.nn.functional.pairwise_distance(x1, x2, p=2.0, eps=1e-6, keepdim=False)
```

### [paddle.nn.functional.pairwise\_distance](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/pairwise_distance_cn.html#pairwise-distance)

```python
paddle.nn.functional.pairwise_distance(x, y, p=2., epsilon=1e-6, keepdim=False, name=None)
```

其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | -- |
| x1 | x | 输入 Tensor,仅参数名不一致。 |
| x2 | y | 输入 Tensor,仅参数名不一致。 |
| p | p | 指定 p 阶的范数。 |
| eps | epsilon | 添加到分母的一个很小值,避免发生除零错误。仅参数名不一致。 |
| keepdim | keepdim | 是否保留输出 Tensor 减少的维度。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## [ torch 参数更多 ]torch.nn.functional.poisson_nll_loss

### [torch.nn.functional.poisson\_nll\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.poisson_nll_loss.html)

```python
torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean')
```

### [paddle.nn.functional.poisson\_nll\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/poisson_nll_loss_cn.html#poisson-nll-loss)

```python
paddle.nn.functional.poisson_nll_loss(input, label, log_input=True, full=False, epsilon=1e-8, reduction='mean', name=None)
```

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------------ | ------------ | -- |
| input | input | 输入 Tensor。 |
| target | label | 标签 Tensor,仅参数名不一致。 |
| log_input | log_input | 输入是否为对数函数映射后结果。 |
| full | full | 是否在损失计算中包括 Stirling 近似项。 |
| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| eps | epsilon | 在 log_input 为 True 时使用的常数小量,仅参数名不一致。 |
| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 |
| reduction | reduction | 指定应用于输出结果的计算方式。 |

### 转写示例

#### size_average、reduce
```python
# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数
if size_average is None:
size_average = True
if reduce is None:
reduce = True

if size_average and reduce:
reduction = 'mean'
elif reduce:
reduction = 'sum'
else:
reduction = 'none'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [ 仅参数默认值不一致 ]torch.nn.functional.rrelu_

### [torch.nn.functional.rrelu\_](https://pytorch.org/docs/stable/generated/torch.nn.functional.rrelu_.html)

```python
torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False)
```

### [paddle.nn.functional.rrelu](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/functional/rrelu_cn.html)

```python
paddle.nn.functional.rrelu(x, lower=1./8., upper=1./3., training=True, name=None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个有没有inplace的版本对应

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有,目前实现上是直接用的 GenericMatcher

```

其中 PyTorch 和 Paddle 功能基本一致,前者会在不变更变量内存地址的情况下,直接改变变量的值,一般对网络训练结果影响不大。参数名与参数默认值不一致,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| -------- | ------------ | -- |
| input | x | 输入的 Tensor,仅参数名不一致。 |
| lower | lower | 负值斜率的随机值范围下限。 |
| upper | upper | 负值斜率的随机值范围上限。 |
| training | training | 标记是否为训练阶段。参数默认值不一致, PyTorch 默认值为 `False`,Paddle 默认值为 `True`,Paddle 需保持与 PyTorch 一致。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ 仅参数名不一致 ]torch.nn.functional.silu
RedContritio marked this conversation as resolved.
Show resolved Hide resolved

### [torch.nn.functional.silu](https://pytorch.org/docs/stable/generated/torch.nn.functional.silu.html)

```python
torch.nn.functional.silu(input, inplace=False)
```

### [paddle.nn.functional.silu](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/functional/silu_cn.html#silu)

```python
paddle.nn.functional.silu(x, name=None)
```

其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | -- |
| input | x | 输入 Tensor,仅参数名不一致。 |
| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 |
Loading