Generate a RichTextLines object showing a tensor in formatted style. Args: tensor: The tensor to be displayed, as a numpy ndarray or other appropriate format (e.g., None representing uninitialized tensors). tensor_label: A label for the tensor, as a string. If set to None, will
(tensor,
tensor_label,
include_metadata=False,
auxiliary_message=None,
include_numeric_summary=False,
np_printoptions=None,
highlight_options=None)
| 70 | |
| 71 | |
| 72 | def format_tensor(tensor, |
| 73 | tensor_label, |
| 74 | include_metadata=False, |
| 75 | auxiliary_message=None, |
| 76 | include_numeric_summary=False, |
| 77 | np_printoptions=None, |
| 78 | highlight_options=None): |
| 79 | """Generate a RichTextLines object showing a tensor in formatted style. |
| 80 | |
| 81 | Args: |
| 82 | tensor: The tensor to be displayed, as a numpy ndarray or other |
| 83 | appropriate format (e.g., None representing uninitialized tensors). |
| 84 | tensor_label: A label for the tensor, as a string. If set to None, will |
| 85 | suppress the tensor name line in the return value. |
| 86 | include_metadata: Whether metadata such as dtype and shape are to be |
| 87 | included in the formatted text. |
| 88 | auxiliary_message: An auxiliary message to display under the tensor label, |
| 89 | dtype and shape information lines. |
| 90 | include_numeric_summary: Whether a text summary of the numeric values (if |
| 91 | applicable) will be included. |
| 92 | np_printoptions: A dictionary of keyword arguments that are passed to a |
| 93 | call of np.set_printoptions() to set the text format for display numpy |
| 94 | ndarrays. |
| 95 | highlight_options: (HighlightOptions) options for highlighting elements |
| 96 | of the tensor. |
| 97 | |
| 98 | Returns: |
| 99 | A RichTextLines object. Its annotation field has line-by-line markups to |
| 100 | indicate which indices in the array the first element of each line |
| 101 | corresponds to. |
| 102 | """ |
| 103 | lines = [] |
| 104 | font_attr_segs = {} |
| 105 | |
| 106 | if tensor_label is not None: |
| 107 | lines.append("Tensor \"%s\":" % tensor_label) |
| 108 | suffix = tensor_label.split(":")[-1] |
| 109 | if suffix.isdigit(): |
| 110 | # Suffix is a number. Assume it is the output slot index. |
| 111 | font_attr_segs[0] = [(8, 8 + len(tensor_label), "bold")] |
| 112 | else: |
| 113 | # Suffix is not a number. It is auxiliary information such as the debug |
| 114 | # op type. In this case, highlight the suffix with a different color. |
| 115 | debug_op_len = len(suffix) |
| 116 | proper_len = len(tensor_label) - debug_op_len - 1 |
| 117 | font_attr_segs[0] = [ |
| 118 | (8, 8 + proper_len, "bold"), |
| 119 | (8 + proper_len + 1, 8 + proper_len + 1 + debug_op_len, "yellow") |
| 120 | ] |
| 121 | |
| 122 | if isinstance(tensor, debug_data.InconvertibleTensorProto): |
| 123 | if lines: |
| 124 | lines.append("") |
| 125 | lines.extend(str(tensor).split("\n")) |
| 126 | return debugger_cli_common.RichTextLines(lines) |
| 127 | elif not isinstance(tensor, np.ndarray): |
| 128 | # If tensor is not a np.ndarray, return simple text-line representation of |
| 129 | # the object without annotations. |
nothing calls this directly
no test coverage detected