Torch - 在维度上应用函数

我想能够将设计用于3D张量的函数应用于4D张量中的每个3D张量,即image.translate()。例如,我可以将该函数分别应用于尺寸为(3,50,50)的两个图像,但如果我可以将它们的4D拼接的(2,3,50,50)输入,则会更好。

这可能可以在for循环中完成,但我想知道是否有任何内置函数可实现此功能。谢谢。

点赞
用户4850610
用户4850610

我在Torch中还没有找到这样的函数,当然你可以自己定义一个函数,让你的生活更加美好:

function apply_to_slices(tensor,dimension, func,...)
    for i,slice in ipairs(tensor:split(1,dimension))do
        func(slice, i,...)
    end
    返回张量
end

例子:

function power_fill(tensor, i, power)
    power = power or 1
    tensor:fill(i ^ power)
end

A = torch.Tensor(5, 6)

apply_to_slices(A, 1, power_fill)

 1  1  1  1  1  1
 2  2  2  2  2  2
 3  3  3  3  3  3
 4  4  4  4  4  4
 5  5  5  5  5  5
[torch.DoubleTensor of size 5x6]

apply_to_slices(A, 2, power_fill, 3)

   1    8   27   64  125  216
   1    8   27   64  125  216
   1    8   27   64  125  216
   1    8   27   64  125  216
   1    8   27   64  125  216
[torch.DoubleTensor of size 5x6]
2015-12-09 09:29:05