Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/plugins/scalar/summary.py: 28%
29 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Scalar summaries and TensorFlow operations to create them.
17A scalar summary stores a single floating-point value, as a rank-0
18tensor.
19"""
22import numpy as np
24from tensorboard.plugins.scalar import metadata
25from tensorboard.plugins.scalar import summary_v2
28# Export V2 versions.
29scalar = summary_v2.scalar
30scalar_pb = summary_v2.scalar_pb
33def op(name, data, display_name=None, description=None, collections=None):
34 """Create a legacy scalar summary op.
36 Arguments:
37 name: A unique name for the generated summary node.
38 data: A real numeric rank-0 `Tensor`. Must have `dtype` castable
39 to `float32`.
40 display_name: Optional name for this summary in TensorBoard, as a
41 constant `str`. Defaults to `name`.
42 description: Optional long-form description for this summary, as a
43 constant `str`. Markdown is supported. Defaults to empty.
44 collections: Optional list of graph collections keys. The new
45 summary op is added to these collections. Defaults to
46 `[Graph Keys.SUMMARIES]`.
48 Returns:
49 A TensorFlow summary op.
50 """
51 # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
52 import tensorflow.compat.v1 as tf
54 if display_name is None:
55 display_name = name
56 summary_metadata = metadata.create_summary_metadata(
57 display_name=display_name, description=description
58 )
59 with tf.name_scope(name):
60 with tf.control_dependencies([tf.assert_scalar(data)]):
61 return tf.summary.tensor_summary(
62 name="scalar_summary",
63 tensor=tf.cast(data, tf.float32),
64 collections=collections,
65 summary_metadata=summary_metadata,
66 )
69def pb(name, data, display_name=None, description=None):
70 """Create a legacy scalar summary protobuf.
72 Arguments:
73 name: A unique name for the generated summary, including any desired
74 name scopes.
75 data: A rank-0 `np.array` or array-like form (so raw `int`s and
76 `float`s are fine, too).
77 display_name: Optional name for this summary in TensorBoard, as a
78 `str`. Defaults to `name`.
79 description: Optional long-form description for this summary, as a
80 `str`. Markdown is supported. Defaults to empty.
82 Returns:
83 A `tf.Summary` protobuf object.
84 """
85 # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
86 import tensorflow.compat.v1 as tf
88 data = np.array(data)
89 if data.shape != ():
90 raise ValueError(
91 "Expected scalar shape for data, saw shape: %s." % data.shape
92 )
93 if data.dtype.kind not in ("b", "i", "u", "f"): # bool, int, uint, float
94 raise ValueError("Cast %s to float is not supported" % data.dtype.name)
95 tensor = tf.make_tensor_proto(data.astype(np.float32))
97 if display_name is None:
98 display_name = name
99 summary_metadata = metadata.create_summary_metadata(
100 display_name=display_name, description=description
101 )
102 tf_summary_metadata = tf.SummaryMetadata.FromString(
103 summary_metadata.SerializeToString()
104 )
105 summary = tf.Summary()
106 summary.value.add(
107 tag="%s/scalar_summary" % name,
108 metadata=tf_summary_metadata,
109 tensor=tensor,
110 )
111 return summary