def testSummaryDescriptionAndDisplayName(self): with self.test_session() as sess: def get_description(summary_op): summ_str = sess.run(summary_op) summ = summary_pb2.Summary() summ.ParseFromString(summ_str) return summ.value[0].metadata const = constant_op.constant(1) # Default case; no description or display name simple_summary = summary_ops.tensor_summary("simple", const) descr = get_description(simple_summary) self.assertEqual(descr.display_name, "") self.assertEqual(descr.summary_description, "") # Values are provided via function args with_values = summary_ops.tensor_summary( "simple", const, display_name="my name", summary_description="my description") descr = get_description(with_values) self.assertEqual(descr.display_name, "my name") self.assertEqual(descr.summary_description, "my description") # Values are provided via the SummaryMetadata arg metadata = summary_pb2.SummaryMetadata() metadata.display_name = "my name" metadata.summary_description = "my description" with_metadata = summary_ops.tensor_summary( "simple", const, summary_metadata=metadata) descr = get_description(with_metadata) self.assertEqual(descr.display_name, "my name") self.assertEqual(descr.summary_description, "my description") # If both SummmaryMetadata and explicit args are provided, the args win overwrite = summary_ops.tensor_summary( "simple", const, summary_metadata=metadata, display_name="overwritten", summary_description="overwritten") descr = get_description(overwrite) self.assertEqual(descr.display_name, "overwritten") self.assertEqual(descr.summary_description, "overwritten")
def testSummaryDescriptionAndDisplayName(self): with self.test_session() as sess: def get_description(summary_op): summ_str = sess.run(summary_op) summ = summary_pb2.Summary() summ.ParseFromString(summ_str) return summ.value[0].metadata const = constant_op.constant(1) # Default case; no description or display name simple_summary = summary_ops.tensor_summary("simple", const) descr = get_description(simple_summary) self.assertEqual(descr.display_name, "") self.assertEqual(descr.summary_description, "") # Values are provided via function args with_values = summary_ops.tensor_summary( "simple", const, display_name="my name", summary_description="my description") descr = get_description(with_values) self.assertEqual(descr.display_name, "my name") self.assertEqual(descr.summary_description, "my description") # Values are provided via the SummaryMetadata arg metadata = summary_pb2.SummaryMetadata() metadata.display_name = "my name" metadata.summary_description = "my description" with_metadata = summary_ops.tensor_summary( "simple", const, summary_metadata=metadata) descr = get_description(with_metadata) self.assertEqual(descr.display_name, "my name") self.assertEqual(descr.summary_description, "my description") # If both SummaryMetadata and explicit args are provided, the args win overwrite = summary_ops.tensor_summary( "simple", const, summary_metadata=metadata, display_name="overwritten", summary_description="overwritten") descr = get_description(overwrite) self.assertEqual(descr.display_name, "overwritten") self.assertEqual(descr.summary_description, "overwritten")
def text_summary(name, tensor, collections=None): """Summarizes textual data. Text data summarized via this plugin will be visible in the Text Dashboard in TensorBoard. The standard TensorBoard Text Dashboard will render markdown in the strings, and will automatically organize 1d and 2d tensors into tables. If a tensor with more than 2 dimensions is provided, a 2d subarray will be displayed along with a warning message. (Note that this behavior is not intrinsic to the text summary api, but rather to the default TensorBoard text plugin.) Args: name: A name for the generated node. Will also serve as a series name in TensorBoard. tensor: a string-type Tensor to summarize. collections: Optional list of ops.GraphKeys. The collections to add the summary to. Defaults to [_ops.GraphKeys.SUMMARIES] Returns: A TensorSummary op that is configured so that TensorBoard will recognize that it contains textual data. The TensorSummary is a scalar `Tensor` of type `string` which contains `Summary` protobufs. Raises: ValueError: If tensor has the wrong type. """ if tensor.dtype != dtypes.string: raise ValueError("Expected tensor %s to have dtype string, got %s" % (tensor.name, tensor.dtype)) t_summary = tensor_summary(name, tensor, collections=collections) text_assets = plugin_asset.get_plugin_asset(TextSummaryPluginAsset) text_assets.register_tensor(t_summary.op.name) return t_summary
def test_report_unsupported_operations(self): """Tests that unsupported operations are detected.""" context = self.create_test_xla_compile_context() context.Enter() dummy_tensor = constant_op.constant(1.1) audio_summary = summary.audio('audio_summary', dummy_tensor, 0.5) histogram_summary = summary.histogram('histogram_summary', dummy_tensor) image_summary = summary.image('image_summary', dummy_tensor) scalar_summary = summary.scalar('scalar_summary', dummy_tensor) tensor_summary = summary_ops.tensor_summary('tensor_summary', dummy_tensor) summary.merge([ audio_summary, histogram_summary, image_summary, scalar_summary, tensor_summary ], name='merge_summary') logging_ops.Print(dummy_tensor, [dummy_tensor], name='print_op') context.Exit() unsupported_ops_names = [op.name for op in context._unsupported_ops] self.assertEqual(unsupported_ops_names, [ u'audio_summary', u'histogram_summary', u'image_summary', u'scalar_summary', u'tensor_summary', u'merge_summary/merge_summary', u'print_op' ])
def text_summary(name, tensor, collections=None): """Summarizes textual data. Text data summarized via this plugin will be visible in the Text Dashboard in TensorBoard. Args: name: A name for the generated node. Will also serve as a series name in TensorBoard. tensor: a scalar string-type Tensor to summarize. collections: Optional list of ops.GraphKeys. The collections to add the summary to. Defaults to [_ops.GraphKeys.SUMMARIES] Returns: A TensorSummary op that is configured so that TensorBoard will recognize that it contains textual data. The TensorSummary is a scalar `Tensor` of type `string` which contains `Summary` protobufs. Raises: ValueError: If tensor has the wrong shape or type. """ if tensor.dtype != dtypes.string: raise ValueError("Expected tensor %s to have dtype string, got %s" % (tensor.name, tensor.dtype)) if tensor.shape.ndims != 0: raise ValueError("Expected tensor %s to be scalar, has shape %s" % (tensor.name, tensor.shape)) t_summary = tensor_summary(name, tensor, collections=collections) text_assets = plugin_asset.get_plugin_asset(TextSummaryPluginAsset) text_assets.register_tensor(t_summary.op.name) return t_summary
def scalar(name, tensor, summary_description=None, collections=None): """Outputs a `Summary` protocol buffer containing a single scalar value. The generated Summary has a Tensor.proto containing the input Tensor. Args: name: A name for the generated node. Will also serve as the series name in TensorBoard. tensor: A tensor containing a single floating point or integer value. summary_description: Optional summary_description_pb2.SummaryDescription collections: Optional list of graph collections keys. The new summary op is added to these collections. Defaults to `[GraphKeys.SUMMARIES]`. Returns: A scalar `Tensor` of type `string`. Which contains a `Summary` protobuf. Raises: ValueError: If tensor has the wrong shape or type. """ dtype = as_dtype(tensor.dtype) if dtype.is_quantized or not (dtype.is_integer or dtype.is_floating): raise ValueError("Can't create scalar summary for type %s." % dtype) shape = tensor.get_shape() if not shape.is_compatible_with(tensor_shape.scalar()): raise ValueError("Can't create scalar summary for shape %s." % shape) if summary_description is None: summary_description = summary_pb2.SummaryDescription() summary_description.type_hint = "scalar" return tensor_summary(name, tensor, summary_description, collections)
def test_report_unsupported_operations(self): """Tests that unsupported operations are detected.""" context = self.create_test_xla_compile_context() context.Enter() dummy_tensor = constant_op.constant(1.1) audio_summary = summary.audio('audio_summary', dummy_tensor, 0.5) histogram_summary = summary.histogram('histogram_summary', dummy_tensor) image_summary = summary.image('image_summary', dummy_tensor) scalar_summary = summary.scalar('scalar_summary', dummy_tensor) tensor_summary = summary_ops.tensor_summary('tensor_summary', dummy_tensor) summary.merge( [ audio_summary, histogram_summary, image_summary, scalar_summary, tensor_summary ], name='merge_summary') logging_ops.Print(dummy_tensor, [dummy_tensor], name='print_op') context.Exit() unsupported_ops_names = [op.name for op in context._unsupported_ops] self.assertEqual(unsupported_ops_names, [ u'audio_summary', u'histogram_summary', u'image_summary', u'scalar_summary', u'tensor_summary', u'merge_summary/merge_summary', u'print_op' ])
def testManyScalarSummary(self): with self.test_session() as sess: const = array_ops.ones([5, 5, 5]) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) value = self._SummarySingleValue(result) n = tensor_util.MakeNdarray(value.tensor) self._AssertNumpyEq(n, np.ones([5, 5, 5]))
def testScalarSummary(self): with self.test_session() as sess: const = constant_op.constant(10.0) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) value = self._SummarySingleValue(result) n = tensor_util.MakeNdarray(value.tensor) self._AssertNumpyEq(n, 10)
def testManyStringSummary(self): strings = [[six.b("foo bar"), six.b("baz")], [six.b("zoink"), six.b("zod")]] with self.test_session() as sess: const = constant_op.constant(strings) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) value = self._SummarySingleValue(result) n = tensor_util.MakeNdarray(value.tensor) self._AssertNumpyEq(n, strings)
def testStringSummary(self): s = six.b("foobar") with self.test_session() as sess: const = constant_op.constant(s) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) value = self._SummarySingleValue(result) n = tensor_util.MakeNdarray(value.tensor) self._AssertNumpyEq(n, s)
def testManyBools(self): bools = [True, True, True, False, False, False] with self.test_session() as sess: const = constant_op.constant(bools) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) value = self._SummarySingleValue(result) n = tensor_util.MakeNdarray(value.tensor) self._AssertNumpyEq(n, bools)
def testTags(self): with self.test_session() as sess: c = constant_op.constant(1) s1 = summary_ops.tensor_summary("s1", c) with ops.name_scope("foo"): s2 = summary_ops.tensor_summary("s2", c) with ops.name_scope("zod"): s3 = summary_ops.tensor_summary("s3", c) s4 = summary_ops.tensor_summary("TensorSummary", c) summ1, summ2, summ3, summ4 = sess.run([s1, s2, s3, s4]) v1 = self._SummarySingleValue(summ1) self.assertEqual(v1.tag, "s1") v2 = self._SummarySingleValue(summ2) self.assertEqual(v2.tag, "foo/s2") v3 = self._SummarySingleValue(summ3) self.assertEqual(v3.tag, "foo/zod/s3") v4 = self._SummarySingleValue(summ4) self.assertEqual(v4.tag, "foo/zod/TensorSummary")
def scalar(display_name, tensor, description="", labels=None, collections=None, name=None): """Outputs a `Summary` protocol buffer containing a single scalar value. The generated Summary has a Tensor.proto containing the input Tensor. Args: display_name: A name to associate with the data series. Will be used to organize output data and as a name in visualizers. tensor: A tensor containing a single floating point or integer value. description: An optional long description of the data being output. labels: a list of strings used to attach metadata. collections: Optional list of graph collections keys. The new summary op is added to these collections. Defaults to `[GraphKeys.SUMMARIES]`. name: An optional name for the generated node (optional). Returns: A scalar `Tensor` of type `string`. Which contains a `Summary` protobuf. Raises: ValueError: If tensor has the wrong shape or type. """ dtype = as_dtype(tensor.dtype) if dtype.is_quantized or not (dtype.is_integer or dtype.is_floating): raise ValueError("Can't create scalar summary for type %s." % dtype) shape = tensor.get_shape() if not shape.is_compatible_with(tensor_shape.scalar()): raise ValueError("Can't create scalar summary for shape %s." % shape) if labels is None: labels = [] else: labels = labels[:] # Otherwise we would mutate the input argument labels.append(SCALAR_SUMMARY_LABEL) with ops.name_scope(name, "ScalarSummary", [tensor]): tensor = ops.convert_to_tensor(tensor) return tensor_summary(display_name, tensor, description, labels, collections, name)
def text_summary(name, tensor, collections=None): """Summarizes textual data. Text data summarized via this plugin will be visible in the Text Dashboard in TensorBoard. The standard TensorBoard Text Dashboard will render markdown in the strings, and will automatically organize 1d and 2d tensors into tables. If a tensor with more than 2 dimensions is provided, a 2d subarray will be displayed along with a warning message. (Note that this behavior is not intrinsic to the text summary api, but rather to the default TensorBoard text plugin.) Args: name: A name for the generated node. Will also serve as a series name in TensorBoard. tensor: a string-type Tensor to summarize. collections: Optional list of ops.GraphKeys. The collections to add the summary to. Defaults to [_ops.GraphKeys.SUMMARIES] Returns: A TensorSummary op that is configured so that TensorBoard will recognize that it contains textual data. The TensorSummary is a scalar `Tensor` of type `string` which contains `Summary` protobufs. Raises: ValueError: If tensor has the wrong type. """ if tensor.dtype != dtypes.string: raise ValueError("Expected tensor %s to have dtype string, got %s" % (tensor.name, tensor.dtype)) summary_metadata = summary_pb2.SummaryMetadata() text_plugin_data = _TextPluginData() data_dict = text_plugin_data._asdict() # pylint: disable=protected-access summary_metadata.plugin_data.add( plugin_name=PLUGIN_NAME, content=json.dumps(data_dict)) t_summary = tensor_summary( name=name, tensor=tensor, summary_metadata=summary_metadata, collections=collections) return t_summary