Writes a Keras model as JSON to as a Summary. Writing the Keras model configuration allows the TensorBoard graph plugin to render a conceptual graph, as opposed to graph of ops. In case the model fails to serialze as JSON, it ignores and returns False. Args: name: A name for this summa
(name, data, step=None)
| 1074 | |
| 1075 | |
| 1076 | def keras_model(name, data, step=None): |
| 1077 | """Writes a Keras model as JSON to as a Summary. |
| 1078 | |
| 1079 | Writing the Keras model configuration allows the TensorBoard graph plugin to |
| 1080 | render a conceptual graph, as opposed to graph of ops. In case the model fails |
| 1081 | to serialze as JSON, it ignores and returns False. |
| 1082 | |
| 1083 | Args: |
| 1084 | name: A name for this summary. The summary tag used for TensorBoard will be |
| 1085 | this name prefixed by any active name scopes. |
| 1086 | data: A Keras Model to write. |
| 1087 | step: Explicit `int64`-castable monotonic step value for this summary. If |
| 1088 | omitted, this defaults to `tf.summary.experimental.get_step()`, which must |
| 1089 | not be None. |
| 1090 | |
| 1091 | Returns: |
| 1092 | True on success, or False if no summary was written because no default |
| 1093 | summary writer was available. |
| 1094 | |
| 1095 | Raises: |
| 1096 | ValueError: if a default writer exists, but no step was provided and |
| 1097 | `tf.summary.experimental.get_step()` is None. |
| 1098 | """ |
| 1099 | summary_metadata = summary_pb2.SummaryMetadata() |
| 1100 | # Hard coding a plugin name. Please refer to go/tb-plugin-name-hardcode for |
| 1101 | # the rationale. |
| 1102 | summary_metadata.plugin_data.plugin_name = "graph_keras_model" |
| 1103 | # version number = 1 |
| 1104 | summary_metadata.plugin_data.content = b"1" |
| 1105 | |
| 1106 | try: |
| 1107 | json_string = data.to_json() |
| 1108 | except Exception as exc: # pylint: disable=broad-except |
| 1109 | # An exception should not break a model code. |
| 1110 | logging.warn("Model failed to serialize as JSON. Ignoring... %s" % exc) |
| 1111 | return False |
| 1112 | |
| 1113 | with summary_scope(name, "graph_keras_model", [data, step]) as (tag, _): |
| 1114 | with ops.device("cpu:0"): |
| 1115 | tensor = constant_op.constant(json_string, dtype=dtypes.string) |
| 1116 | return write( |
| 1117 | tag=tag, |
| 1118 | tensor=tensor, |
| 1119 | step=step, |
| 1120 | metadata=summary_metadata) |
| 1121 | |
| 1122 | |
| 1123 | _TraceContext = collections.namedtuple("TraceContext", ("graph", "profiler")) |
nothing calls this directly
no test coverage detected