First up, apologies for the confusing title… as for lack of a better wording, ill explain in numbers:
I have given two tensors
t1 = [[1,2,3], [2,3,4]]
t2 = [[5,6],[6,7],[7,8]]
further i have a function f(x,y) that takes one element of each tensor (e.g. t1[0] and t2[0]) and outputs some value.
What I now aim for, is an efficient way of building a new tensor t3, such that t3[0,1] = f(t1[0], t2[1])
[[f([1,2,3],[5,6]), f([1,2,3],[6,7]), f([1,2,3],[7,8])],
[f([2,3,4],[5,6]), f([2,3,4],[6,7]), f([2,3,4],[7,8])]]
Naturally I can solve this via a for loop but I have the feeling that there is a cool tensor magic way.
t3 = torch.zeros((len(t1), len(t2)))
for i in range((len(t1)):
for j in range(len(t2)):
t3[i,j] = f(t1[i], t2[j])
I think it would already be a huge help to get to know a way to just have t3 containing the raw values of t1 and t2 at the appropriate position. (I guess the function might then be easily applicable later on)
Thanks a lot in advance!