Summing over product of tensor elements and vector
I'm trying to write a python code for a higher order (d=4) factorization machine that returns the scalar result y of

Where x is  a vector of some length n, v is a vector of length n, w is an upper triangular matrix of size n by n, and t is a rank 4 tensor of size n by n by n by n. The easiest implementation is just for loops over each index:
for i in range(0,len(x)):
    for j in range(0,len(x)):
        for k in range(0,len(x)):
            for l in range(0,len(x)):
                y += t[i,j,k,l] * x[i] * x[j] * x[k] * x[l]

The first two terms are easily calculated:
y = u @ x + x @ v @ x.T

My question- is there a better way of calculating the sum over the tensor than a nested for-loop? (currently looking at possible solutions in pytorch)

This seems like a perfect fit for torch.einsum:
&gt;&gt;&gt; torch.einsum('ijkl,i,j,k,l-&gt;', t, *(x,)*4)

In expanded form, this looks like torch.einsum('ijkl,i,j,k,l-&gt;', t, x, x, x, x) and computes the value defined by your four for loops:
for i, j, k, l in cartesian_prod:
    y += t[i,j,k,l] * x[i] * x[j] * x[k] * x[l]

Where cartesian_prod is the cartesian product: range(len(x))^4
