title: Matrix size-incompatible between Flatten and Dense layer
body: <p>I'm trying to implement multi-agent A2C algorithm with Keras.<br>
  There is an issue when I use Keras backend function (K.fuction).  </p>
  
  <p>I gave input whose shape is (None, 3, 4, 5).<br>
  This input passes through <code>Flatten()</code>, and then it is connected to <code>Dense()</code>.  </p>
  
  <p>It works fine when I use <code>model.predict(input)</code>, but a problem happened when I use customized optimizer using K.function.</p>
  
  <p>I gave input whose shape is (1000, 3, 4, 5) into <code>model.input</code> in K.function, but Matrix size-incompatible error appear:  </p>
  
  <pre><code>tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [3,20], In[1]: [60,64]
      [[{{node dense_1/Relu}}]]
  </code></pre>
  
  <p>It looks <code>Flatten()</code> doesn't work properly.  </p>
  
  <p>Network looks like this:  </p>
  
  <pre><code>def build_network(self):
      inp = Input(shape=(3,4,5))
      x = Flatten()(inp)
      x = Dense(64, activation='relu')(x)
      x = Dense(128, activation='relu')(x)
      x = Dense(128, activation='relu')(x)
      out = Dense(9, activation='softmax')(x)
  
      return Model(inp, out)
  
  self.model = self.build_network()
  </code></pre>
  
  <p><em>model.summary()</em> :<br>
  <a href="https://i.stack.imgur.com/iI5UH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iI5UH.png" alt=""></a>  </p>
  
  <p>The customized optimizer is here:  </p>
  
  <pre><code>import keras.backend as K
  
  def optimizer(self):
      weighted_actions = K.sum(self.action_pl * self.model.ouput, axis=1)
      eligibility = K.log(weighted_actions + 1e-10) * K.stop_gradient(self.advantages_pl)
      entropy = K.sum(self.model.output * K.log(self.model.output + 1e-10), axis=1)
      loss = 0.001 * entropy - K.sum(eligibility)
  
      _out = K.zeros(shape=(1))
      updates = self.rms_optimizer.get_updates(self.model.trainable_weights, [], loss)
  
      return K.fucntion([self.model.input, self.action_pl, self.advantages_pl], _out, updates=updates)
  
  self.opt = self.optimizer()
  
  self.opt([input, actions, advantages]) # Error occured here. input.shape = (1000,3,4,5)
  </code></pre>
  
  <p>I don't know why it happens and how to solve this issue.</p>
  
  <p>Any advice will be appreciated.</p>
  
  <p>Environment:
  Ubuntu16.04
  Python2.7
  Keras: 2.3.0
  tensorflow 1.14.0</p>