In TensorFlow I have a rank-2 tensor M
(a matrix) of shape [D, D]
and a rank-3 tensor T
of shape [D, D, D]
.
I need to combine them to form a new matrix R
as follows: the element R[a, b+c-a]
is given by the sum of all the elements T[a, b, c]*M[b, c]
where b+c-a
is constant (where b+c-a
has to be between 0 and D-1
).
An inefficient way to create R
is with nested for
loops over the indices and a check that b+c-a
does not exceed D-1
(e.g. in numpy):
R = np.zeros([D,D])
for a in range(D):
for b in range(D):
for c in range(D):
if 0 <= b+c-a < D:
R[a, b+c-a] += T[a, b, c]*M[b, c]
but I would like to use broadcasting and/or other more efficient methods.
How can I achieve this?
for
loop code, it's not possible to understand what you want from this explanation. – Daniel Fb + c - a
may take the same value for more than one combination ofa
,b
andc
, even ifa
is fixed (e.g. for(a=2, b=1, c=3)
and(a=2, b=3, c=1)
the result is 2). Which of the values shouldR[a, b + c - a]
take? Or should it be a combination (addition, product)? – jdehesa