title: Keras -- no Dot layer with broadcasting?
body: <p>I am using Keras 2.0.2 with the Tensorflow backend. I'm trying to do a batched dot product as part of a layer. I'm not entirely sure how to do this, and none of the things I've seen seem to have the desired functionality.</p>
  
  <p>In particular, I have two layers with shapes (None, 2, 50, 5, 3) and (None, 2, 50, 3, 1), and I want to take the dot product of the '3' dimension, and have that broadcasted over the (None, 2, 50) dimensions -- i.e., I want an output of (None, 2, 50, 5, 1). My use case is very simple: I am computing a matrix (5, 3) and a vector (3, 1) at each timestep of a sequence, and I want to take their dot product at each timestep.</p>
  
  <p>Here is an example showing what I am coming across:</p>
  
  <pre><code>import keras
  import keras.backend as K
  from keras.layers import Dot, Input
  
  v1 = K.variable(value=np.random.rand(2, 50, 5, 3))
  v2 = K.variable(value=np.random.rand(2, 50, 3, 1))
  K.batch_dot(v1, v2)  # this works as desired, gives output shape: (2, 50, 5, 1)
  
  x1 = Input((2, 50, 3, 5)) # shape: (None, 2, 50, 3, 5)
  x2 = Input((2, 50, 3, 1)) # shape: (None, 2, 50, 3, 1)
  Dot(3)([x1, x2]) # output shape is (None, 2, 50, 5, 2, 50, 1)
  </code></pre>
  
  <p>It is strange because the code for the Dot layer (<a href="https://github.com/fchollet/keras/blob/master/keras/layers/merge.py" rel="nofollow noreferrer">https://github.com/fchollet/keras/blob/master/keras/layers/merge.py</a>) actually uses K.batch_dot, but the behaviour is not the same.</p>
  
  <p>This also seems to contradict the behaviour stated in the docs:
  "E.g. if applied to two tensors <code>a</code> and <code>b</code> of shape <code>(batch_size, n)</code>,
      the output will be a tensor of shape <code>(batch_size, 1)</code>
      where each entry <code>i</code> will be the dot product between
      <code>a[i]</code> and <code>b[i]</code>."</p>
  
  <p>I've tried other things with no success, e.g. wrapping K.batch_dot in a Lambda layer (which can only take a single input - shouldn't there be an equivalent general-purpose layer taking multiple inputs?) or wrapping the Dot layer in a TimeDistributed Layer (which seems to not work since TimeDistributed can't handle a list as input).</p>
  
  <p>Any advice would be greatly appreciated!</p>