Tensorflow - matmul of input matrix with batch data
<p>I have some data represented by <code>input_x</code>. It is a tensor of unknown size (should be inputted by batch) and each item there is of size <code>n</code>. <code>input_x</code> undergoes <code>tf.nn.embedding_lookup</code>, so that <code>embed</code> now has dimensions <code>[?, n, m]</code> where <code>m</code> is the embedding size and <code>?</code> refers to the unknown batch size. </p>

<p>This is described here:</p>

<pre><code>input_x = tf.placeholder(tf.int32, [None, n], name="input_x") 
embed = tf.nn.embedding_lookup(W, input_x)
</code></pre>

<p>I'm now trying to multiply each sample in my input data (which is now expanded by embedding dimension) by a matrix variable, <code>U</code>, and I can't seem to get how to do that.</p>

<p>I first tried using <code>tf.matmul</code> but it gives an error due to mismatch in shapes. I then tried the following, by expanding the dimension of <code>U</code> and applying <code>batch_matmul</code> (I also tried the function from <code>tf.nn.math_ops.</code>, the result was the same):</p>

<pre><code>U = tf.Variable( ... )    
U1 = tf.expand_dims(U,0)
h=tf.batch_matmul(embed, U1)
</code></pre>

<p>This passes the initial compilation, but then when actual data is applied, I get the following error:</p>

<p><code>In[0].dim(0) and In[1].dim(0) must be the same: [64,58,128] vs [1,128,128]</code></p>

<p>I also know why this is happening - I replicated the dimension of <code>U</code> and it is now <code>1</code>, but the minibatch size, <code>64</code>, doesn't fit. </p>

<p>How can I do that matrix multiplication on my tensor-matrix input correctly (for unknown batch size)?</p>