<p dir="auto"><strong><em>(See the comments below for a simpler repro case.)</em></strong></p>
<p dir="auto">A model with shared inputs, LSTM, and Lambda layers fails to deserialize. A minimal repro case is here.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from keras.engine import Input, Model
from keras.layers import Lambda, concatenate, Dense, LSTM
from keras.models import save_model, load_model

input_shape = (20, 300)

input_1 = Input(input_shape)
input_2 = Input(input_shape)

lstm = LSTM(32)
e1 = lstm(input_1)
e2 = lstm(input_2)

negative_e1 = Lambda(lambda x: -x)(e1)
hidden = concatenate([e1, e2, negative_e1])

logistic_regression = Dense(1, activation=&quot;sigmoid&quot;)(hidden)
model = Model(inputs=[input_1, input_2], outputs=logistic_regression)
model.compile(optimizer=&quot;adam&quot;, loss=&quot;binary_crossentropy&quot;, metrics=[&quot;accuracy&quot;])
print(&quot;Original model %s&quot; % model)

name = &quot;model.hd5&quot;
save_model(model, name)

restored_model = load_model(name)
print(&quot;Restored model %s&quot; % restored_model)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">keras</span>.<span class="pl-s1">engine</span> <span class="pl-k">import</span> <span class="pl-v">Input</span>, <span class="pl-v">Model</span>
<span class="pl-k">from</span> <span class="pl-s1">keras</span>.<span class="pl-s1">layers</span> <span class="pl-k">import</span> <span class="pl-v">Lambda</span>, <span class="pl-s1">concatenate</span>, <span class="pl-v">Dense</span>, <span class="pl-v">LSTM</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-s1">save_model</span>, <span class="pl-s1">load_model</span>

<span class="pl-s1">input_shape</span> <span class="pl-c1">=</span> (<span class="pl-c1">20</span>, <span class="pl-c1">300</span>)

<span class="pl-s1">input_1</span> <span class="pl-c1">=</span> <span class="pl-v">Input</span>(<span class="pl-s1">input_shape</span>)
<span class="pl-s1">input_2</span> <span class="pl-c1">=</span> <span class="pl-v">Input</span>(<span class="pl-s1">input_shape</span>)

<span class="pl-s1">lstm</span> <span class="pl-c1">=</span> <span class="pl-v">LSTM</span>(<span class="pl-c1">32</span>)
<span class="pl-s1">e1</span> <span class="pl-c1">=</span> <span class="pl-en">lstm</span>(<span class="pl-s1">input_1</span>)
<span class="pl-s1">e2</span> <span class="pl-c1">=</span> <span class="pl-en">lstm</span>(<span class="pl-s1">input_2</span>)

<span class="pl-s1">negative_e1</span> <span class="pl-c1">=</span> <span class="pl-v">Lambda</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-c1">-</span><span class="pl-s1">x</span>)(<span class="pl-s1">e1</span>)
<span class="pl-s1">hidden</span> <span class="pl-c1">=</span> <span class="pl-en">concatenate</span>([<span class="pl-s1">e1</span>, <span class="pl-s1">e2</span>, <span class="pl-s1">negative_e1</span>])

<span class="pl-s1">logistic_regression</span> <span class="pl-c1">=</span> <span class="pl-v">Dense</span>(<span class="pl-c1">1</span>, <span class="pl-s1">activation</span><span class="pl-c1">=</span><span class="pl-s">"sigmoid"</span>)(<span class="pl-s1">hidden</span>)
<span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-v">Model</span>(<span class="pl-s1">inputs</span><span class="pl-c1">=</span>[<span class="pl-s1">input_1</span>, <span class="pl-s1">input_2</span>], <span class="pl-s1">outputs</span><span class="pl-c1">=</span><span class="pl-s1">logistic_regression</span>)
<span class="pl-s1">model</span>.<span class="pl-en">compile</span>(<span class="pl-s1">optimizer</span><span class="pl-c1">=</span><span class="pl-s">"adam"</span>, <span class="pl-s1">loss</span><span class="pl-c1">=</span><span class="pl-s">"binary_crossentropy"</span>, <span class="pl-s1">metrics</span><span class="pl-c1">=</span>[<span class="pl-s">"accuracy"</span>])
<span class="pl-en">print</span>(<span class="pl-s">"Original model %s"</span> <span class="pl-c1">%</span> <span class="pl-s1">model</span>)

<span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-s">"model.hd5"</span>
<span class="pl-en">save_model</span>(<span class="pl-s1">model</span>, <span class="pl-s1">name</span>)

<span class="pl-s1">restored_model</span> <span class="pl-c1">=</span> <span class="pl-en">load_model</span>(<span class="pl-s1">name</span>)
<span class="pl-en">print</span>(<span class="pl-s">"Restored model %s"</span> <span class="pl-c1">%</span> <span class="pl-s1">restored_model</span>)</pre></div>
<p dir="auto">(This model is a simplification of a strategy used for textual semantic equivalence detection. The actual strategy takes the square of <code class="notranslate">e2 - e1</code> which exhibits the same problem.)</p>
<p dir="auto">The <code class="notranslate">restore_model</code> fails with the exception <code class="notranslate">ValueError: Missing layer: input_2</code>.</p>
<p dir="auto">This is as simple as I have been able to make the repro case.<br>
Changing the model in the following ways makes the bug go away:</p>
<ul dir="auto">
<li>Not using multiple inputs</li>
<li>Not putting the inputs through a shared LSTM. (It works if you either omit the LSTM or use a separate LSTM for each input.)</li>
<li>Not using a Lambda layer. (It works if you omit the Lambda layer, or replace it with a non-lambda function, e.g. <code class="notranslate">keras.layers.add([e1, e2])</code>)</li>
</ul>
<p dir="auto">This looks similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="184697280" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/4160" data-hovercard-type="issue" data-hovercard-url="/keras-team/keras/issues/4160/hovercard" href="https://github.com/keras-team/keras/issues/4160">#4160</a>, but doesn't have the nested layers that issue has, so I'm not sure if it's the same bug.</p>
<p dir="auto">This is the full stack trace.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/Users/billmcn/src/keras_serialization_bug/keras_serialization_bug.py&quot;, line 28, in &lt;module&gt;
    restored_model = load_model(name)
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/models.py&quot;, line 232, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/models.py&quot;, line 293, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/layers/__init__.py&quot;, line 46, in deserialize
    printable_module_name='layer')
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/utils/generic_utils.py&quot;, line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/engine/topology.py&quot;, line 2374, in from_config
    process_layer(layer_data)
  File &quot;/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/engine/topology.py&quot;, line 2361, in process_layer
    raise ValueError('Missing layer: ' + inbound_layer_name)
