Utils.misc(通用函数) 模块¶
ppsci.utils.misc
¶
AverageMeter
¶
Computes and stores the average and current value Code was based on https://github.com/pytorch/examples/blob/master/imagenet/main.py
Source code in ppsci/utils/misc.py
reset()
¶
PrettyOrderedDict
¶
Prettydefaultdict
¶
RankZeroOnly
¶
A context manager that ensures the code inside it is only executed by the process
with rank zero. All rank will be synchronized by dist.barrier
in
distributed environment.
NOTE: Always used for time consuming code blocks, such as initialization of log writer, saving result to disk, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank |
Optional[int]
|
The rank of the current process. If not provided,
it will be obtained from |
None
|
Examples:
>>> import paddle.distributed as dist
>>> with RankZeroOnly(dist.get_rank()) as is_master:
... if is_master:
... # code here that should only be executed by the master process
Source code in ppsci/utils/misc.py
__enter__()
¶
Enter the context and check if the current process is the master.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the current process is the master (rank zero), False otherwise. |
__init__(rank=None)
¶
Enter the context and check if the current process is the master.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank |
Optional[int]
|
The rank of the current process. If not provided,
it will be obtained from |
None
|
Source code in ppsci/utils/misc.py
Timer
¶
Bases: ContextDecorator
Count time cost for code block within context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of timer discriminate different code block. Defaults to "Timer". |
'Timer'
|
auto_print |
bool
|
Whether print time cost when exit context. Defaults to True. |
True
|
Examples:
>>> import paddle
>>> from ppsci.utils import misc
>>> with misc.Timer("test1", auto_print=False) as timer:
... w = sum(range(0, 10))
>>> print(f"time cost of 'sum(range(0, 10))' is {timer.interval:.2f}")
time cost of 'sum(range(0, 10))' is 0.00
Source code in ppsci/utils/misc.py
all_gather(tensor, concat=True, axis=0)
¶
Gather tensor from all devices, concatenate them along given axis if specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
Tensor to be gathered from all GPUs. |
required |
concat |
bool
|
Whether to concatenate gathered Tensors. Defaults to True. |
True
|
axis |
int
|
Axis which concatenated along. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
Union[Tensor, List[Tensor]]
|
Union[paddle.Tensor, List[paddle.Tensor]]: Gathered Tensors. |
Source code in ppsci/utils/misc.py
concat_dict_list(dict_list)
¶
Concatenate arrays in tuple of dicts at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_list |
Sequence[Dict[str, ndarray]]
|
Sequence of dicts. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: A dict with concatenated arrays for each key. |
Source code in ppsci/utils/misc.py
convert_to_array(dict_, keys)
¶
Concatenate arrays in axis -1 in order of given keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_ |
Dict[str, ndarray]
|
Dict contains arrays. |
required |
keys |
Tuple[str, ...]
|
Concatenate keys used in concatenation. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Concatenated array. |
Source code in ppsci/utils/misc.py
convert_to_dict(array, keys)
¶
Split given array into single channel array at axis -1 in order of given keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ndarray
|
Array to be split. |
required |
keys |
Tuple[str, ...]
|
Keys used in split. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: Split dict. |
Source code in ppsci/utils/misc.py
stack_dict_list(dict_list)
¶
Stack arrays in tuple of dicts at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_list |
Sequence[Dict[str, ndarray]]
|
Sequence of dicts. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: A dict with stacked arrays for each key. |
Source code in ppsci/utils/misc.py
cartesian_product(*arrays)
¶
Cartesian product for input sequence of array(s).
Reference: https://stackoverflow.com/questions/11144513/cartesian-product-of-x-and-y-array-points-into-single-array-of-2d-points
Assume shapes of input arrays are: \((N_1,), (N_2,), (N_3,), ..., (N_M,)\), then the cartesian product result will be shape of \((N_1×N_2×N_3×...×N_M, M)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays |
ndarray
|
Input arrays. |
()
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Cartesian product result of shape \((N_1×N_2×N_3×...×N_M, M)\). |
Examples:
>>> t = np.array([1, 2])
>>> x = np.array([10, 20])
>>> y = np.array([100, 200])
>>> txy = cartesian_product(t, x, y)
>>> print(txy)
[[ 1 10 100]
[ 1 10 200]
[ 1 20 100]
[ 1 20 200]
[ 2 10 100]
[ 2 10 200]
[ 2 20 100]
[ 2 20 200]]
Source code in ppsci/utils/misc.py
combine_array_with_time(x, t)
¶
Combine given data x with time sequence t. Given x with shape (N, D) and t with shape (T, ), this function will repeat t_i for N times and will concat it with data x for each t_i in t, finally return the stacked result, which is of shape (N×T, D+1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
Points data with shape (N, D). |
required |
t |
Tuple[int, ...]
|
Time sequence with shape (T, ). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Combined data with shape of (N×T, D+1). |
Source code in ppsci/utils/misc.py
set_random_seed(seed)
¶
Set numpy, random, paddle random_seed to given seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed |
int
|
Random seed. |
required |
run_on_eval_mode(func)
¶
A decorator automatically running given class method in eval mode and keep training state unchanged after function finished.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
Class method which is expected running in eval mode. |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
Decorated class method. |
Source code in ppsci/utils/misc.py
run_at_rank0(func)
¶
A decorator that allow given function run only at rank 0 to avoid multiple logs or other events. Usually effected in distributed environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
Given function. |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
Wrapped function which will only run at at rank 0, skipped at other rank. |
Source code in ppsci/utils/misc.py
plot_curve(data, xlabel='X', ylabel='Y', output_dir='./output/', smooth_step=1, use_semilogy=False)
¶
Plotting curve.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, List]
|
Dict of all data, keys are curves' name. |
required |
xlabel |
str
|
Label of x-axis. Defaults to "X". |
'X'
|
ylabel |
str
|
Label of y-axis. Defaults to "Y". |
'Y'
|
output_dir |
str
|
Output directory of figure. Defaults to "./output/". |
'./output/'
|
smooth_step |
int
|
How many points are squeezed to one point to smooth the curve. Defaults to 1. |
1
|
use_semilogy |
bool
|
Whether to set non-uniform coordinates for the y-axis. Defaults to False. |
False
|
Source code in ppsci/utils/misc.py
创建日期: November 6, 2023