Combine the permutation/combination of two tensors in new tensor

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!

Is the function arbitarary or is it fixed?

the function is fixed and returns a scalar value.

It is imperative that you first rewrite the function to take in t1 and t2 and give a tensor as an output rather than a scalar. Can you print out the function?

The actual function is rather complex but for the sake of this example we might assume it to be something like

def f(x,y):
    return torch.sum(x) + torch.sum(y)

where x in en element of t1 and y is an element of t2

Did you ever get an answer? In numpy something called np.meshgrid is used.

So in pytorch, it would be

torch.stack(
torch.meshgrid(x, y)
).T.reshape(-1,2)