Spaces:
Sleeping
Sleeping
File size: 3,451 Bytes
5ac1897 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# Provides methods to summarize the information of data, giving a brief overview in text.
import torch
import numpy as np
from typing import Optional
from .log import get_logger
def look_tensor(
x : torch.Tensor,
prompt : Optional[str] = None,
silent : bool = False,
):
'''
Summarize the information of a tensor, including its shape, value range (min, max, mean, std), and dtype.
Then return a string containing the information.
### Args
- x: torch.Tensor
- silent: bool, default `False`
- If not silent, the function will print the message itself. The information string will always be returned.
- prompt: Optional[str], default `None`
- If have prompt, it will be printed at the very beginning.
### Returns
- str
'''
info_list = [] if prompt is None else [prompt]
# Convert to float to calculate the statistics.
x_num = x.float()
info_list.append(f'π [{x_num.min():06f} -> {x_num.max():06f}] ~ ({x_num.mean():06f}, {x_num.std():06f})')
info_list.append(f'π¦ {tuple(x.shape)}')
info_list.append(f'π·οΈ {x.dtype}')
info_list.append(f'π₯οΈ {x.device}')
# Generate the final information and print it if necessary.
ret = '\t'.join(info_list)
if not silent:
get_logger().info(ret)
return ret
def look_ndarray(
x : np.ndarray,
silent : bool = False,
prompt : Optional[str] = None,
):
'''
Summarize the information of a numpy array, including its shape, value range (min, max, mean, std), and dtype.
Then return a string containing the information.
### Args
- x: np.ndarray
- silent: bool, default `False`
- If not silent, the function will print the message itself. The information string will always be returned.
- prompt: Optional[str], default `None`
- If have prompt, it will be printed at the very beginning.
### Returns
- str
'''
info_list = [] if prompt is None else [prompt]
# Convert to float to calculate the statistics.
x_num = x.astype(np.float32)
info_list.append(f'π [ {x_num.min():06f} -> {x_num.max():06f} ] ~ ( {x_num.mean():06f}, {x_num.std():06f} )')
info_list.append(f'π¦ {tuple(x.shape)}')
info_list.append(f'π·οΈ {x.dtype}')
# Generate the final information and print it if necessary.
ret = '\t'.join(info_list)
if not silent:
get_logger().info(ret)
return ret
def look_dict(
d : dict,
silent : bool = False,
):
'''
Summarize the information of a dictionary, including the keys and the information of the values.
Then return a string containing the information.
### Args
- d: dict
- silent: bool, default `False`
- If not silent, the function will print the message itself. The information string will always be returned.
### Returns
- str
'''
info_list = ['{']
for k, v in d.items():
if isinstance(v, torch.Tensor):
info_list.append(f'{k} : tensor: {look_tensor(v, silent=True)}')
elif isinstance(v, np.ndarray):
info_list.append(f'{k} : ndarray: {look_ndarray(v, silent=True)}')
elif isinstance(v, str):
info_list.append(f'{k} : {v[:32]}')
else:
info_list.append(f'{k} : {type(v)}')
info_list.append('}')
ret = '\n'.join(info_list)
if not silent:
get_logger().info(ret)
return ret |