这似乎是一个基本问题,但我无法解决这个问题.
在我的神经网络的前向传递中,我有一个8x3x3形状的输出张量,其中8是我的批量大小.我们可以假设每个3x3张量是非奇异矩阵.我需要找到这些矩阵的逆.PyTorch inverse()函数仅适用于方形矩阵.由于我现在有8x3x3,如何以可区分的方式将此功能应用于批处理中的每个矩阵?
如果我遍历样本并将反转追加到python列表,然后我将其转换为PyTorch张量,那么在backprop期间它是否会出现问题?(我问,因为将PyTorch张量转换为numpy以执行某些操作然后返回张量将不会在backprop期间为此类操作计算渐变)
当我尝试做类似的事情时,我也会收到以下错误.
a = torch.arange(0,8).view(-1,2,2)
b = [m.inverse() for m in a]
c = torch.FloatTensor(b)
Run Code Online (Sandbox Code Playgroud)
TypeError:'torch.FloatTensor'对象不支持索引
EDIT:
As of Pytorch version 1.0, torch.inverse now supports batches of tensors. See here. So you can simply use the built-in function torch.inverse
OLD ANSWER
There are plans to implement batched inverse soon. For discussion, see for example issue 7500 or issue 9102. However, as of the time of writing, the current stable version (0.4.1), no batch inverse operation is available.
Having said that, recently batch support for torch.gesv was added. This can be (ab)used to define your own batched inverse operation along the following lines:
def b_inv(b_mat):
eye = b_mat.new_ones(b_mat.size(-1)).diag().expand_as(b_mat)
b_inv, _ = torch.gesv(eye, b_mat)
return b_inv
Run Code Online (Sandbox Code Playgroud)
我发现在GPU上运行时,这比for循环可提高速度。
您可以使用 分割张量torch.functional.unbind(),对结果的每个元素应用逆,然后堆栈回来:
a = torch.arange(0,8).view(-1,2,2)
b = [t.inverse() for t in torch.functional.unbind(a)]
c = torch.functional.stack(b)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9583 次 |
| 最近记录: |