Writes a generic summary to the default SummaryWriter if one exists. This exists primarily to support the definition of type-specific summary ops like scalar() and image(), and is not intended for direct use unless defining a new type-specific summary op. Args: tag: string tag used to
(tag, tensor, step=None, metadata=None, name=None)
| 588 | |
| 589 | @tf_export("summary.write", v1=[]) |
| 590 | def write(tag, tensor, step=None, metadata=None, name=None): |
| 591 | """Writes a generic summary to the default SummaryWriter if one exists. |
| 592 | |
| 593 | This exists primarily to support the definition of type-specific summary ops |
| 594 | like scalar() and image(), and is not intended for direct use unless defining |
| 595 | a new type-specific summary op. |
| 596 | |
| 597 | Args: |
| 598 | tag: string tag used to identify the summary (e.g. in TensorBoard), usually |
| 599 | generated with `tf.summary.summary_scope` |
| 600 | tensor: the Tensor holding the summary data to write |
| 601 | step: Explicit `int64`-castable monotonic step value for this summary. If |
| 602 | omitted, this defaults to `tf.summary.experimental.get_step()`, which must |
| 603 | not be None. |
| 604 | metadata: Optional SummaryMetadata, as a proto or serialized bytes |
| 605 | name: Optional string name for this op. |
| 606 | |
| 607 | Returns: |
| 608 | True on success, or false if no summary was written because no default |
| 609 | summary writer was available. |
| 610 | |
| 611 | Raises: |
| 612 | ValueError: if a default writer exists, but no step was provided and |
| 613 | `tf.summary.experimental.get_step()` is None. |
| 614 | """ |
| 615 | with ops.name_scope(name, "write_summary") as scope: |
| 616 | if context.context().summary_writer is None: |
| 617 | return constant_op.constant(False) |
| 618 | if step is None: |
| 619 | step = get_step() |
| 620 | if step is None: |
| 621 | raise ValueError("No step set via 'step' argument or " |
| 622 | "tf.summary.experimental.set_step()") |
| 623 | if metadata is None: |
| 624 | serialized_metadata = b"" |
| 625 | elif hasattr(metadata, "SerializeToString"): |
| 626 | serialized_metadata = metadata.SerializeToString() |
| 627 | else: |
| 628 | serialized_metadata = metadata |
| 629 | |
| 630 | def record(): |
| 631 | """Record the actual summary and return True.""" |
| 632 | # Note the identity to move the tensor to the CPU. |
| 633 | with ops.device("cpu:0"): |
| 634 | write_summary_op = gen_summary_ops.write_summary( |
| 635 | context.context().summary_writer._resource, # pylint: disable=protected-access |
| 636 | step, |
| 637 | array_ops.identity(tensor), |
| 638 | tag, |
| 639 | serialized_metadata, |
| 640 | name=scope) |
| 641 | with ops.control_dependencies([write_summary_op]): |
| 642 | return constant_op.constant(True) |
| 643 | |
| 644 | with ops.device("cpu:0"): |
| 645 | op = smart_cond.smart_cond( |
| 646 | _should_record_summaries_v2(), record, _nothing, name="summary_cond") |
| 647 | if not context.executing_eagerly(): |