Extract the subgraph that can reach any of the nodes in 'dest_nodes'. Args: graph_def: A graph_pb2.GraphDef proto. dest_nodes: A list of strings specifying the destination node names. Returns: The GraphDef of the sub-graph. Raises: TypeError: If 'graph_def' is not a graph_pb2
(graph_def, dest_nodes)
| 211 | instructions="Use `tf.compat.v1.graph_util.extract_sub_graph`") |
| 212 | @tf_export(v1=["graph_util.extract_sub_graph"]) |
| 213 | def extract_sub_graph(graph_def, dest_nodes): |
| 214 | """Extract the subgraph that can reach any of the nodes in 'dest_nodes'. |
| 215 | |
| 216 | Args: |
| 217 | graph_def: A graph_pb2.GraphDef proto. |
| 218 | dest_nodes: A list of strings specifying the destination node names. |
| 219 | Returns: |
| 220 | The GraphDef of the sub-graph. |
| 221 | |
| 222 | Raises: |
| 223 | TypeError: If 'graph_def' is not a graph_pb2.GraphDef proto. |
| 224 | """ |
| 225 | |
| 226 | if not isinstance(graph_def, graph_pb2.GraphDef): |
| 227 | raise TypeError("graph_def must be a graph_pb2.GraphDef proto.") |
| 228 | |
| 229 | if isinstance(dest_nodes, six.string_types): |
| 230 | raise TypeError("dest_nodes must be a list.") |
| 231 | |
| 232 | name_to_input_name, name_to_node, name_to_seq_num = _extract_graph_summary( |
| 233 | graph_def) |
| 234 | _assert_nodes_are_present(name_to_node, dest_nodes) |
| 235 | |
| 236 | nodes_to_keep = _bfs_for_reachable_nodes(dest_nodes, name_to_input_name) |
| 237 | |
| 238 | nodes_to_keep_list = sorted( |
| 239 | list(nodes_to_keep), key=lambda n: name_to_seq_num[n]) |
| 240 | # Now construct the output GraphDef |
| 241 | out = graph_pb2.GraphDef() |
| 242 | for n in nodes_to_keep_list: |
| 243 | out.node.extend([copy.deepcopy(name_to_node[n])]) |
| 244 | out.library.CopyFrom(graph_def.library) |
| 245 | out.versions.CopyFrom(graph_def.versions) |
| 246 | |
| 247 | return out |
| 248 | |
| 249 | |
| 250 | @deprecation.deprecated( |
no test coverage detected