Returns a serialized `GraphDef` representation of this graph. The serialized `GraphDef` can be imported into another `Graph` (using `tf.import_graph_def`) or used with the [C++ Session API](../../../../api_docs/cc/index.md). This method is thread-safe. Args: from_version
(self, from_version=None, add_shapes=False)
| 3135 | graph_def.library.gradient.extend([grad_def]) |
| 3136 | |
| 3137 | def _as_graph_def(self, from_version=None, add_shapes=False): |
| 3138 | # pylint: disable=line-too-long |
| 3139 | """Returns a serialized `GraphDef` representation of this graph. |
| 3140 | |
| 3141 | The serialized `GraphDef` can be imported into another `Graph` |
| 3142 | (using `tf.import_graph_def`) or used with the |
| 3143 | [C++ Session API](../../../../api_docs/cc/index.md). |
| 3144 | |
| 3145 | This method is thread-safe. |
| 3146 | |
| 3147 | Args: |
| 3148 | from_version: Optional. If this is set, returns a `GraphDef` containing |
| 3149 | only the nodes that were added to this graph since its `version` |
| 3150 | property had the given value. |
| 3151 | add_shapes: If true, adds an "_output_shapes" list attr to each node with |
| 3152 | the inferred shapes of each of its outputs. |
| 3153 | |
| 3154 | Returns: |
| 3155 | A tuple containing a |
| 3156 | [`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto) |
| 3157 | protocol buffer, and the version of the graph to which that |
| 3158 | `GraphDef` corresponds. |
| 3159 | |
| 3160 | Raises: |
| 3161 | ValueError: If the `graph_def` would be too large. |
| 3162 | |
| 3163 | """ |
| 3164 | # pylint: enable=line-too-long |
| 3165 | with self._lock: |
| 3166 | with c_api_util.tf_buffer() as buf: |
| 3167 | c_api.TF_GraphToGraphDef(self._c_graph, buf) |
| 3168 | data = c_api.TF_GetBuffer(buf) |
| 3169 | graph = graph_pb2.GraphDef() |
| 3170 | graph.ParseFromString(compat.as_bytes(data)) |
| 3171 | # Strip the experimental library field iff it's empty. |
| 3172 | if not graph.library.function: |
| 3173 | graph.ClearField("library") |
| 3174 | |
| 3175 | if add_shapes: |
| 3176 | for node in graph.node: |
| 3177 | op = self._nodes_by_name[node.name] |
| 3178 | if op.outputs: |
| 3179 | node.attr["_output_shapes"].list.shape.extend( |
| 3180 | [output.get_shape().as_proto() for output in op.outputs]) |
| 3181 | for function_def in graph.library.function: |
| 3182 | defined_function = self._functions[function_def.signature.name] |
| 3183 | try: |
| 3184 | func_graph = defined_function.graph |
| 3185 | except AttributeError: |
| 3186 | # _DefinedFunction doesn't have a graph, _EagerDefinedFunction |
| 3187 | # does. Both rely on ops.py, so we can't really isinstance check |
| 3188 | # them. |
| 3189 | continue |
| 3190 | input_shapes = function_def.attr["_input_shapes"] |
| 3191 | try: |
| 3192 | func_graph_inputs = func_graph.inputs |
| 3193 | except AttributeError: |
| 3194 | continue |
no test coverage detected