减去 Torch 张量中所有列的向量

Subtract a vector across all columns in a tensor for Torch

给定一个 NxM 维的张量和一个 Nx1 维的向量,如何在 Torch 中减去张量每一列中的向量?

一种可能是使用expand。示例:

local A = torch.Tensor{{1, 2},{3, 4},{5,6}}
local B = torch.ones(3)
local C = A - B:view(3, 1):expandAs(A) -- make a 3x1 tensor before expand
print(C)
--  0  1
--  2  3
--  4  5
-- [torch.DoubleTensor of size 3x2]