Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 def testTensorSummaryOpCreated(self):
   c = tf.constant(0)
   s = tf.summary.scalar('x', c)
   self.assertEqual(s.op.type, 'TensorSummary')
   self.assertEqual(s.op.inputs[0], c)
   description = s.op.get_attr('description')
   summary_description = summary_pb2.SummaryDescription()
   json_format.Parse(description, summary_description)
   self.assertEqual(summary_description.type_hint, 'scalar')
Ejemplo n.º 3
0
def get_summary_description(node_def):
  """Given a TensorSummary node_def, retrieve its SummaryDescription.

  When a Summary op is instantiated, a SummaryDescription of associated
  metadata is stored in its NodeDef. This method retrieves the description.

  Args:
    node_def: the node_def_pb2.NodeDef of a TensorSummary op

  Returns:
    a summary_pb2.SummaryDescription

  Raises:
    ValueError: if the node is not a summary op.
  """

  if node_def.op != 'TensorSummary':
    raise ValueError("Can't get_summary_description on %s" % node_def.op)
  description_str = _compat.as_str_any(node_def.attr['description'].s)
  summary_description = _summary_pb2.SummaryDescription()
  _json_format.Parse(description_str, summary_description)
  return summary_description
Ejemplo n.º 4
0
def tensor_summary(  # pylint: disable=invalid-name
    name,
    tensor,
    summary_description=None,
    collections=None):
  # pylint: disable=line-too-long
  """Outputs a `Summary` protocol buffer with a serialized tensor.proto.

  The generated
  [`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
  has one summary value containing the input tensor.

  Args:
    name: A name for the generated node. Will also serve as the series name in
      TensorBoard.
    tensor: A tensor of any type and shape to serialize.
    summary_description: Optional summary_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`. The serialized `Summary` protocol
    buffer.
  """
  # pylint: enable=line-too-long

  if summary_description is None:
    summary_description = summary_pb2.SummaryDescription()

  description = json_format.MessageToJson(summary_description)
  with ops.name_scope(name, None, [tensor]) as scope:
    val = gen_logging_ops._tensor_summary(
        tensor=tensor,
        description=description,
        name=scope)
    _Collect(val, collections, [ops.GraphKeys.SUMMARIES])
  return val