Layers could not be added due to missing dependencies.

Raise code
ocessed_nodes = copy.copy(relevant_nodes)
i = 0
while unprocessed_nodes:
i += 1
# Do a sanity check. This can occur if `Input`s from outside this Model
# are being relied on.
if i > 10000:
raise ValueError('Layers could not be added due to missing '
'dependencies.')
node = unprocessed_nodes.pop(0)
depth = _get_min_depth(node)
if depth is None: # Defer until inbound nodes are processed.
unprocessed_nodes.append(node)
continue
no
Links to the raise (1)
https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/engine/functional.py#L778See also in the other packages (1)
Ways to fix
The process starts when we define our model. The model should be Functional.
import keras
import tensorflow as tf
from keras.engine import input_layer
from keras.engine import training as training_lib
inputs = input_layer.Input((5,5))
legacy_dense_0 = keras.layers.Dense(5, name='dense_0')
legacy_dense_1 = keras.layers.Dense(5, name='dense_1')
layer = legacy_dense_0(inputs)
layer = keras.layers.Dense(1)(layer)
layer = legacy_dense_1(layer)
model = training_lib.Model(inputs=[inputs], outputs=[inputs])
To insert a layer(s) into our model, we are using _insert_layers
relevant_nodes = tf.nest.flatten([layer.inbound_nodes for layer in model.layers]) model._insert_layers([legacy_dense_1],relevant_nodes)
Everything starts after we insert layers. We are getting error within the while loop :
i = 0
while unprocessed_nodes:
i += 1
# Do a sanity check. This can occur if `Input`s from outside this Model
# are being relied on.
if i > 10000:
raise ValueError('Layers could not be added due to missing '
'dependencies.')
node = unprocessed_nodes.pop(0)
depth = _get_min_depth(node)
if depth is None: # Defer until inbound nodes are processed.
unprocessed_nodes.append(node)
continue
node_key = _make_node_key(node.layer.name,
node.layer._inbound_nodes.index(node))
if node_key not in self._network_nodes:
node_to_depth[node] = depth
self._network_nodes.add(node_key)
self._nodes_by_depth[depth].append(node)
Here i is an increment and goes one by one help to process nodes. So, we get an error because that increment will exceed 10000. It exceeds because of depth. When depth is none, the loop continues from the beginning i.e i increase one.
node = unprocessed_nodes.pop(0)
depth = _get_min_depth(node)
if depth is None: # Defer until inbound nodes are processed.
unprocessed_nodes.append(node)
continue
And to get depth, There is a function that returns minimum depth to reach the node.
def _get_min_depth(node):
"""Gets the minimum depth at which node can be computed."""
min_depth = 0
for layer, node_id, _, _ in node.iterate_inbound():
inbound_node = layer._inbound_nodes[node_id]
if inbound_node in node_to_depth:
min_depth = min(min_depth, node_to_depth[inbound_node])
elif inbound_node not in network_nodes:
continue
else:
# Previous relevant nodes haven't been processed yet.
return None
# New node is one shallower than its shallowest input.
return min_depth - 1
To get None from the function, the node should be in network_nodes and not be in node_to_depth. In that case, we are getting Node from function and loop goes infinity and get an error.
We have to choose exactly the correct layer to avoid that error.