Fix depth calculation for shared layers
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
index 7b27a9b45d0..ff05e806731 100644
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -1690,6 +1690,15 @@ def build_map_of_graph(tensor, finished_nodes, nodes_in_progress,
             # If the depth is not set, the node has no outbound nodes (depth 0).
             depth = nodes_depths.setdefault(node, 0)
 
+            # Update the depth of the corresponding layer
+            previous_depth = layers_depths.get(node.outbound_layer, 0)
+            # If we've seen this layer before at a higher depth, we should use that depth instead
+            # of the node depth.  This is necessary for shared layers that have inputs at different
+            # depth levels in the graph.
+            depth = max(depth, previous_depth)
+            layers_depths[node.outbound_layer] = depth
+            nodes_depths[node] = depth
+
             # Update the depth of inbound nodes.
             for i in range(len(node.inbound_layers)):
                 inbound_layer = node.inbound_layers[i]
@@ -1698,10 +1707,6 @@ def build_map_of_graph(tensor, finished_nodes, nodes_in_progress,
                 previous_depth = nodes_depths.get(inbound_node, 0)
                 nodes_depths[inbound_node] = max(depth + 1, previous_depth)
 
-            # Update the depth of the corresponding layer
-            previous_depth = layers_depths.get(node.outbound_layer, 0)
-            layers_depths[node.outbound_layer] = max(depth, previous_depth)
-
         # Build a dict {depth: list of nodes with this depth}
         nodes_by_depth = {}
         for node, depth in nodes_depths.items():
diff --git a/tests/keras/engine/test_topology.py b/tests/keras/engine/test_topology.py
index 38fff21710a..1cc8422e479 100644
--- a/tests/keras/engine/test_topology.py
+++ b/tests/keras/engine/test_topology.py
@@ -577,5 +577,32 @@ def test_recursion_with_bn_and_loss():
     model2.fit(x, y, verbose=0, epochs=1)
 
 
+def test_shared_layer_depth_is_correct():
+    # Basic outline here: we have a shared embedding layer, and two inputs that go through
+    # different depths of computation in the graph before the final output.  We need the computed
+    # depth of the input layers to be the same, because they both pass through the embedding layer
+    # before anything else happens.  That's what we're testing.
+    from keras.layers import Embedding, Input, Dense, Concatenate
+    from keras.models import Model
+    input1 = Input(shape=(10,), name="input1")
+    input2 = Input(shape=(10,), name="input2")
+    embedding_layer = Embedding(name="embedding", input_dim=5, output_dim=10)
+    embedded_input1 = embedding_layer(input1)
+    embedded_input2 = embedding_layer(input2)
+    transformed_input2 = Dense(6)(Dense(5)(Dense(3)(embedded_input2)))
+    final_output = Dense(2)(Concatenate()([embedded_input1, transformed_input2]))
+    model = Model(inputs=[input1, input2], outputs=final_output)
+    input1_depth = -1
+    input2_depth = -1
+    for depth, layers in model.layers_by_depth.items():
+        for layer in layers:
+            if layer.name == 'input1':
+                input1_depth = depth
+            if layer.name == 'input2':
+                input2_depth = depth
+    assert input1_depth != -1
+    assert input1_depth == input2_depth
+
+
 if __name__ == '__main__':
     pytest.main([__file__])

This PR fixes a bug where model serialization and loading is broken when a shared layer is used at several different depths in a model (issue found here: https://github.com/allenai/deep_qa/pull/357).  I first added a test that failed on master, then fixed the test.  Hopefully the test and the comments around the test and the fix are enough to explain what's going on.