title: Tensorflow, how to multiply a 2D tensor (matrix) by corresponding elements in a 1D vector
body: <p>I have a 2D matrix <code>M</code> of shape <code>[batch x dim]</code>, I have a vector <code>V</code> of shape <code>[batch]</code>. How can I multiply each of the columns in the matrix by the corresponding element in the V? That is:</p>
  
  <p><a href="https://i.stack.imgur.com/Kit9E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kit9E.png" alt="enter image description here"></a></p>
  
  <p>I know an inefficient numpy implementation would look like this:</p>
  
  <pre><code>import numpy as np
  M = np.random.uniform(size=(4, 10))
  V = np.random.randint(4)
  def tst(M, V):
    rows = []
    for i in range(len(M)):
      col = []
      for j in range(len(M[i])):
        col.append(M[i][j] * V[i])
      rows.append(col)
    return np.array(rows)
  </code></pre>
  
  <p>In tensorflow, given two tensors, what is the most efficient way to achieve this?</p>
  
  <pre><code>import tensorflow as tf
  sess = tf.InteractiveSession()
  M = tf.constant(np.random.normal(size=(4,10)), dtype=tf.float32)
  V = tf.constant([1,2,3,4], dtype=tf.float32)
  </code></pre>