在 torch7 中平均居中张量

Mean Centering a Tensor in torch7

我想知道是否有更简单(或更有效)的方法来执行均值居中操作?

目前,我正在做以下事情:

data = torch.Tensor{
   {40,  6,  4},
   {44, 10,  4},
   {46, 12,  5},
   {48, 14,  7},
   {52, 16,  9},
   {58, 18, 12},
   {60, 22, 14},
   {68, 24, 20},
   {74, 26, 21},
   {80, 32, 24}
}

x = torch.Tensor(data:size(2)):fill(0)
centeredData = torch.Tensor(data:size())

x:addmv(1/data:size(1), data:t(), torch.ones(data:size(1)))
torch.addr(centeredData:t(), data:t(), -1, x, torch.ones(data:size(1)))

-- double check it's mean centered:
assert(torch.mean(centeredData,1) == torch.zeros(1,data:size(2)), "not centered")
mean = data:mean(1)
data:add(-1, mean:expandAs(data))