def testTensorCountMustMatchPlaceholderCount(self):
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"The template expects 2 tensors, but the inputs only has"
                 r" 1\.\s.*"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format("{} {}", tensor)
             self.evaluate(format_output)
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"The template expects 2 tensors, but the inputs only has"
                 r" 1\.\s.*"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format("{} {}", [tensor])
             self.evaluate(format_output)
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"The template expects 1 tensors, but the inputs only has"
                 r" 2\.\s.*"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format(
                 "{}", (tensor, tensor))
             self.evaluate(format_output)
Esempio n. 2
0
 def testTensorCountMustMatchPlaceholderCount(self):
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"2 placeholder\(s\) in template does not match 1 "
                 r"tensor\(s\) provided as input"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format("{} {}", tensor)
             self.evaluate(format_output)
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"2 placeholder\(s\) in template does not match 1 "
                 r"tensor\(s\) provided as input"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format("{} {}", [tensor])
             self.evaluate(format_output)
     with self.cached_session():
         with self.assertRaisesRegex(
                 ValueError,
                 r"1 placeholder\(s\) in template does not match 2 "
                 r"tensor\(s\) provided as input"):
             tensor = math_ops.range(10)
             format_output = string_ops.string_format(
                 "{}", (tensor, tensor))
             self.evaluate(format_output)
Esempio n. 3
0
  def testFormatOneTensorOneDimVarySummarize(self):
    with self.test_session():
      tensor = math_ops.range(6)
      format_output = string_ops.string_format("{}", tensor, summarize=-1)
      out = self.evaluate(format_output)
      expected = "[0 1 2 3 4 5]"
      self.assertEqual(compat.as_text(out), expected)

    with self.test_session():
      tensor = math_ops.range(6)
      format_output = string_ops.string_format("{}", tensor, summarize=1)
      out = self.evaluate(format_output)
      expected = "[0 ... 5]"
      self.assertEqual(compat.as_text(out), expected)

    with self.test_session():
      tensor = math_ops.range(6)
      format_output = string_ops.string_format("{}", tensor, summarize=2)
      out = self.evaluate(format_output)
      expected = "[0 1 ... 4 5]"
      self.assertEqual(compat.as_text(out), expected)

    with self.test_session():
      tensor = math_ops.range(6)
      format_output = string_ops.string_format("{}", tensor, summarize=10)
      out = self.evaluate(format_output)
      expected = "[0 1 2 3 4 5]"
      self.assertEqual(compat.as_text(out), expected)
Esempio n. 4
0
  def testFormatOneTensorOneDim(self):
    with self.test_session():
      tensor = math_ops.range(10)
      format_output = string_ops.string_format("{}", tensor)
      out = self.evaluate(format_output)
      expected = "[0 1 2 ... 7 8 9]"
      self.assertEqual(compat.as_text(out), expected)

    with self.test_session():
      tensor = math_ops.range(10)
      format_output = string_ops.string_format("{}", [tensor])
      out = self.evaluate(format_output)
      expected = "[0 1 2 ... 7 8 9]"
      self.assertEqual(compat.as_text(out), expected)
Esempio n. 5
0
 def testTensorAndFormatUnicode(self):
     with self.cached_session():
         tensor = constant_op.constant("😊")
         format_output = string_ops.string_format("😊:{}", tensor)
         out = self.evaluate(format_output)
         expected = '😊:"😊"'
         self.assertEqual(compat.as_text(out), expected)
Esempio n. 6
0
 def testFormatOneTensorTwoDimLessThanSummarize(self):
     with self.cached_session():
         tensor = array_ops.reshape(math_ops.range(4), [2, 2])
         format_output = string_ops.string_format("{}", tensor, summarize=3)
         out = self.evaluate(format_output)
         expected = ("[[0 1]\n" " [2 3]]")
         self.assertEqual(compat.as_text(out), expected)
