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

映射文档 torch.nn.functional.embedding #5928

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## [ 参数不一致 ] torch.nn.functional.binary_cross_entropy
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.

done


### [torch.nn.functional.binary_cross_entropy](https://pytorch.org/docs/2.0/generated/torch.nn.functional.binary_cross_entropy.html?highlight=binary_cross_entropy#torch.nn.functional.binary_cross_entropy)

```python
torch.nn.functional.binary_cross_entropy(input, target, 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)
```

两者功能一致,torch 参数多,具体如下:
### 参数差异
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | input | 表示输入的 Tensor |
| target | label | 标签 |
| weight | weight | 指定每个 batch 的权重 |
| size_average | - | 已废弃,和 reduce 组合决定损失计算方式 |
| reduce | - | 已废弃,和 size_average 组合决定损失计算方式 |
| reduction | reduction | 输出结果的计算方式 |


### 转写示例

```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.embedding
### [torch.nn.functional.embedding](https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html?highlight=torch+nn+functional+embedding#torch.nn.functional.embedding)

```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 | 表示存储 id 信息的 Tensor |
| weight | weight | 表示存储词嵌入权重参数的 Tensor |
| padding_idx | padding_idx | 在此区间内的参数及对应的梯度将会以 0 进行填充 |
| max_norm | - | 如果给定,Embeddding 向量的范数(范数的计算方式由 norm_type 决定)超过了 max_norm 这个界限,就要再进行归一化,PaddlePaddle 无此功能,暂无转写方式。 |
| norm_type | - | 为 maxnorm 选项计算 p-范数的 p。默认值 2,PaddlePaddle 暂无此功能,暂无转写方式。 |
| scale_grad_by_freq | - | 是否根据单词在 mini-batch 中出现的频率,对梯度进行放缩,PaddlePaddle 暂无此功能。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

少了个 暂无转写方式。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

Choose a reason for hiding this comment

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

写成统一一致的吧:暂无转写方式

| sparse | sparse | 表示是否使用稀疏更新。 |

### 转写示例
```python
Copy link
Collaborator

Choose a reason for hiding this comment

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

转写前需要写下是 转写哪个参数,请严格按规范来

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

Choose a reason for hiding this comment

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

那这个是转哪个参数呢

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(x,embedding_matrix)

# paddle 写法
paddle.nn.functional.embedding(x,embedding_matrix)

# 当 PyTorch 使用 max_norm、norm_type、scale_grad_by_freq 任意参数时,暂不支持转写
torch.nn.functional.embedding(x,embedding_matrix,max_norm=2.0)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## [ 参数不一致 ]torch.nn.functional.unfold
### [torch.nn.functional.unfold](https://pytorch.org/docs/stable/generated/torch.nn.functional.unfold.html#torch.nn.functional.unfold)

```python
torch.nn.functional.unfold(input,
kernel_size,
dilation=1,
padding=0,
stride=1)
```

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

```python
paddle.nn.functional.unfold(x,
kernel_size=[3, 3],
strides=1,
addings=1,
dilation=1,
name=None)
```
其中 Paddle 与 Pytorch 前四个参数所支持的参数类型不一致,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 输入 Tensor |
| kernel_size | kernel_sizes | 卷积核大小, PyTorch 参数类型为 int、tuple(int) 或者 list(int), Paddle 参数类型为 int 或者 list(int)。 |
| dilation | dilations | 卷积膨胀,PyTorch 参数类型为 int、tuple(int) 或者 list(int), Paddle 参数类型为 int 或者 list(int)。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

可以写成,Pytorch支持int、tuple(int) 或者 list(int),Paddle仅支持int 或者 list(int),突出我们比他们少

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

| padding | paddings | 每个维度的扩展,PyTorch 参数类型为 int、tuple(int) 或者 list(int), Paddle 参数类型为 int 或者 list(int)。 |
| stride | strides | 步长大小,PyTorch 参数类型为 int、tuple(int) 或者 list(int), Paddle 参数类型为 int 或者 list(int)。|

### 转写示例
``` python
Copy link
Collaborator

Choose a reason for hiding this comment

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

转写前需要写下是 转写哪个参数

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

# PyTorch 写法:
unfold = nn.functional.unfold(input,kernel_size=(2, 3))

# Paddle 写法
unfold = nn.functional.unfold(x,kernel_size=[2, 3])
```
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ paddle.nn.Embedding(num_embeddings,
| scale_grad_by_freq | - | 是否根据单词在 mini-batch 中出现的频率,对梯度进行放缩,PaddlePaddle 暂无此功能。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这些都用统一的话术描述:Paddle无此功能,暂无转写方式。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

| sparse | sparse | 表示是否使用稀疏更新。 |
| - | weight_attr | 指定权重参数属性的对象,Pytorch 无此参数,Paddle 保持默认即可。 |


### 转写示例
```python
Copy link
Collaborator

Choose a reason for hiding this comment

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

转写前需要写下是 转写哪个参数,请严格按规范来

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

torch.nn.Embedding(4, 3)

# paddle 写法
paddle.nn.Embedding(4,3)
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Jul 4, 2023

Choose a reason for hiding this comment

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

这个不需要转写的

转写示例是针对某个参数,抬头说明是要转哪个参数

不需要转写的情况不要写

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done



torch.nn.Embedding(4, 3,padding_idx=0)

# paddle 写法
paddle.nn.Embedding(4, 3,padding_idx=0)

# 当 PyTorch 使用 max_norm、norm_type、scale_grad_by_freq 任意参数时,暂不支持转写
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个不用写

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

torch.nn.Embedding(4, 3,padding_idx=padding_idx,max_norm=2.0)
```