I have my data tensor which is of the shape [batch_size,512]
and I have a constant matrix with values only of 0 and 1 which has the shape [256,512]
.
I would like to compute efficiently for each batch the sum of the products of my vector (second dimension of the data tensor) only for the entries which are 1 and not 0.
An explaining example:
let us say I have 1-sized batch: the data tensor has the values [5,4,3,7,8,2]
and my constant matrix has the values:
[0,1,1,0,0,0]
[1,0,0,0,0,0]
[1,1,1,0,0,1]
it means that I would like to compute for the first row 4*3
, for the second 5
and for the third 5*4*3*2
.
and in total for this batch, I get 4*3+5+5*4*3*2
which equals to 137.
Currently, I do it by iterating over all the rows, compute elementwise the product of my data and constant-matrix-row and then sum, which runs pretty slow.