ValueError: Missing layer: input_2"><pre class="notranslate"><span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/src/keras_serialization_bug/keras_serialization_bug.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">28</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
    <span class="pl-s1">restored_model</span> <span class="pl-c1">=</span> <span class="pl-en">load_model</span>(<span class="pl-s1">name</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/models.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">232</span>, <span class="pl-s1">in</span> <span class="pl-s1">load_model</span>
    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-en">model_from_config</span>(<span class="pl-s1">model_config</span>, <span class="pl-s1">custom_objects</span><span class="pl-c1">=</span><span class="pl-s1">custom_objects</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/models.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">293</span>, <span class="pl-s1">in</span> <span class="pl-s1">model_from_config</span>
    <span class="pl-k">return</span> <span class="pl-s1">layer_module</span>.<span class="pl-en">deserialize</span>(<span class="pl-s1">config</span>, <span class="pl-s1">custom_objects</span><span class="pl-c1">=</span><span class="pl-s1">custom_objects</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/layers/__init__.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">46</span>, <span class="pl-s1">in</span> <span class="pl-s1">deserialize</span>
    <span class="pl-s1">printable_module_name</span><span class="pl-c1">=</span><span class="pl-s">'layer'</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/utils/generic_utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">140</span>, <span class="pl-s1">in</span> <span class="pl-s1">deserialize_keras_object</span>
    <span class="pl-en">list</span>(<span class="pl-s1">custom_objects</span>.<span class="pl-en">items</span>())))
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/engine/topology.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">2374</span>, <span class="pl-s1">in</span> <span class="pl-s1">from_config</span>
    <span class="pl-en">process_layer</span>(<span class="pl-s1">layer_data</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/billmcn/anaconda/envs/keras-serialization-bug/lib/python3.5/site-packages/keras/engine/topology.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">2361</span>, <span class="pl-s1">in</span> <span class="pl-s1">process_layer</span>
    <span class="pl-k">raise</span> <span class="pl-v">ValueError</span>(<span class="pl-s">'Missing layer: '</span> <span class="pl-c1">+</span> <span class="pl-s1">inbound_layer_name</span>)
<span class="pl-v">ValueError</span>: <span class="pl-v">Missing</span> <span class="pl-s1">layer</span>: <span class="pl-s1">input_2</span></pre></div>
<p dir="auto"><em>Keras version 2.0.4 with TensorFlow backend, Python 3.5.3</em></p>
<h2 dir="auto">Checklist</h2>
<ul class="contains-task-list">
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Check that you are up-to-date with the master branch of Keras. You can update with:<br>
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found <a href="https://www.tensorflow.org/get_started/os_setup" rel="nofollow">here</a>.</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:<br>
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps</p>
</li>
<li class="task-list-item">
<p dir="auto"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).</p>
</li>
</ul>
<p dir="auto"><code class="notranslate">AttributeError: module 'tensorflow' has no attribute 'get_default_graph'</code></p>
<p dir="auto">I am working through an example of building a simple CNN with tensorflow/keras to classify dogs/cat pictures. I keep getting the above traceback message when executing following line of code (the first layer of my network):<br>
<code class="notranslate">Conv2D(32, (3, 3), activation='relu', input_shape=(224,224,3))</code></p>
<p dir="auto">Writing <code class="notranslate">tensorflow.keras</code> instead of <code class="notranslate">keras</code> doesn't make a difference, neither does importing any of the other modules etc as suggested in previous threads for similar issues.</p>
<p dir="auto">I'm running this all on my macbook on IDLE (for various reasons) - could this problem have something to do with the version of tensorflow I have installed? i.e not <code class="notranslate">tensorflow-GPU==2.0.0-beta1</code></p>
<p dir="auto"><strong>System information</strong></p>
<ul dir="auto">
<li>Mac OSX</li>
<li>TensorFlow backend</li>
<li>TensorFlow version:  2.0.0</li>
<li>Keras version:  2.2.4</li>
<li>Python version:  3.6.8</li>
<li>CUDA/cuDNN version:  n/a</li>
</ul>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/44614292/61803434-04914700-ae2a-11e9-85a9-ee6ad8254be6.png"><img width="941" alt="Screen Shot 2019-07-24 at 15 43 43" src="https://user-images.githubusercontent.com/44614292/61803434-04914700-ae2a-11e9-85a9-ee6ad8254be6.png" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/44614292/61803435-04914700-ae2a-11e9-826d-dce6d3eb3270.png"><img width="666" alt="Screen Shot 2019-07-24 at 15 44 11" src="https://user-images.githubusercontent.com/44614292/61803435-04914700-ae2a-11e9-826d-dce6d3eb3270.png" style="max-width: 100%;"></a></p>