{"record":{"id":"9347450fc84f7f39","repo":"keras-team/keras","slug":"the-name-name-is-used-all-names-count-name","errorCode":null,"errorMessage":"The name \"{name}\" is used {all_names.count(name)} times in the model. All operation names should be unique.","messagePattern":"The name \"(.+?)\" is used (.+?) times in the model\\. All operation names should be unique\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/function.py","lineNumber":384,"sourceCode":"                if x not in computable_tensors:\n                    operation = node.operation\n                    raise ValueError(\n                        \"Graph disconnected: cannot find parent for \"\n                        f\"tensor {x} at operation '{operation}'. \"\n                        \"The following previous operations were accessed \"\n                        f\"without issue: {operations_with_complete_input}\"\n                    )\n                operations_with_complete_input.append(node.operation.name)\n\n            for x in tree.flatten(node.outputs):\n                computable_tensors.add(x)\n\n    # Ensure name unicity, which will be crucial for serialization\n    # (since serialized nodes refer to operations by their name).\n    all_names = [operation.name for operation in operations]\n    for name in all_names:\n        if all_names.count(name) != 1:\n            raise ValueError(\n                f'The name \"{name}\" is used {all_names.count(name)} '\n                \"times in the model. All operation names should be unique.\"\n            )\n    return network_nodes, nodes_by_depth, operations, operations_by_depth\n\n\ndef _build_map(inputs, outputs):\n    \"\"\"Topologically sort nodes in order from inputs to outputs.\n\n    It uses a depth-first search to topologically sort nodes that appear in the\n    _keras_history connectivity metadata of `outputs`.\n\n    Args:\n        outputs: the output tensors whose _keras_history metadata should be\n                walked. This may be an arbitrary nested structure.\n\n    Returns:\n        A tuple like (ordered_nodes, operation_to_first_traversal_index)","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/function.py#L366-L402","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Find the duplicate name in the message and rename one of the layers: layers.Dense(10, name='dense_a') vs name='dense_b'","If names are generated in a loop, include the loop index in the name string","When merging models, re-instantiate layers fresh instead of reusing the same layer objects with the same names"],"exampleFix":"# before\na = layers.Dense(10, name='proj')(x)\nb = layers.Dense(10, name='proj')(a)  # duplicate 'proj'\n\n# after\na = layers.Dense(10, name='proj_a')(x)\nb = layers.Dense(10, name='proj_b')(a)","handlingStrategy":"validation","validationCode":"names = [op.name for op in ops]\ndupes = {n for n in names if names.count(n) > 1}\nassert not dupes, f'duplicate op names: {dupes}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never hardcode the same layer name twice; include an index in loop-generated names","After merging models, re-scan layer names for duplicates before serialization"],"tags":["keras","naming","serialization","graph"],"backgroundTag":"duplicate-layer-name","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}