Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
b492545f
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
328
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
b492545f
编写于
1月 11, 2022
作者:
W
WJJ1995
提交者:
GitHub
1月 11, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add scatter mapper (#726)
上级
9247ffca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
1 deletion
+76
-1
docs/pytorch_project_convertor/API_docs/ops/README.md
docs/pytorch_project_convertor/API_docs/ops/README.md
+1
-1
docs/pytorch_project_convertor/API_docs/ops/torch.scatter.md
docs/pytorch_project_convertor/API_docs/ops/torch.scatter.md
+75
-0
未找到文件。
docs/pytorch_project_convertor/API_docs/ops/README.md
浏览文件 @
b492545f
...
...
@@ -152,7 +152,7 @@
| 147 |
[
torch.matmul
](
https://pytorch.org/docs/stable/generated/torch.matmul.html?highlight=matmul#torch.matmul
)
|
[
paddle.matmul
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/matmul_cn.html
)
|
[
差异对比
](
https://github.com/PaddlePaddle/X2Paddle/tree/develop/docs/pytorch_project_convertor/API_docs/ops/torch.matmul.md
)
|
| 148 |
[
torch.mm
](
https://pytorch.org/docs/stable/generated/torch.mm.html?highlight=mm#torch.mm
)
|
[
paddle.matmul
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/matmul_cn.html
)
|
[
差异对比
](
https://github.com/PaddlePaddle/X2Paddle/tree/develop/docs/pytorch_project_convertor/API_docs/ops/torch.mm.md
)
|
| 149 |
[
torch.mv
](
https://pytorch.org/docs/stable/generated/torch.mv.html?highlight=mv#torch.mv
)
| 无对应实现 |
[
组合实现
](
https://github.com/PaddlePaddle/X2Paddle/tree/develop/docs/pytorch_project_convertor/API_docs/ops/torch.mv.md
)
|
| 150 |
[
torch.scatter
](
https://pytorch.org/docs/stable/generated/torch.scatter.html?highlight=scatter#torch.scatter
)
|
[
paddle.scatter_nd_add
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/scatter_nd_add_cn.html
)
|
[
组合实现
](
https://github.com/PaddlePaddle/X2Paddle/tree/develop/docs/pytorch_project_convertor/API_docs/ops/torch.scatter.md
)
|
...
...
docs/pytorch_project_convertor/API_docs/ops/torch.scatter.md
0 → 100644
浏览文件 @
b492545f
## torch.scatter
### [torch.scatter](https://pytorch.org/docs/stable/generated/torch.scatter.html?highlight=scatter#torch.scatter)
```
python
torch
.
scatter
(
tensor
,
dim
,
index
,
src
)
```
### [paddle.scatter_nd_add](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/scatter_nd_add_cn.html)
```
python
paddle
.
scatter_nd_add
(
x
,
index
,
updates
,
name
=
None
)
```
### 参数差异
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| tensor | x | 表示输入Tensor。 |
| dim | - | 表示在哪一个维度scatter,Paddle无此参数 |
| index | index | 输入的索引张量 |
| src | updates | 输入的更新张量 |
### 功能差异
#### 使用方式
因 torch.scatter 与 paddle.scatter_nd_add 差异较大,必须使用 paddle.flatten + paddle.meshgrid + paddle.scatter_nd_add 组合实现,看如下例子
### 代码示例
```
python
# PyTorch 示例:
src
=
torch
.
arange
(
1
,
11
).
reshape
((
2
,
5
))
# 输出
# tensor([[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10]])
index
=
torch
.
tensor
([[
0
,
1
,
2
],
[
0
,
1
,
4
]])
torch
.
zeros
(
3
,
5
,
dtype
=
src
.
dtype
).
scatter_
(
1
,
index
,
src
)
# 输出
# tensor([[1, 2, 3, 0, 0],
# [6, 7, 0, 0, 8],
# [0, 0, 0, 0, 0]])
```
```
python
# PaddlePaddle 组合实现:
x
=
paddle
.
zeros
([
3
,
5
],
dtype
=
"int64"
)
updates
=
paddle
.
arange
(
1
,
11
).
reshape
([
2
,
5
])
# 输出
# Tensor(shape=[2, 5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
# [[1 , 2 , 3 , 4 , 5 ],
# [6 , 7 , 8 , 9 , 10]])
index
=
paddle
.
to_tensor
([[
0
,
1
,
2
],
[
0
,
1
,
4
]])
i
,
j
=
index
.
shape
grid_x
,
grid_y
=
paddle
.
meshgrid
(
paddle
.
arange
(
i
),
paddle
.
arange
(
j
))
# 若 PyTorch 的 dim 取 0
# index = paddle.stack([index.flatten(), grid_y.flatten()], axis=1)
# 若 PyTorch 的 dim 取 1
index
=
paddle
.
stack
([
grid_x
.
flatten
(),
index
.
flatten
()],
axis
=
1
)
# PaddlePaddle updates 的 shape 大小必须与 index 对应
updates_index
=
paddle
.
stack
([
grid_x
.
flatten
(),
grid_y
.
flatten
()],
axis
=
1
)
updates
=
paddle
.
gather_nd
(
updates
,
index
=
updates_index
)
paddle
.
scatter_nd_add
(
x
,
index
,
updates
)
# 输出
# Tensor(shape=[3, 5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
# [[1, 2, 3, 0, 0],
# [6, 7, 0, 0, 8],
# [0, 0, 0, 0, 0]])
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录