<p dir="auto">This code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="m = krm.Graph()
m.add_input('x', input_shape=(10,))
m.add_node(klc.Dense(5), 'y', input='x', create_output=True)
m.compile(loss={'y': 'mse'}, optimizer='adam')

with open('test.json', 'w') as f:
    f.write(m.to_json())

with open('test.json', 'r') as f:
    m = krm.model_from_json(f.read())"><pre class="notranslate"><code class="notranslate">m = krm.Graph()
m.add_input('x', input_shape=(10,))
m.add_node(klc.Dense(5), 'y', input='x', create_output=True)
m.compile(loss={'y': 'mse'}, optimizer='adam')

with open('test.json', 'w') as f:
    f.write(m.to_json())

with open('test.json', 'r') as f:
    m = krm.model_from_json(f.read())
</code></pre></div>
<p dir="auto">crashes with the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="... models.py&quot;, line 166, in model_from_json
... models.py&quot;, line 177, in model_from_config
... layer_utils.py&quot;, line 64, in container_from_config
... containers.py&quot;, line 568, in add_output
    raise Exception('Duplicate output identifier: ' + name)
Exception: Duplicate output identifier: y"><pre class="notranslate"><code class="notranslate">... models.py", line 166, in model_from_json
... models.py", line 177, in model_from_config
... layer_utils.py", line 64, in container_from_config
... containers.py", line 568, in add_output
    raise Exception('Duplicate output identifier: ' + name)
Exception: Duplicate output identifier: y
</code></pre></div>
<p dir="auto">I'll see if I can fix it.</p>
<p dir="auto">This is probably a duplicate of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="187552076" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/4302" data-hovercard-type="issue" data-hovercard-url="/keras-team/keras/issues/4302/hovercard" href="https://github.com/keras-team/keras/issues/4302">#4302</a> (where only tensorflow was tested).</p>
<h3 dir="auto">Problem</h3>
<p dir="auto">when using the Reshape layer it is not clear if one can use an unknown dimension (<code class="notranslate">-1</code>) like in <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html" rel="nofollow">numpy</a>. Using it results in an error. See the following example (please bare with we that this is not a useful example, but it's just to show the actual problem):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from keras.layers.core import Dense, Reshape
from keras.models import Sequential
import numpy as np

X = np.random.random((1000, 50))
y = np.random.random((1000, 1))

model = Sequential()
model.add(Dense(30, input_shape=(50,)))
model.add(Reshape((-1, 6)))
model.add(Reshape((30,)))
model.add(Dense(1))
model.compile(loss='mse', optimizer='sgd')
model.fit(X, y)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">keras</span>.<span class="pl-s1">layers</span>.<span class="pl-s1">core</span> <span class="pl-k">import</span> <span class="pl-v">Dense</span>, <span class="pl-v">Reshape</span>
<span class="pl-k">from</span> <span class="pl-s1">keras</span>.<span class="pl-s1">models</span> <span class="pl-k">import</span> <span class="pl-v">Sequential</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>((<span class="pl-c1">1000</span>, <span class="pl-c1">50</span>))
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>((<span class="pl-c1">1000</span>, <span class="pl-c1">1</span>))

<span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-v">Sequential</span>()
<span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Dense</span>(<span class="pl-c1">30</span>, <span class="pl-s1">input_shape</span><span class="pl-c1">=</span>(<span class="pl-c1">50</span>,)))
<span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Reshape</span>((<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">6</span>)))
<span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Reshape</span>((<span class="pl-c1">30</span>,)))
<span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Dense</span>(<span class="pl-c1">1</span>))
<span class="pl-s1">model</span>.<span class="pl-en">compile</span>(<span class="pl-s1">loss</span><span class="pl-c1">=</span><span class="pl-s">'mse'</span>, <span class="pl-s1">optimizer</span><span class="pl-c1">=</span><span class="pl-s">'sgd'</span>)
<span class="pl-s1">model</span>.<span class="pl-en">fit</span>(<span class="pl-v">X</span>, <span class="pl-s1">y</span>)</pre></div>
<p dir="auto">This results in the following error (tested with Keras 1.2.0 and Theano-0.9.0.dev4)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;test.py&quot;, line 10, in &lt;module&gt;
    model.add(Reshape((-1, 6)))
  File &quot;keras/models.py&quot;, line 327, in add
    output_tensor = layer(self.outputs[0])
  File &quot;keras/engine/topology.py&quot;, line 569, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File &quot;keras/engine/topology.py&quot;, line 632, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File &quot;keras/engine/topology.py&quot;, line 164, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File &quot;keras/layers/core.py&quot;, line 354, in call
    return K.reshape(x, (-1,) + target_shape)
  File &quot;keras/backend/theano_backend.py&quot;, line 567, in reshape
    return T.reshape(x, shape)
  File &quot;theano/tensor/basic.py&quot;, line 4722, in reshape
    newshape = as_tensor_variable(newshape)
  File &quot;theano/tensor/basic.py&quot;, line 212, in as_tensor_variable
    raise AsTensorError(&quot;Cannot convert %s to TensorType&quot; % str_x, type(x))
theano.tensor.var.AsTensorError: ('Cannot convert (-1, None, 5, 6) to TensorType', &lt;type 'tuple'&gt;)"><pre class="notranslate"><code class="notranslate">  File "test.py", line 10, in &lt;module&gt;
    model.add(Reshape((-1, 6)))
  File "keras/models.py", line 327, in add
    output_tensor = layer(self.outputs[0])
  File "keras/engine/topology.py", line 569, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "keras/engine/topology.py", line 632, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "keras/engine/topology.py", line 164, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "keras/layers/core.py", line 354, in call
    return K.reshape(x, (-1,) + target_shape)
  File "keras/backend/theano_backend.py", line 567, in reshape
    return T.reshape(x, shape)
  File "theano/tensor/basic.py", line 4722, in reshape
    newshape = as_tensor_variable(newshape)
  File "theano/tensor/basic.py", line 212, in as_tensor_variable
    raise AsTensorError("Cannot convert %s to TensorType" % str_x, type(x))
theano.tensor.var.AsTensorError: ('Cannot convert (-1, None, 5, 6) to TensorType', &lt;type 'tuple'&gt;)
</code></pre></div>
<p dir="auto">where as</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="model.add(Reshape((5, 6))) "><pre class="notranslate"><span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Reshape</span>((<span class="pl-c1">5</span>, <span class="pl-c1">6</span>))) </pre></div>
<p dir="auto">instead of</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="model.add(Reshape((-1, 6))) "><pre class="notranslate"><span class="pl-s1">model</span>.<span class="pl-en">add</span>(<span class="pl-v">Reshape</span>((<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">6</span>))) </pre></div>
<p dir="auto">would work as expected.</p>
<p dir="auto">To me it looks like the <a href="https://github.com/fchollet/keras/blob/2a3d4722c21d99d882b2cbc2da451108147fe1c4/keras/layers/recurrent.py#L29">keras backend supports <code class="notranslate">-1</code> dims</a> so the question is why can't users access it from the reshape layer? Is there any way around it? Where should I look to implement this feature?</p>
<h3 dir="auto">Applications</h3>
<p dir="auto">Connecting the output of a 2D CNN and a RNN is difficult because the exact output shape after pooling/downsampling operations need to be known. <a href="https://github.com/fchollet/keras/blob/master/keras/applications/music_tagger_crnn.py#L122">See the the following keras builtin application</a>. If the input dimension would change or even just the pooling stride, the reshape operation would need to be adjusted manually, which could be cumbersome in a network with many layers.</p>