Skip to content
Merged
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
41 changes: 41 additions & 0 deletions docs/faq/train_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@

----------

##### 问题:请问`paddle.gather`和`torch.gather`有什么区别?

+ 答复:[`paddle.gather`](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/gather_cn.html#gather)和`torch.gather`的函数签名分别为:

```python
paddle.gather(x, index, axis=None, name=None)
torch.gather(input, dim, index, *, sparse_grad=False, out=None)
```

其中,`paddle.gather`的参数`x`,`index`,`axis`分别与`torch.gather`的参数`input`,`index`,`dim`意义相同。

两者在输入形状、输出形状、计算公式等方面都有区别,具体如下:

- `paddle.gather`

- 输入形状:`x`可以是任意的`N`维Tensor。但`index`必须是形状为`[M]`的一维Tensor,或形状为`[M, 1]`的二维Tensor。

- 输出形状:输出Tensor `out`的形状`shape_out`和`x`的形状`shape_x`的关系为:`shape_out[i] = shape_x[i] if i != axis else M`。

- 计算公式:`out[i_1][i_2]...[i_axis]...[i_N] = x[i_1][i_2]...[index[i_axis]]...[i_N]` 。

- 举例说明:假设`x`的形状为`[N1, N2, N3]`,`index`的形状为`[M]`,`axis`的值为1,那么输出`out`的形状为`[N1, M, N3]`,且`out[i_1][i_2][i_3] = x[i_1][index[i_2]][i_3]`。

- `torch.gather`

- 输入形状:`input`可以是任意的`N`维Tensor,且`index.rank`必须等于`input.rank`。

- 输出形状:输出Tensor `out`的形状与`index`相同。

- 计算公式:`out[i_1][i_2]...[i_dim]...[i_N] = input[i_1][i_2]...[index[i_1][i_2]...[i_N]]...[i_N]`。

- 举例说明:假设`x`的形状为`[N1, N2, N3]`,`index`的形状为`[M1, M2, M3]`,`dim`的值为1,那么输出`out`的形状为`[M1, M2, M3]`,且`out[i_1][i_2][i_3] = input[i_1][index[i_1][i_2][i_3]][i_3]`。

- 异同比较

- 只有当`x.rank == 1`且`index.rank == 1`时,`paddle.gather`和`torch.gather`功能相同。其余情况两者无法直接互换使用。

- `paddle.gather`不支持`torch.gather`的`sparse_grad`参数。

----------
Copy link
Collaborator

@Ligoml Ligoml Nov 12, 2021

Choose a reason for hiding this comment

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

使用预览工具 check 后发现在官网中序号有误,需要修改下 md:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done


##### 问题:在模型组网时,inplace参数的设置会影响梯度回传吗?经过不带参数的op之后,梯度是否会保留下来?

+ 答复:inplace 参数不会影响梯度回传。只要用户没有手动设置`stop_gradient=True`,梯度都会保留下来。
Expand Down