Generate formatted str to represent a tensor or its slices. Args: tensor: (numpy ndarray) The tensor value. tensor_name: (str) Name of the tensor, e.g., the tensor's debug watch key. np_printoptions: (dict) Numpy tensor formatting options. print_all: (bool) Whether the tensor is t
(tensor,
tensor_name,
np_printoptions,
print_all=False,
tensor_slicing=None,
highlight_options=None,
include_numeric_summary=False,
write_path=None)
| 148 | |
| 149 | |
| 150 | def format_tensor(tensor, |
| 151 | tensor_name, |
| 152 | np_printoptions, |
| 153 | print_all=False, |
| 154 | tensor_slicing=None, |
| 155 | highlight_options=None, |
| 156 | include_numeric_summary=False, |
| 157 | write_path=None): |
| 158 | """Generate formatted str to represent a tensor or its slices. |
| 159 | |
| 160 | Args: |
| 161 | tensor: (numpy ndarray) The tensor value. |
| 162 | tensor_name: (str) Name of the tensor, e.g., the tensor's debug watch key. |
| 163 | np_printoptions: (dict) Numpy tensor formatting options. |
| 164 | print_all: (bool) Whether the tensor is to be displayed in its entirety, |
| 165 | instead of printing ellipses, even if its number of elements exceeds |
| 166 | the default numpy display threshold. |
| 167 | (Note: Even if this is set to true, the screen output can still be cut |
| 168 | off by the UI frontend if it consist of more lines than the frontend |
| 169 | can handle.) |
| 170 | tensor_slicing: (str or None) Slicing of the tensor, e.g., "[:, 1]". If |
| 171 | None, no slicing will be performed on the tensor. |
| 172 | highlight_options: (tensor_format.HighlightOptions) options to highlight |
| 173 | elements of the tensor. See the doc of tensor_format.format_tensor() |
| 174 | for more details. |
| 175 | include_numeric_summary: Whether a text summary of the numeric values (if |
| 176 | applicable) will be included. |
| 177 | write_path: A path to save the tensor value (after any slicing) to |
| 178 | (optional). `numpy.save()` is used to save the value. |
| 179 | |
| 180 | Returns: |
| 181 | An instance of `debugger_cli_common.RichTextLines` representing the |
| 182 | (potentially sliced) tensor. |
| 183 | """ |
| 184 | |
| 185 | if tensor_slicing: |
| 186 | # Validate the indexing. |
| 187 | value = command_parser.evaluate_tensor_slice(tensor, tensor_slicing) |
| 188 | sliced_name = tensor_name + tensor_slicing |
| 189 | else: |
| 190 | value = tensor |
| 191 | sliced_name = tensor_name |
| 192 | |
| 193 | auxiliary_message = None |
| 194 | if write_path: |
| 195 | with gfile.Open(write_path, "wb") as output_file: |
| 196 | np.save(output_file, value) |
| 197 | line = debugger_cli_common.RichLine("Saved value to: ") |
| 198 | line += debugger_cli_common.RichLine(write_path, font_attr="bold") |
| 199 | line += " (%sB)" % bytes_to_readable_str(gfile.Stat(write_path).length) |
| 200 | auxiliary_message = debugger_cli_common.rich_text_lines_from_rich_line_list( |
| 201 | [line, debugger_cli_common.RichLine("")]) |
| 202 | |
| 203 | if print_all: |
| 204 | np_printoptions["threshold"] = value.size |
| 205 | else: |
| 206 | np_printoptions["threshold"] = DEFAULT_NDARRAY_DISPLAY_THRESHOLD |
| 207 |
nothing calls this directly
no test coverage detected