標題中的問題。有沒有一種操作或方法可以在不回圈的情況下進行廣播?這是一個帶有串列理解的簡單示例:
image = torch.tensor([[6, 9], [8.7, 5.5]])
c = torch.tensor([5.7675, 8.8325])
# with list comprehension
desired_result = torch.stack([image - c_i for c_i in c])
# output:
tensor([[[ 0.2325, 3.2325],
[ 2.9325, -0.2675]],
[[-2.8325, 0.1675],
[-0.1325, -3.3325]]])
我嘗試過重塑“標量陣列”,以各種方式獲得所需的結果,但沒有運氣。
uj5u.com熱心網友回復:
不確定是否torch有outer:
- np.subtract.outer(c.numpy(), image.numpy() )
輸出:
array([[[ 0.23250008, 3.2325 ],
[ 2.9325 , -0.26749992]],
[[-2.8325005 , 0.16749954],
[-0.13250065, -3.3325005 ]]], dtype=float32)
在 Torch 中,您可以展平兩個張量并重塑:
-(c[:,None] - image.ravel()).reshape(*c.shape, *image.shape)
輸出:
tensor([[[ 0.2325, 3.2325],
[ 2.9325, -0.2675]],
[[-2.8325, 0.1675],
[-0.1325, -3.3325]]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497818.html
上一篇:在Python中從多個陣列中的特定位置形成一個包含元素的陣列
下一篇:拆分字串并連接串列中的值
