Python 在pytorch中的外部总和等
Numpy为任何Python 在pytorch中的外部总和等,python,optimization,pytorch,torch,Python,Optimization,Pytorch,Torch,Numpy为任何RxR->R函数提供优化的外部操作,如np.multiply.outer或np.subtract.outer,其行为如下: >>> np.subtract.outer([6, 5, 4], [3, 2, 1]) array([[3, 4, 5], [2, 3, 4], [1, 2, 3]]) 似乎没有提供这样的功能(或者我错过了)。 使用火炬张量的最佳/常用/最快/最干净的方法是什么?请参见: 许多PyTorch操作支持NumPy广
RxR->R
函数提供优化的外部操作,如np.multiply.outer
或np.subtract.outer
,其行为如下:
>>> np.subtract.outer([6, 5, 4], [3, 2, 1])
array([[3, 4, 5],
[2, 3, 4],
[1, 2, 3]])
似乎没有提供这样的功能(或者我错过了)。使用火炬张量的最佳/常用/最快/最干净的方法是什么?请参见: 许多PyTorch操作支持NumPy广播语义 外部减法是从2d数组到1d数组的广播减法,因此基本上可以将第一个数组重塑为(3,1),然后从中减去第二个数组:
x = torch.Tensor([6, 5, 4])
y = torch.Tensor([3, 2, 1])
x.reshape(-1, 1) - y
#tensor([[3., 4., 5.],
# [2., 3., 4.],
# [1., 2., 3.]])