Exemple #1
0
    def _format_tensor(self,
                       tensor,
                       watch_key,
                       np_printoptions,
                       print_all=False,
                       tensor_slicing=None,
                       highlight_options=None):
        """Generate formatted str to represent a tensor or its slices.

    Args:
      tensor: (numpy ndarray) The tensor value.
      watch_key: (str) Tensor debug watch key.
      np_printoptions: (dict) Numpy tensor formatting options.
      print_all: (bool) Whether the tensor is to be displayed in its entirety,
        instead of printing ellipses, even if its number of elements exceeds
        the default numpy display threshold.
        (Note: Even if this is set to true, the screen output can still be cut
         off by the UI frontend if it consist of more lines than the frontend
         can handle.)
      tensor_slicing: (str or None) Slicing of the tensor, e.g., "[:, 1]". If
        None, no slicing will be performed on the tensor.
      highlight_options: (tensor_format.HighlightOptions) options to highlight
        elements of the tensor. See the doc of tensor_format.format_tensor()
        for more details.

    Returns:
      (str) Formatted str representing the (potentially sliced) tensor.

    Raises:
      ValueError: If tehsor_slicing is not a valid numpy ndarray slicing str.
    """

        if tensor_slicing:
            # Validate the indexing.
            if not command_parser.validate_slicing_string(tensor_slicing):
                raise ValueError("Invalid tensor-slicing string.")

            value = eval("tensor" + tensor_slicing)  # pylint: disable=eval-used
            sliced_name = watch_key + tensor_slicing
        else:
            value = tensor
            sliced_name = watch_key

        if print_all:
            np_printoptions["threshold"] = value.size
        else:
            np_printoptions[
                "threshold"] = self.default_ndarray_display_threshold

        return tensor_format.format_tensor(
            value,
            sliced_name,
            include_metadata=True,
            np_printoptions=np_printoptions,
            highlight_options=highlight_options)
Exemple #2
0
    def _format_tensor(self,
                       tensor,
                       watch_key,
                       np_printoptions,
                       print_all=False,
                       tensor_slicing=None,
                       highlight_options=None):
        """Generate formatted str to represent a tensor or its slices.

    Args:
      tensor: (numpy ndarray) The tensor value.
      watch_key: (str) Tensor debug watch key.
      np_printoptions: (dict) Numpy tensor formatting options.
      print_all: (bool) Whether the tensor is to be displayed in its entirety,
        instead of printing ellipses, even if its number of elements exceeds
        the default numpy display threshold.
        (Note: Even if this is set to true, the screen output can still be cut
         off by the UI frontend if it consist of more lines than the frontend
         can handle.)
      tensor_slicing: (str or None) Slicing of the tensor, e.g., "[:, 1]". If
        None, no slicing will be performed on the tensor.
      highlight_options: (tensor_format.HighlightOptions) options to highlight
        elements of the tensor. See the doc of tensor_format.format_tensor()
        for more details.

    Returns:
      (str) Formatted str representing the (potentially sliced) tensor.

    Raises:
      ValueError: If tehsor_slicing is not a valid numpy ndarray slicing str.
    """

        if tensor_slicing:
            # Validate the indexing.
            if not command_parser.validate_slicing_string(tensor_slicing):
                raise ValueError("Invalid tensor-slicing string.")

            value = eval("tensor" + tensor_slicing)  # pylint: disable=eval-used
            sliced_name = watch_key + tensor_slicing
        else:
            value = tensor
            sliced_name = watch_key

        if print_all:
            np_printoptions["threshold"] = value.size
        else:
            np_printoptions[
                "threshold"] = self.default_ndarray_display_threshold

        return tensor_format.format_tensor(value,
                                           sliced_name,
                                           include_metadata=True,
                                           np_printoptions=np_printoptions,
                                           highlight_options=highlight_options)
 def testValidateInvalidSlicingStrings(self):
   self.assertFalse(command_parser.validate_slicing_string(""))
   self.assertFalse(command_parser.validate_slicing_string("[1,"))
   self.assertFalse(command_parser.validate_slicing_string("2,3]"))
   self.assertFalse(command_parser.validate_slicing_string("[4, foo()]"))
   self.assertFalse(command_parser.validate_slicing_string("[5, bar]"))
 def testValidateValidSlicingStrings(self):
   self.assertTrue(command_parser.validate_slicing_string("[1]"))
   self.assertTrue(command_parser.validate_slicing_string("[2,3]"))
   self.assertTrue(command_parser.validate_slicing_string("[4, 5, 6]"))
   self.assertTrue(command_parser.validate_slicing_string("[7,:, :]"))
 def testValidateInvalidSlicingStrings(self):
     self.assertFalse(command_parser.validate_slicing_string(""))
     self.assertFalse(command_parser.validate_slicing_string("[1,"))
     self.assertFalse(command_parser.validate_slicing_string("2,3]"))
     self.assertFalse(command_parser.validate_slicing_string("[4, foo()]"))
     self.assertFalse(command_parser.validate_slicing_string("[5, bar]"))
 def testValidateValidSlicingStrings(self):
     self.assertTrue(command_parser.validate_slicing_string("[1]"))
     self.assertTrue(command_parser.validate_slicing_string("[2,3]"))
     self.assertTrue(command_parser.validate_slicing_string("[4, 5, 6]"))
     self.assertTrue(command_parser.validate_slicing_string("[7,:, :]"))