-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Closed
Labels
PFCCPaddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfccPaddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfccstatus/close已关闭已关闭
Description
版本、环境信息:
- AIStudio GPU 版
- PaddlePaddle 版本号:2.1.2
- Python 版本号:3.7.4
问题描述
使用 shape 为 (1, ) 的 Tensor 对另一个 Tensor 进行索引时,结果会减少一根轴(见下面代码)
import paddle
import numpy as np
n = 10
a_numpy = np.arange(16).reshape(16, 1) # (16, 1)
b_numpy = np.random.permutation(n) # (n, )
a_paddle = paddle.to_tensor(a_numpy)
b_paddle = paddle.to_tensor(b_numpy)
# 当 n != 1 时,下面结果的 shpae 为 (n, 1)
# 当 n == 1 时,下面结果的 shape 为 (1) <--- 错误发生在这里,显然少了一根轴
a_paddle[b_paddle]
作为对比,我使用 numpy 和 pytorch 都进行了测试,结果是和预期一致的,即便索引(b)的 shape 为 (1, ),结果也不会少一根轴
import numpy as np
import torch
n = 10
a_numpy = np.arange(16).reshape(16, 1) # (16, 1)
b_numpy = np.random.permutation(n) # (n, )
a_numpy[b_numpy] # (n, 1)
a_torch = torch.from_numpy(a_numpy) # (16, 1)
b_torch = torch.from_numpy(b_numpy) # (n, )
a_torch[b_torch] # (n, 1)
我不太清楚这在 paddle 中是否是一个预期的效果,我并没有在文档中找到关于该特殊用例的详细说明,但至少它与 numpy 和 pytorch 的表现是不一致的
另外,为了防止数据在 n == 1 时丢失一根轴,目前我暂时使用的解决方案是
if len(b_paddle) == 1:
c = a_paddle[b_paddle].unsqueeze(0)
else:
c = a_paddle[b_paddle]
Metadata
Metadata
Assignees
Labels
PFCCPaddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfccPaddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfccstatus/close已关闭已关闭