Get a text summary of a numeric tensor. This summary is only available for numeric (int*, float*, complex*) and Boolean tensors. Args: tensor: (`numpy.ndarray`) the tensor value object to be summarized. Returns: The summary text as a `RichTextLines` object. If the type of `tensor`
(tensor)
| 486 | |
| 487 | |
| 488 | def numeric_summary(tensor): |
| 489 | """Get a text summary of a numeric tensor. |
| 490 | |
| 491 | This summary is only available for numeric (int*, float*, complex*) and |
| 492 | Boolean tensors. |
| 493 | |
| 494 | Args: |
| 495 | tensor: (`numpy.ndarray`) the tensor value object to be summarized. |
| 496 | |
| 497 | Returns: |
| 498 | The summary text as a `RichTextLines` object. If the type of `tensor` is not |
| 499 | numeric or Boolean, a single-line `RichTextLines` object containing a |
| 500 | warning message will reflect that. |
| 501 | """ |
| 502 | |
| 503 | def _counts_summary(counts, skip_zeros=True, total_count=None): |
| 504 | """Format values as a two-row table.""" |
| 505 | if skip_zeros: |
| 506 | counts = [(count_key, count_val) for count_key, count_val in counts |
| 507 | if count_val] |
| 508 | max_common_len = 0 |
| 509 | for count_key, count_val in counts: |
| 510 | count_val_str = str(count_val) |
| 511 | common_len = max(len(count_key) + 1, len(count_val_str) + 1) |
| 512 | max_common_len = max(common_len, max_common_len) |
| 513 | |
| 514 | key_line = debugger_cli_common.RichLine("|") |
| 515 | val_line = debugger_cli_common.RichLine("|") |
| 516 | for count_key, count_val in counts: |
| 517 | count_val_str = str(count_val) |
| 518 | key_line += _pad_string_to_length(count_key, max_common_len) |
| 519 | val_line += _pad_string_to_length(count_val_str, max_common_len) |
| 520 | key_line += " |" |
| 521 | val_line += " |" |
| 522 | |
| 523 | if total_count is not None: |
| 524 | total_key_str = "total" |
| 525 | total_val_str = str(total_count) |
| 526 | max_common_len = max(len(total_key_str) + 1, len(total_val_str)) |
| 527 | total_key_str = _pad_string_to_length(total_key_str, max_common_len) |
| 528 | total_val_str = _pad_string_to_length(total_val_str, max_common_len) |
| 529 | key_line += total_key_str + " |" |
| 530 | val_line += total_val_str + " |" |
| 531 | |
| 532 | return debugger_cli_common.rich_text_lines_from_rich_line_list( |
| 533 | [key_line, val_line]) |
| 534 | |
| 535 | if not isinstance(tensor, np.ndarray) or not np.size(tensor): |
| 536 | return debugger_cli_common.RichTextLines([ |
| 537 | "No numeric summary available due to empty tensor."]) |
| 538 | elif (np.issubdtype(tensor.dtype, np.floating) or |
| 539 | np.issubdtype(tensor.dtype, np.complex) or |
| 540 | np.issubdtype(tensor.dtype, np.integer)): |
| 541 | counts = [ |
| 542 | ("nan", np.sum(np.isnan(tensor))), |
| 543 | ("-inf", np.sum(np.isneginf(tensor))), |
| 544 | ("-", np.sum(np.logical_and( |
| 545 | tensor < 0.0, np.logical_not(np.isneginf(tensor))))), |
no test coverage detected