Esempio n. 7
0
 def testFormatOneTensorOneDimFloat(self):
   with self.test_session():
     tensor = constant_op.constant([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
     format_output = string_ops.string_format("{}", tensor)
     out = self.evaluate(format_output)
     expected = "[0 0.1 0.2 ... 0.5 0.6 0.7]"
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 8
0
def string_format(template, inputs, placeholder="{}", summarize=3, name=None):
    """Version of tf.strings.format that handles RaggedTensors."""
    if tensor_util.is_tensor(inputs) or ragged_tensor.is_ragged(inputs):
        inputs = [inputs]

    split_template = template.split(placeholder)
    if len(inputs) != len(split_template) - 1:
        raise ValueError(
            "num placeholders in template and num inputs must match"
            ": {} vs {}".format(len(split_template) - 1, len(inputs)))

    with ops.name_scope(name, "StringFormat", [inputs]):
        output_pieces = [constant_op.constant(split_template[0])]
        for i, input in enumerate(inputs):
            if ragged_tensor.is_ragged(input):
                output_pieces.append(ragged_tensor_to_string(input, summarize))
            else:
                output_pieces.append(
                    string_ops.string_format("{}", [input],
                                             summarize=summarize))
            output_pieces.append(constant_op.constant(split_template[i + 1]))
        if len(output_pieces) == 1:
            return output_pieces[0]
        else:
            return string_ops.reduce_join(output_pieces)
Esempio n. 9
0
 def testFormatOneTensorOneDimAlmostSummarize(self):
   with self.test_session():
     tensor = math_ops.range(5)
     format_output = string_ops.string_format("{}", tensor, summarize=3)
     out = self.evaluate(format_output)
     expected = "[0 1 2 3 4]"
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 10
0
 def testFormatOneTensorTwoDimLessThanSummarize(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(4), [2, 2])
     format_output = string_ops.string_format("{}", tensor, summarize=3)
     out = self.evaluate(format_output)
     expected = ("[[0 1]\n"
                 " [2 3]]")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 11
0
 def testNoDuplicateFormatOpGraphModeAfterExplicitFormat(self):
     tensor = math_ops.range(10)
     formatted_string = string_ops.string_format("{}", tensor)
     print_op = logging_ops.print_v2(formatted_string)
     self.evaluate(print_op)
     graph_ops = ops.get_default_graph().get_operations()
     format_ops = [op for op in graph_ops if op.type == "StringFormat"]
     # Should be only 1 format_op for graph mode.
     self.assertEqual(len(format_ops), 1)
Esempio n. 12
0
 def testFormatOneVariableScalar(self):
   with self.test_session():
     var = variables.Variable(3.34)
     format_output = string_ops.string_format("{}", [var])
     if not context.executing_eagerly():
       variables.global_variables_initializer().run()
     out = self.evaluate(format_output)
     expected = "3.34"
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 13
0
 def testFormatOneVariableOneDim(self):
   with self.test_session():
     var = variables.Variable(math_ops.range(10))
     format_output = string_ops.string_format("{}", [var])
     if not context.executing_eagerly():
       variables.global_variables_initializer().run()
     out = self.evaluate(format_output)
     expected = "[0 1 2 ... 7 8 9]"
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 14
0
 def testFormatSummarizeOne(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(100), [10, 10])
     format_output = string_ops.string_format("tensor summary: {}", tensor,
                                              summarize=1)
     out = self.evaluate(format_output)
     expected = ("tensor summary: [[0 ... 9]\n"
                 " ...\n"
                 " [90 ... 99]]")
     self.assertEqual(compat.as_text(out), expected)
 def testNoDuplicateFormatOpGraphModeAfterExplicitFormat(self):
   with self.cached_session():
     tensor = math_ops.range(10)
     formatted_string = string_ops.string_format("{}", tensor)
     print_op = logging_ops.print_v2(formatted_string)
     self.evaluate(print_op)
     graph_ops = ops.get_default_graph().get_operations()
     format_ops = [op for op in graph_ops if op.type == "StringFormat"]
     # Should be only 1 format_op for graph mode.
     self.assertEqual(len(format_ops), 1)
Esempio n. 16
0
  def call(self, inputs):
    if isinstance(inputs, (list, tuple, np.ndarray)):
      inputs = ops.convert_to_tensor_v2_with_dispatch(inputs)

    if not self.max_tokens and self._vocab_size is None:
      raise ValueError("You must set the layer's vocabulary before calling it. "
                       "Either pass a `vocabulary` argument to the layer, or "
                       "call `layer.adapt(dataset)` with some sample data.")
    self._called = True
    if self._key_dtype == dtypes.int64 and inputs.dtype == dtypes.int32:
      inputs = math_ops.cast(inputs, dtypes.int64)
    lookup_result = self._table_handler.lookup(inputs)

    lookup_checks = []

    if self.num_oov_indices == 0 and not self.invert:
      if tf_utils.is_sparse(inputs):
        lookup_values = lookup_result.values
        input_values = inputs.values
      elif tf_utils.is_ragged(inputs):
        lookup_values = lookup_result.flat_values
        input_values = inputs.flat_values
      else:
        lookup_values = lookup_result
        input_values = inputs
      oov_indices = array_ops.where_v2(math_ops.equal(lookup_values, -1))
      oov_inputs = array_ops.gather_nd(input_values, oov_indices)
      msg = string_ops.string_format(
          "When `num_oov_indices=0` all inputs should be in vocabulary, "
          "found OOV values {}, consider setting `num_oov_indices=1`.",
          (oov_inputs,))
      assertion = control_flow_ops.Assert(
          math_ops.equal(array_ops.size(oov_indices), 0), [msg])
      lookup_checks.append(assertion)

    with ops.control_dependencies(lookup_checks):
      if self.output_mode == INT:
        return array_ops.identity(lookup_result)

      multi_hot_output = (self.output_mode == MULTI_HOT)
      if self._vocab_size and not self.pad_to_max_tokens:
        out_depth = self._vocab_size
      else:
        out_depth = self.max_tokens
      if self.sparse:
        bincounts = category_encoding.sparse_bincount(lookup_result, out_depth,
                                                      multi_hot_output)
      else:
        bincounts = category_encoding.dense_bincount(lookup_result, out_depth,
                                                     multi_hot_output)

      if self.output_mode == TF_IDF:
        return math_ops.multiply(bincounts, self.tf_idf_weights)

      return bincounts
Esempio n. 17
0
 def testFormatOneTensorThreeDim(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(1000), [10, 10, 10])
     format_output = string_ops.string_format("{}", tensor)
     out = self.evaluate(format_output)
     expected = ("[[[0 1 2 ... 7 8 9]\n"
                 "  [10 11 12 ... 17 18 19]\n"
                 "  [20 21 22 ... 27 28 29]\n"
                 "  ...\n"
                 "  [70 71 72 ... 77 78 79]\n"
                 "  [80 81 82 ... 87 88 89]\n"
                 "  [90 91 92 ... 97 98 99]]\n"
                 "\n"
                 " [[100 101 102 ... 107 108 109]\n"
                 "  [110 111 112 ... 117 118 119]\n"
                 "  [120 121 122 ... 127 128 129]\n"
                 "  ...\n  [170 171 172 ... 177 178 179]\n"
                 "  [180 181 182 ... 187 188 189]\n"
                 "  [190 191 192 ... 197 198 199]]\n"
                 "\n"
                 " [[200 201 202 ... 207 208 209]\n"
                 "  [210 211 212 ... 217 218 219]\n"
                 "  [220 221 222 ... 227 228 229]\n"
                 "  ...\n"
                 "  [270 271 272 ... 277 278 279]\n"
                 "  [280 281 282 ... 287 288 289]\n"
                 "  [290 291 292 ... 297 298 299]]\n"
                 "\n"
                 " ...\n"
                 "\n"
                 " [[700 701 702 ... 707 708 709]\n"
                 "  [710 711 712 ... 717 718 719]\n"
                 "  [720 721 722 ... 727 728 729]\n"
                 "  ...\n"
                 "  [770 771 772 ... 777 778 779]\n"
                 "  [780 781 782 ... 787 788 789]\n"
                 "  [790 791 792 ... 797 798 799]]\n"
                 "\n"
                 " [[800 801 802 ... 807 808 809]\n"
                 "  [810 811 812 ... 817 818 819]\n"
                 "  [820 821 822 ... 827 828 829]\n"
                 "  ...\n"
                 "  [870 871 872 ... 877 878 879]\n"
                 "  [880 881 882 ... 887 888 889]\n"
                 "  [890 891 892 ... 897 898 899]]\n"
                 "\n"
                 " [[900 901 902 ... 907 908 909]\n"
                 "  [910 911 912 ... 917 918 919]\n"
                 "  [920 921 922 ... 927 928 929]\n"
                 "  ...\n"
                 "  [970 971 972 ... 977 978 979]\n"
                 "  [980 981 982 ... 987 988 989]\n"
                 "  [990 991 992 ... 997 998 999]]]")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 18
0
 def testFormatOneTensorTwoDimSummarizeTwo(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(100), [10, 10])
     format_output = string_ops.string_format("{}", tensor, summarize=2)
     out = self.evaluate(format_output)
     expected = ("[[0 1 ... 8 9]\n"
                 " [10 11 ... 18 19]\n"
                 " ...\n"
                 " [80 81 ... 88 89]\n"
                 " [90 91 ... 98 99]]")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 19
0
 def testFormatTwoVariablesWithAssignAdd(self):
   with self.test_session():
     var_one = variables.Variable(2.14)
     plus_one = var_one.assign_add(1.0)
     var_two = variables.Variable(math_ops.range(10))
     format_output = string_ops.string_format("{}, {}", [var_one, var_two])
     if not context.executing_eagerly():
       variables.global_variables_initializer().run()
     self.evaluate(plus_one)
     out = self.evaluate(format_output)
     expected = "3.14, [0 1 2 ... 7 8 9]"
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 20
0
 def testTensorCountMustMatchPlaceholderCount(self):
   with self.test_session():
     with self.assertRaisesRegexp(
         ValueError, r"2 placeholder\(s\) in template does not match 1 "
                     r"tensor\(s\) provided as input"):
       tensor = math_ops.range(10)
       format_output = string_ops.string_format("{} {}", tensor)
       self.evaluate(format_output)
   with self.test_session():
     with self.assertRaisesRegexp(
         ValueError, r"2 placeholder\(s\) in template does not match 1 "
                     r"tensor\(s\) provided as input"):
       tensor = math_ops.range(10)
       format_output = string_ops.string_format("{} {}", [tensor])
       self.evaluate(format_output)
   with self.test_session():
     with self.assertRaisesRegexp(
         ValueError, r"1 placeholder\(s\) in template does not match 2 "
                     r"tensor\(s\) provided as input"):
       tensor = math_ops.range(10)
       format_output = string_ops.string_format("{}", (tensor, tensor))
       self.evaluate(format_output)
Esempio n. 21
0
 def testFormatOneTensorTemplateSuffix(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(100), [10, 10])
     format_output = string_ops.string_format("{}, suffix", tensor)
     out = self.evaluate(format_output)
     expected = ("[[0 1 2 ... 7 8 9]\n"
                 " [10 11 12 ... 17 18 19]\n"
                 " [20 21 22 ... 27 28 29]\n"
                 " ...\n"
                 " [70 71 72 ... 77 78 79]\n"
                 " [80 81 82 ... 87 88 89]\n"
                 " [90 91 92 ... 97 98 99]], suffix")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 22
0
 def testFormatPlaceholder(self):
   with self.test_session():
     tensor = array_ops.reshape(math_ops.range(100), [10, 10])
     format_output = string_ops.string_format("tensor summary: %t%", tensor,
                                              placeholder="%t%")
     out = self.evaluate(format_output)
     expected = ("tensor summary: [[0 1 2 ... 7 8 9]\n"
                 " [10 11 12 ... 17 18 19]\n"
                 " [20 21 22 ... 27 28 29]\n"
                 " ...\n"
                 " [70 71 72 ... 77 78 79]\n"
                 " [80 81 82 ... 87 88 89]\n"
                 " [90 91 92 ... 97 98 99]]")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 23
0
  def testValidPipelineWithRangeDataset(self, shuffle):
    dataset = dataset_ops.Dataset.range(self._num_files)
    dataset = dataset.map(lambda n: string_ops.string_join(  # pylint:disable=g-long-lambda
        [self.get_temp_dir(),
         string_ops.string_format("/tf_record.{}.txt", [n])]))
    dataset = dataset.apply(
        interleave_ops.parallel_interleave(core_readers.TFRecordDataset, 10))
    dataset = dataset.map(lambda x: string_ops.substr_v2(x, 2, 1000))
    dataset = dataset.batch(5)
    dataset = distribute._AutoShardDataset(dataset, 5, 3)

    expected = [
        b"cord %d of file %d" % (r, f)  # pylint:disable=g-complex-comprehension
        for r in range(0, 10)
        for f in (3, 8)
    ]
    self.assertDatasetProducesWithShuffle(dataset, expected, 5, 4, shuffle)
  def testValidPipelineWithRangeDataset(self, shuffle):
    dataset = dataset_ops.Dataset.range(self._num_files)
    dataset = dataset.map(lambda n: string_ops.string_join(  # pylint:disable=g-long-lambda
        [self.get_temp_dir(),
         string_ops.string_format("/tf_record.{}.txt", [n])]))
    dataset = dataset.apply(
        interleave_ops.parallel_interleave(core_readers.TFRecordDataset, 10))
    dataset = dataset.map(lambda x: string_ops.substr_v2(x, 2, 1000))
    dataset = dataset.batch(5)
    dataset = distribute._AutoShardDataset(dataset, 5, 3)

    expected = [
        b"cord %d of file %d" % (r, f)  # pylint:disable=g-complex-comprehension
        for r in range(0, 10)
        for f in (3, 8)
    ]
    self.assertDatasetProducesWithShuffle(dataset, expected, 5, 4, shuffle)
Esempio n. 25
0
 def testFormatMultiTensor(self):
   with self.test_session():
     tensor_one = array_ops.reshape(math_ops.range(100), [10, 10])
     tensor_two = tensor_one * 10
     format_output = string_ops.string_format("One: {},\nTwo: {}",
                                              (tensor_one, tensor_two))
     out = self.evaluate(format_output)
     expected = ("One: [[0 1 2 ... 7 8 9]\n"
                 " [10 11 12 ... 17 18 19]\n"
                 " [20 21 22 ... 27 28 29]\n"
                 " ...\n"
                 " [70 71 72 ... 77 78 79]\n"
                 " [80 81 82 ... 87 88 89]\n"
                 " [90 91 92 ... 97 98 99]],\n"
                 "Two: [[0 10 20 ... 70 80 90]\n"
                 " [100 110 120 ... 170 180 190]\n"
                 " [200 210 220 ... 270 280 290]\n"
                 " ...\n"
                 " [700 710 720 ... 770 780 790]\n"
                 " [800 810 820 ... 870 880 890]\n"
                 " [900 910 920 ... 970 980 990]]")
     self.assertEqual(compat.as_text(out), expected)
Esempio n. 26
0
def computation_with_string_ops(x):
    output = string_ops.string_format("1{}", x)
    return string_ops.string_to_number(output)
Esempio n. 27
0
def print_v2(*inputs, **kwargs):
  """Print the specified inputs.

  Returns an operator that prints the specified inputs to a desired
  output stream or logging level. The inputs may be dense or sparse Tensors,
  primitive python objects, data structures that contain Tensors, and printable
  python objects. Printed tensors will recursively show the first and last
  `summarize` elements of each dimension.

  With eager execution enabled and/or inside a `tf.contrib.eager.defun` this
  operator will automatically execute, and users only need to call `tf.print`
  without using the return value. When constructing graphs outside of a
  `tf.contrib.eager.defun`, one must either include the returned op
  in the input to `session.run`, or use the operator as a control dependency for
  executed ops by specifying `with tf.control_dependencies([print_op])`.

  @compatibility(python2)
  In python 2.7, make sure to import the following:
  `from __future__ import print_function`
  @end_compatibility

  Example:
    Single-input usage:
    ```python
    tf.enable_eager_execution()
    tensor = tf.range(10)
    tf.print(tensor, output_stream=sys.stderr)
    ```
    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

    Multi-input usage:
    ```python
    tf.enable_eager_execution()
    tensor = tf.range(10)
    tf.print("tensors:", tensor, {2: tensor * 2}, output_stream=sys.stdout)
    ```
    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

    Usage in a defun:
    ```python
    tf.enable_eager_execution()

    @tf.contrib.eager.defun
    def f():
        tensor = tf.range(10)
        tf.print(tensor, output_stream=sys.stderr)
        return tensor

    range_tensor = f()
    ```
    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

    Usage when constructing graphs:
    ```python
    sess = tf.Session()
    with sess.as_default():
        tensor = tf.range(10)
        print_op = tf.print("tensors:", tensor, {2: tensor * 2},
                            output_stream=sys.stdout)
        with tf.control_dependencies([print_op]):
          tripled_tensor = tensor * 3
        sess.run(tripled_tensor)
    ```
    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

  Note: This op is only partially compatible with Jupyter notebooks and colabs.
    Because it prints to the C++ standard out / standard error, this will go
    in the notebook kernel's console output, not in the notebook cell output.

  Args:
    *inputs: Positional arguments that are the inputs to print. Inputs in the
      printed output will be separated by spaces. Inputs may be python
      primitives, tensors, data structures such as dicts and lists that
      may contain tensors (with the data structures possibly nested in
      arbitrary ways), and printable python objects.
    output_stream: The output stream or logging level to print to. Defaults to
      sys.stderr, but sys.stdout, tf.logging.info, tf.logging.warning, and
      tf.logging.error are also supported.
    summarize: The first and last `summarize` elements within each dimension are
      recursively printed per Tensor. If None, then the first 3 and last 3
      elements of each dimension are printed for each tensor. If set to -1, it
      will print all elements of every tensor.
    name: A name for the operation (optional).

  Returns:
    A print operator that prints the specified inputs in the specified output
    stream or logging level.

  Raises:
    ValueError: If an unsupported output stream is specified.
  """
  # Because we are using arbitrary-length positional arguments, python 2
  # does not support explicitly specifying the keyword arguments in the
  # function definition. So, we manually get the keyword arguments w/ default
  # values here.
  output_stream = kwargs.pop("output_stream", sys.stderr)
  name = kwargs.pop("name", None)
  summarize = kwargs.pop("summarize", 3)
  if kwargs:
    raise ValueError("Unrecognized keyword arguments for tf.print: %s" % kwargs)
  format_name = None
  if name:
    format_name = name + "_format"

  # Match the C++ string constants representing the different output streams.
  # Keep this updated!
  output_stream_to_constant = {
      sys.stdout: "stdout",
      sys.stderr: "stderr",
      tf_logging.INFO: "log(info)",
      tf_logging.info: "log(info)",
      tf_logging.WARN: "log(warning)",
      tf_logging.warning: "log(warning)",
      tf_logging.warn: "log(warning)",
      tf_logging.ERROR: "log(error)",
      tf_logging.error: "log(error)",
  }

  output_stream_string = output_stream_to_constant.get(output_stream)
  if not output_stream_string:
    raise ValueError(
        "Unsupported output stream or logging level " +
        str(output_stream) + ". Supported streams are sys.stdout, "
                             "sys.stderr, tf.logging.info, "
                             "tf.logging.warning, tf.logging.error")

  # If we are only printing a single string scalar, there is no need to format
  if (len(inputs) == 1 and tensor_util.is_tensor(inputs[0])
      and (not isinstance(inputs[0], sparse_tensor.SparseTensor))
      and inputs[0].shape and (inputs[0].dtype == dtypes.string)):
    formatted_string = inputs[0]
  # Otherwise, we construct an appropriate template for the tensors we are
  # printing, and format the template using those tensors.
  else:
    # For each input to this print function, we extract any nested tensors,
    # and construct an appropriate template to format representing the
    # printed input.
    templates = []
    tensors = []
    tensor_free_structure = nest.map_structure(
        lambda x: "" if tensor_util.is_tensor(x) else x,
        inputs)
    tensor_free_template = " ".join(pprint.pformat(x)
                                    for x in tensor_free_structure)
    placeholder = _generate_placeholder_string(tensor_free_template)

    for input_ in inputs:
      placeholders = []
      # Use the nest utilities to flatten & process any nested elements in this
      # input. The placeholder for a tensor in the template should be the
      # placeholder string, and the placeholder for a non-tensor can just be
      # the printed value of the non-tensor itself.
      for x in nest.flatten(input_):
        # support sparse tensors
        if isinstance(x, sparse_tensor.SparseTensor):
          tensors.extend([x.indices, x.values, x.dense_shape])
          placeholders.append(
              "SparseTensor(indices={}, values={}, shape={})".format(
                  placeholder, placeholder, placeholder)
          )
        elif tensor_util.is_tensor(x):
          tensors.append(x)
          placeholders.append(placeholder)
        else:
          placeholders.append(x)

      if isinstance(input_, six.string_types):
        # If the current input to format/print is a normal string, that string
        # can act as the template.
        cur_template = input_
      else:
        # We pack the placeholders into a data structure that matches the
        # input data structure format, then format that data structure
        # into a string template.
        #
        # NOTE: We must use pprint.pformat here for building the template for
        # unordered data structures such as `dict`, because `str` doesn't
        # guarantee orderings, while pprint prints in sorted order. pprint
        # will match the ordering of `nest.flatten`.
        # This even works when nest.flatten reorders OrderedDicts, because
        # pprint is printing *after* the OrderedDicts have been reordered.
        cur_template = pprint.pformat(
            nest.pack_sequence_as(input_, placeholders))
      templates.append(cur_template)

    # We join the templates for the various inputs into a single larger
    # template. We also remove all quotes surrounding the placeholders, so that
    # the formatted/printed output will not contain quotes around tensors.
    # (example of where these quotes might appear: if we have added a
    # placeholder string into a list, then pretty-formatted that list)
    template = " ".join(templates)
    template = template.replace("'" + placeholder + "'", placeholder)
    formatted_string = string_ops.string_format(
        inputs=tensors, template=template, placeholder=placeholder,
        summarize=summarize,
        name=format_name)

  return gen_logging_ops.print_v2(formatted_string,
                                  output_stream=output_stream_string,
                                  name=name)
Esempio n. 28
0
def print_v2(*inputs, **kwargs):
  """Print the specified inputs.

  Returns an operator that prints the specified inputs to a desired
  output stream or logging level. The inputs may be dense or sparse Tensors,
  primitive python objects, data structures that contain Tensors, and printable
  python objects. Printed tensors will recursively show the first and last
  `summarize` elements of each dimension.

  With eager execution enabled and/or inside a `tf.contrib.eager.defun` this
  operator will automatically execute, and users only need to call `tf.print`
  without using the return value. When constructing graphs outside of a
  `tf.contrib.eager.defun`, one must either include the returned op
  in the input to `session.run`, or use the operator as a control dependency for
  executed ops by specifying `with tf.control_dependencies([print_op])`.

  @compatibility(python2)
  In python 2.7, make sure to import the following:
  `from __future__ import print_function`
  @end_compatibility

  Example:
    Single-input usage:
    ```python
    tf.enable_eager_execution()
    tensor = tf.range(10)
    tf.print(tensor, output_stream=sys.stderr)
    ```
    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

    Multi-input usage:
    ```python
    tf.enable_eager_execution()
    tensor = tf.range(10)
    tf.print("tensors:", tensor, {2: tensor * 2}, output_stream=sys.stdout)
    ```
    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

    Usage in a defun:
    ```python
    tf.enable_eager_execution()

    @tf.contrib.eager.defun
    def f():
        tensor = tf.range(10)
        tf.print(tensor, output_stream=sys.stderr)
        return tensor

    range_tensor = f()
    ```
    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

    Usage when constructing graphs:
    ```python
    sess = tf.Session()
    with sess.as_default():
        tensor = tf.range(10)
        print_op = tf.print("tensors:", tensor, {2: tensor * 2},
                            output_stream=sys.stdout)
        with tf.control_dependencies([print_op]):
          tripled_tensor = tensor * 3
        sess.run(tripled_tensor)
    ```
    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

  Note: This op is only partially compatible with Jupyter notebooks and colabs.
    Because it prints to the C++ standard out / standard error, this will go
    in the notebook kernel's console output, not in the notebook cell output.

  Args:
    *inputs: Positional arguments that are the inputs to print. Inputs in the
      printed output will be separated by spaces. Inputs may be python
      primitives, tensors, data structures such as dicts and lists that
      may contain tensors (with the data structures possibly nested in
      arbitrary ways), and printable python objects.
    output_stream: The output stream, logging level, or file to print to.
      Defaults to sys.stderr, but sys.stdout, tf.logging.info,
      tf.logging.warning, and tf.logging.error are also supported. To print to
      a file, pass a string started with "file://" followed by the file path,
      e.g., "file:///tmp/foo.out".
    summarize: The first and last `summarize` elements within each dimension are
      recursively printed per Tensor. If None, then the first 3 and last 3
      elements of each dimension are printed for each tensor. If set to -1, it
      will print all elements of every tensor.
    name: A name for the operation (optional).

  Returns:
    A print operator that prints the specified inputs in the specified output
    stream or logging level.

  Raises:
    ValueError: If an unsupported output stream is specified.
  """
  # Because we are using arbitrary-length positional arguments, python 2
  # does not support explicitly specifying the keyword arguments in the
  # function definition. So, we manually get the keyword arguments w/ default
  # values here.
  output_stream = kwargs.pop("output_stream", sys.stderr)
  name = kwargs.pop("name", None)
  summarize = kwargs.pop("summarize", 3)
  if kwargs:
    raise ValueError("Unrecognized keyword arguments for tf.print: %s" % kwargs)
  format_name = None
  if name:
    format_name = name + "_format"

  # Match the C++ string constants representing the different output streams.
  # Keep this updated!
  output_stream_to_constant = {
      sys.stdout: "stdout",
      sys.stderr: "stderr",
      tf_logging.INFO: "log(info)",
      tf_logging.info: "log(info)",
      tf_logging.WARN: "log(warning)",
      tf_logging.warning: "log(warning)",
      tf_logging.warn: "log(warning)",
      tf_logging.ERROR: "log(error)",
      tf_logging.error: "log(error)",
  }

  if _is_filepath(output_stream):
    output_stream_string = output_stream
  else:
    output_stream_string = output_stream_to_constant.get(output_stream)
    if not output_stream_string:
      raise ValueError(
          "Unsupported output stream, logging level, or file." +
          str(output_stream) + ". Supported streams are sys.stdout, "
          "sys.stderr, tf.logging.info, "
          "tf.logging.warning, tf.logging.error. " +
          "File needs to be in the form of 'file://<filepath>'.")

  # If we are only printing a single string scalar, there is no need to format
  if (len(inputs) == 1 and tensor_util.is_tensor(inputs[0])
      and (not isinstance(inputs[0], sparse_tensor.SparseTensor))
      and inputs[0].shape and (inputs[0].dtype == dtypes.string)):
    formatted_string = inputs[0]
  # Otherwise, we construct an appropriate template for the tensors we are
  # printing, and format the template using those tensors.
  else:
    # For each input to this print function, we extract any nested tensors,
    # and construct an appropriate template to format representing the
    # printed input.
    templates = []
    tensors = []
    tensor_free_structure = nest.map_structure(
        lambda x: "" if tensor_util.is_tensor(x) else x,
        inputs)
    tensor_free_template = " ".join(pprint.pformat(x)
                                    for x in tensor_free_structure)
    placeholder = _generate_placeholder_string(tensor_free_template)

    for input_ in inputs:
      placeholders = []
      # Use the nest utilities to flatten & process any nested elements in this
      # input. The placeholder for a tensor in the template should be the
      # placeholder string, and the placeholder for a non-tensor can just be
      # the printed value of the non-tensor itself.
      for x in nest.flatten(input_):
        # support sparse tensors
        if isinstance(x, sparse_tensor.SparseTensor):
          tensors.extend([x.indices, x.values, x.dense_shape])
          placeholders.append(
              "SparseTensor(indices={}, values={}, shape={})".format(
                  placeholder, placeholder, placeholder)
          )
        elif tensor_util.is_tensor(x):
          tensors.append(x)
          placeholders.append(placeholder)
        else:
          placeholders.append(x)

      if isinstance(input_, six.string_types):
        # If the current input to format/print is a normal string, that string
        # can act as the template.
        cur_template = input_
      else:
        # We pack the placeholders into a data structure that matches the
        # input data structure format, then format that data structure
        # into a string template.
        #
        # NOTE: We must use pprint.pformat here for building the template for
        # unordered data structures such as `dict`, because `str` doesn't
        # guarantee orderings, while pprint prints in sorted order. pprint
        # will match the ordering of `nest.flatten`.
        # This even works when nest.flatten reorders OrderedDicts, because
        # pprint is printing *after* the OrderedDicts have been reordered.
        cur_template = pprint.pformat(
            nest.pack_sequence_as(input_, placeholders))
      templates.append(cur_template)

    # We join the templates for the various inputs into a single larger
    # template. We also remove all quotes surrounding the placeholders, so that
    # the formatted/printed output will not contain quotes around tensors.
    # (example of where these quotes might appear: if we have added a
    # placeholder string into a list, then pretty-formatted that list)
    template = " ".join(templates)
    template = template.replace("'" + placeholder + "'", placeholder)
    formatted_string = string_ops.string_format(
        inputs=tensors, template=template, placeholder=placeholder,
        summarize=summarize,
        name=format_name)

  return gen_logging_ops.print_v2(formatted_string,
                                  output_stream=output_stream_string,
                                  name=name)
Esempio n. 29
0
 def fn(x):
     return string_ops.string_length(
         string_ops.string_format('{}', x))  # COMMENT4
Esempio n. 30
0
 def f1(self, x):
     return string_ops.string_length(
         string_ops.string_format('{}', x))
Esempio n. 31
0
 def testFormatNoTensor(self):
   with self.test_session():
     format_output = string_ops.string_format("No tensor.", ())
     out = self.evaluate(format_output)
     expected = "No tensor."
     self.assertEqual(compat.as_text(out), expected)
def print_v2(*inputs, **kwargs):
    """Print the specified inputs.

  A TensorFlow operator that prints the specified inputs to a desired
  output stream or logging level. The inputs may be dense or sparse Tensors,
  primitive python objects, data structures that contain tensors, and printable
  Python objects. Printed tensors will recursively show the first and last
  elements of each dimension to summarize.

  @compatibility(python2)
  In python 2.7, make sure to import the following:
  `from __future__ import print_function`
  @end_compatibility

  Example:
    Single-input usage:

    ```python
    tensor = tf.range(10)
    tf.print(tensor, output_stream=sys.stderr)
    ```

    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

    Multi-input usage:

    ```python
    tensor = tf.range(10)
    tf.print("tensors:", tensor, {2: tensor * 2}, output_stream=sys.stdout)
    ```

    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

    Changing the input separator:
    ```python
    tensor_a = tf.range(2)
    tensor_b = tensor_a * 2
    tf.print(tensor_a, tensor_b, output_stream=sys.stderr, sep=',')
    ```

    (This prints "[0 1],[0 2]" to sys.stderr)

    Usage in a `tf.function`:

    ```python
    @tf.function
    def f():
        tensor = tf.range(10)
        tf.print(tensor, output_stream=sys.stderr)
        return tensor

    range_tensor = f()
    ```

    (This prints "[0 1 2 ... 7 8 9]" to sys.stderr)

  @compatibility(TF 1.x Graphs and Sessions)
  In graphs manually created outside of `tf.function`, this method returns
  the created TF operator that prints the data. To make sure the
  operator runs, users need to pass the produced op to
  `tf.compat.v1.Session`'s run method, or to use the op as a control
  dependency for executed ops by specifying
  `with tf.compat.v1.control_dependencies([print_op])`.
  @end_compatibility

    Compatibility usage in TF 1.x graphs:

    ```python
    sess = tf.compat.v1.Session()
    with sess.as_default():
        tensor = tf.range(10)
        print_op = tf.print("tensors:", tensor, {2: tensor * 2},
                            output_stream=sys.stdout)
        with tf.control_dependencies([print_op]):
          tripled_tensor = tensor * 3
        sess.run(tripled_tensor)
    ```

    (This prints "tensors: [0 1 2 ... 7 8 9] {2: [0 2 4 ... 14 16 18]}" to
    sys.stdout)

  Note: In Jupyter notebooks and colabs, `tf.print` prints to the notebook
    cell outputs. It will not write to the notebook kernel's console logs.

  Args:
    *inputs: Positional arguments that are the inputs to print. Inputs in the
      printed output will be separated by spaces. Inputs may be python
      primitives, tensors, data structures such as dicts and lists that may
      contain tensors (with the data structures possibly nested in arbitrary
      ways), and printable python objects.
    output_stream: The output stream, logging level, or file to print to.
      Defaults to sys.stderr, but sys.stdout, tf.compat.v1.logging.info,
      tf.compat.v1.logging.warning, tf.compat.v1.logging.error,
      absl.logging.info, absl.logging.warning and absl.loogging,error are also
      supported. To print to a file, pass a string started with "file://"
      followed by the file path, e.g., "file:///tmp/foo.out".
    summarize: The first and last `summarize` elements within each dimension are
      recursively printed per Tensor. If None, then the first 3 and last 3
      elements of each dimension are printed for each tensor. If set to -1, it
      will print all elements of every tensor.
    sep: The string to use to separate the inputs. Defaults to " ".
    end: End character that is appended at the end the printed string.
      Defaults to the newline character.
    name: A name for the operation (optional).

  Returns:
    None when executing eagerly. During graph tracing this returns
    a TF operator that prints the specified inputs in the specified output
    stream or logging level. This operator will be automatically executed
    except inside of `tf.compat.v1` graphs and sessions.

  Raises:
    ValueError: If an unsupported output stream is specified.
  """
    # Because we are using arbitrary-length positional arguments, python 2
    # does not support explicitly specifying the keyword arguments in the
    # function definition. So, we manually get the keyword arguments w/ default
    # values here.
    output_stream = kwargs.pop("output_stream", sys.stderr)
    name = kwargs.pop("name", None)
    summarize = kwargs.pop("summarize", 3)
    sep = kwargs.pop("sep", " ")
    end = kwargs.pop("end", os.linesep)
    if kwargs:
        raise ValueError("Unrecognized keyword arguments for tf.print: %s" %
                         kwargs)
    format_name = None
    if name:
        format_name = name + "_format"

    # Match the C++ string constants representing the different output streams.
    # Keep this updated!
    output_stream_to_constant = {
        sys.stdout: "stdout",
        sys.stderr: "stderr",
        tf_logging.INFO: "log(info)",
        tf_logging.info: "log(info)",
        tf_logging.WARN: "log(warning)",
        tf_logging.warning: "log(warning)",
        tf_logging.warn: "log(warning)",
        tf_logging.ERROR: "log(error)",
        tf_logging.error: "log(error)",
        logging.INFO: "log(info)",
        logging.info: "log(info)",
        logging.INFO: "log(info)",
        logging.WARNING: "log(warning)",
        logging.WARN: "log(warning)",
        logging.warning: "log(warning)",
        logging.warn: "log(warning)",
        logging.ERROR: "log(error)",
        logging.error: "log(error)",
    }

    if _is_filepath(output_stream):
        output_stream_string = output_stream
    else:
        output_stream_string = output_stream_to_constant.get(output_stream)
        if not output_stream_string:
            raise ValueError(
                "Unsupported output stream, logging level, or file." +
                str(output_stream) + ". Supported streams are sys.stdout, "
                "sys.stderr, tf.logging.info, "
                "tf.logging.warning, tf.logging.error. " +
                "File needs to be in the form of 'file://<filepath>'.")

    # If we are only printing a single string scalar, there is no need to format
    if (len(inputs) == 1 and tensor_util.is_tensor(inputs[0])
            and (not isinstance(inputs[0], sparse_tensor.SparseTensor))
            and (inputs[0].shape.ndims == 0)
            and (inputs[0].dtype == dtypes.string)):
        formatted_string = inputs[0]
    # Otherwise, we construct an appropriate template for the tensors we are
    # printing, and format the template using those tensors.
    else:
        # For each input to this print function, we extract any nested tensors,
        # and construct an appropriate template to format representing the
        # printed input.
        templates = []
        tensors = []
        tensor_free_structure = nest.map_structure(
            lambda x: "" if tensor_util.is_tensor(x) else x, inputs)
        tensor_free_template = " ".join(
            pprint.pformat(x) for x in tensor_free_structure)
        placeholder = _generate_placeholder_string(tensor_free_template)

        for input_ in inputs:
            placeholders = []
            # Use the nest utilities to flatten & process any nested elements in this
            # input. The placeholder for a tensor in the template should be the
            # placeholder string, and the placeholder for a non-tensor can just be
            # the printed value of the non-tensor itself.
            for x in nest.flatten(input_):
                # support sparse tensors
                if isinstance(x, sparse_tensor.SparseTensor):
                    tensors.extend([x.indices, x.values, x.dense_shape])
                    placeholders.append(
                        "SparseTensor(indices={}, values={}, shape={})".format(
                            placeholder, placeholder, placeholder))
                elif tensor_util.is_tensor(x):
                    tensors.append(x)
                    placeholders.append(placeholder)
                else:
                    placeholders.append(x)

            if isinstance(input_, six.string_types):
                # If the current input to format/print is a normal string, that string
                # can act as the template.
                cur_template = input_
            else:
                # We pack the placeholders into a data structure that matches the
                # input data structure format, then format that data structure
                # into a string template.
                #
                # NOTE: We must use pprint.pformat here for building the template for
                # unordered data structures such as `dict`, because `str` doesn't
                # guarantee orderings, while pprint prints in sorted order. pprint
                # will match the ordering of `nest.flatten`.
                # This even works when nest.flatten reorders OrderedDicts, because
                # pprint is printing *after* the OrderedDicts have been reordered.
                cur_template = pprint.pformat(
                    nest.pack_sequence_as(input_, placeholders))
            templates.append(cur_template)

        # We join the templates for the various inputs into a single larger
        # template. We also remove all quotes surrounding the placeholders, so that
        # the formatted/printed output will not contain quotes around tensors.
        # (example of where these quotes might appear: if we have added a
        # placeholder string into a list, then pretty-formatted that list)
        template = sep.join(templates)
        template = template.replace("'" + placeholder + "'", placeholder)
        formatted_string = string_ops.string_format(inputs=tensors,
                                                    template=template,
                                                    placeholder=placeholder,
                                                    summarize=summarize,
                                                    name=format_name)

    if compat.forward_compatible(2019, 5, 27):
        return gen_logging_ops.print_v2(formatted_string,
                                        output_stream=output_stream_string,
                                        name=name,
                                        end=end)
    else:
        if end == os.linesep:
            end = ""
        return gen_logging_ops.print_v2(formatted_string + end,
                                        output_stream=output_stream_string,
                                        name=name)