问题描述
我希望能够将一个为 3D 张量设计的函数应用于 4D 张量中的每个 3D 张量,即 image.translate()
.例如,我可以将该函数单独应用于维度 (3,50,50) 的两个图像,但如果我可以提供它们的 (2,3,50,50) 4D 连接,那就太好了.
I would like to be able to apply a function which is designed for a 3D tensor to each 3D tensor in a 4D tensor, namely image.translate()
. For example, I can apply the function individually to two images of dimension (3,50,50) but it would be great if I could feed their 4D concatenation of (2,3,50,50).
这可能可以在 for 循环中完成,但我想知道是否有任何内置函数.谢谢.
This could probably be done in a for loop but I was wondering if there was any built in function for this. Thanks.
推荐答案
我还没有在 Torch
中找到这样的功能.当然,你可以自己定义一个,让你的生活更快乐一点:
I haven't managed to find such a function in Torch
. You can, of course, define one yourself to make your life a little bit happier:
function apply_to_slices(tensor, dimension, func, ...)
for i, slice in ipairs(tensor:split(1, dimension)) do
func(slice, i, ...)
end
return tensor
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]
这篇关于Torch - 在维度上应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!