pytorch中的外和等
Outer sum, etc. in 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好像没有提供这样的功能(或者我错过了)。
使用火炬张量的最佳/通常/最快/最干净的方法是什么?
根据 documenation:
Many PyTorch operations support NumPy Broadcasting Semantics.
外部减法是从二维数组到一维数组的广播减法,因此基本上您可以将第一个数组重塑为 (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.]])
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好像没有提供这样的功能(或者我错过了)。
使用火炬张量的最佳/通常/最快/最干净的方法是什么?
根据 documenation:
Many PyTorch operations support NumPy Broadcasting Semantics.
外部减法是从二维数组到一维数组的广播减法,因此基本上您可以将第一个数组重塑为 (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.]])