title: Tensorflow compute multiplication by binary matrix
body: <p>I have my data tensor which is of the shape <code>[batch_size,512]</code> and I have a constant matrix with values only of 0 and 1 which has the shape <code>[256,512]</code>.</p>
  
  <p>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.</p>
  
  <p>An explaining example:
  let us say I have 1-sized batch: the data tensor has the values <code>[5,4,3,7,8,2]</code> and my constant matrix has the values:</p>
  
  <pre><code>[0,1,1,0,0,0]
  [1,0,0,0,0,0]
  [1,1,1,0,0,1]
  </code></pre>
  
  <p>it means that I would like to compute for the first row <code>4*3</code>, for the second <code>5</code> and for the third <code>5*4*3*2</code>.
  and in total for this batch, I get <code>4*3+5+5*4*3*2</code> 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.</p>