MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / build_map

Function build_map

tensorflow/python/keras/engine/network.py:1678–1732  ·  view source on GitHub ↗

Builds a map of the graph of layers. This recursively updates the map `layer_indices`, the list `nodes_in_decreasing_depth` and the set `network_nodes`. Arguments: tensor: Some tensor in a graph. finished_nodes: Set of nodes whose subgraphs have been traversed

(tensor,
                finished_nodes,
                nodes_in_progress,
                layer,
                node_index,
                tensor_index)

Source from the content-addressed store, hash-verified

1676 nodes_in_decreasing_depth = []
1677
1678 def build_map(tensor,
1679 finished_nodes,
1680 nodes_in_progress,
1681 layer,
1682 node_index,
1683 tensor_index):
1684 """Builds a map of the graph of layers.
1685
1686 This recursively updates the map `layer_indices`,
1687 the list `nodes_in_decreasing_depth` and the set `network_nodes`.
1688
1689 Arguments:
1690 tensor: Some tensor in a graph.
1691 finished_nodes: Set of nodes whose subgraphs have been traversed
1692 completely. Useful to prevent duplicated work.
1693 nodes_in_progress: Set of nodes that are currently active on the
1694 recursion stack. Useful to detect cycles.
1695 layer: Layer from which `tensor` comes from. If not provided,
1696 will be obtained from `tensor._keras_history`.
1697 node_index: Node index from which `tensor` comes from.
1698 tensor_index: Tensor_index from which `tensor` comes from.
1699
1700 Raises:
1701 ValueError: if a cycle is detected.
1702 """
1703 node = layer._inbound_nodes[node_index] # pylint: disable=protected-access
1704
1705 # Prevent cycles.
1706 if node in nodes_in_progress:
1707 raise ValueError('The tensor ' + str(tensor) + ' at layer "' +
1708 layer.name + '" is part of a cycle.')
1709
1710 # Don't repeat work for shared subgraphs
1711 if node in finished_nodes:
1712 return
1713
1714 node_key = _make_node_key(layer.name, node_index)
1715 # Update network_nodes.
1716 network_nodes.add(node_key)
1717
1718 # Store the traversal order for layer sorting.
1719 if layer not in layer_indices:
1720 layer_indices[layer] = len(layer_indices)
1721
1722 nodes_in_progress.add(node)
1723
1724 # Propagate to all previous tensors connected to this node.
1725 for layer, node_index, tensor_index, tensor in node.iterate_inbound(
1726 include_arguments=True):
1727 build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index,
1728 tensor_index)
1729
1730 finished_nodes.add(node)
1731 nodes_in_progress.remove(node)
1732 nodes_in_decreasing_depth.append(node)
1733
1734 finished_nodes = set()
1735 nodes_in_progress = set()

Callers 1

_map_graph_networkFunction · 0.85

Calls 5

_make_node_keyFunction · 0.85
iterate_inboundMethod · 0.80
addMethod · 0.45
removeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected