-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...uides/model_convert/convert_from_pytorch/api_difference/ops/torch.index_copy.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## [ 组合替代实现 ]torch.index_copy | ||
|
||
### [torch.index_copy](https://pytorch.org/docs/stable/generated/torch.index_copy.html#torch.index_copy) | ||
|
||
```python | ||
torch.index_copy(input, dim, index, source, *, out=None) | ||
``` | ||
|
||
Paddle 无此 API,需要组合实现。 | ||
|
||
### 转写示例 | ||
|
||
```python | ||
# Pytorch 写法,dim=0 | ||
y = torch.index_copy(input, 0, index, source) | ||
|
||
# Paddle 写法 | ||
y = paddle.scatter(input, index, source) | ||
|
||
# Pytorch 写法,dim>0 | ||
y = torch.index_copy(input, dim, index, source) | ||
|
||
# Paddle 写法 | ||
times, temp_shape, temp_index = paddle.prod(paddle.to_tensor(input.shape[:dim])), input.shape, index | ||
input, new_t = input.reshape([-1] + temp_shape[dim+1:]), source.reshape([-1] + temp_shape[dim+1:]) | ||
for i in range(1, times): | ||
temp_index= paddle.concat([temp_index, index+len(index)*i]) | ||
y = paddle.scatter(input, temp_index, new_t).reshape(temp_shape) | ||
``` |