votes up 7

Layers could not be added due to missing dependencies.

Package:
keras
github stars 52268
Exception Class:
ValueError

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
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 4 votes down

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.

Jun 26, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix