keras-team/keras · error · ValueError

The name "{name}" is used {all_names.count(name)} times in t

Error message

The name "{name}" is used {all_names.count(name)} times in the model. All operation names should be unique.

What it means

map_graph collects all operation names in the function's graph and enforces uniqueness, because serialized nodes refer to operations by name. If two operations share a name (e.g. two layers both named 'dense'), serialization would be ambiguous and construction fails.

Source

Thrown at keras/src/ops/function.py:384

                if x not in computable_tensors:
                    operation = node.operation
                    raise ValueError(
                        "Graph disconnected: cannot find parent for "
                        f"tensor {x} at operation '{operation}'. "
                        "The following previous operations were accessed "
                        f"without issue: {operations_with_complete_input}"
                    )
                operations_with_complete_input.append(node.operation.name)

            for x in tree.flatten(node.outputs):
                computable_tensors.add(x)

    # Ensure name unicity, which will be crucial for serialization
    # (since serialized nodes refer to operations by their name).
    all_names = [operation.name for operation in operations]
    for name in all_names:
        if all_names.count(name) != 1:
            raise ValueError(
                f'The name "{name}" is used {all_names.count(name)} '
                "times in the model. All operation names should be unique."
            )
    return network_nodes, nodes_by_depth, operations, operations_by_depth


def _build_map(inputs, outputs):
    """Topologically sort nodes in order from inputs to outputs.

    It uses a depth-first search to topologically sort nodes that appear in the
    _keras_history connectivity metadata of `outputs`.

    Args:
        outputs: the output tensors whose _keras_history metadata should be
                walked. This may be an arbitrary nested structure.

    Returns:
        A tuple like (ordered_nodes, operation_to_first_traversal_index)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Find the duplicate name in the message and rename one of the layers: layers.Dense(10, name='dense_a') vs name='dense_b'
  2. If names are generated in a loop, include the loop index in the name string
  3. When merging models, re-instantiate layers fresh instead of reusing the same layer objects with the same names

Example fix

# before
a = layers.Dense(10, name='proj')(x)
b = layers.Dense(10, name='proj')(a)  # duplicate 'proj'

# after
a = layers.Dense(10, name='proj_a')(x)
b = layers.Dense(10, name='proj_b')(a)
Defensive patterns

Strategy: validation

Validate before calling

names = [op.name for op in ops]
dupes = {n for n in names if names.count(n) > 1}
assert not dupes, f'duplicate op names: {dupes}'

Prevention

When it happens

Trigger: Building a keras.ops.Function whose graph contains two operations with the same explicit name — usually two layers instantiated with name='block1' or a name auto-generated identically after manual renaming; also after loading and merging models where names collide.

Common situations: Naming layers programmatically in a loop with a constant name; merging/cloning models; hand-edited saved configs with duplicated layer names; mixing restored weights with re-instantiated layers.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/9347450fc84f7f39. Report an issue: GitHub.