Writes a graph proto to a file. The graph is written as a text proto unless `as_text` is `False`. ```python v = tf.Variable(0, name='my_variable') sess = tf.compat.v1.Session() tf.io.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt') ``` or ```python v = tf.Variable(0,
(graph_or_graph_def, logdir, name, as_text=True)
| 29 | |
| 30 | @tf_export('io.write_graph', v1=['io.write_graph', 'train.write_graph']) |
| 31 | def write_graph(graph_or_graph_def, logdir, name, as_text=True): |
| 32 | """Writes a graph proto to a file. |
| 33 | |
| 34 | The graph is written as a text proto unless `as_text` is `False`. |
| 35 | |
| 36 | ```python |
| 37 | v = tf.Variable(0, name='my_variable') |
| 38 | sess = tf.compat.v1.Session() |
| 39 | tf.io.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt') |
| 40 | ``` |
| 41 | |
| 42 | or |
| 43 | |
| 44 | ```python |
| 45 | v = tf.Variable(0, name='my_variable') |
| 46 | sess = tf.compat.v1.Session() |
| 47 | tf.io.write_graph(sess.graph, '/tmp/my-model', 'train.pbtxt') |
| 48 | ``` |
| 49 | |
| 50 | Args: |
| 51 | graph_or_graph_def: A `Graph` or a `GraphDef` protocol buffer. |
| 52 | logdir: Directory where to write the graph. This can refer to remote |
| 53 | filesystems, such as Google Cloud Storage (GCS). |
| 54 | name: Filename for the graph. |
| 55 | as_text: If `True`, writes the graph as an ASCII proto. |
| 56 | |
| 57 | Returns: |
| 58 | The path of the output proto file. |
| 59 | """ |
| 60 | if isinstance(graph_or_graph_def, ops.Graph): |
| 61 | graph_def = graph_or_graph_def.as_graph_def() |
| 62 | else: |
| 63 | graph_def = graph_or_graph_def |
| 64 | |
| 65 | # gcs does not have the concept of directory at the moment. |
| 66 | if not file_io.file_exists(logdir) and not logdir.startswith('gs:'): |
| 67 | file_io.recursive_create_dir(logdir) |
| 68 | path = os.path.join(logdir, name) |
| 69 | if as_text: |
| 70 | file_io.atomic_write_string_to_file(path, |
| 71 | text_format.MessageToString( |
| 72 | graph_def, float_format='')) |
| 73 | else: |
| 74 | file_io.atomic_write_string_to_file(path, graph_def.SerializeToString()) |
| 75 | return path |