File size: 1,648 Bytes
36fdbcf |
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 |
import numpy as np
import torch
from torch import Tensor
def seed(seed: int = None):
"""
Sets the seed for both the numpy and PyTorch random number generators.
Parameters
----------
seed : int or None, optional
Seed value to be used for random number generation. If None (default),
the seed is set to 0.
"""
if seed is None:
seed = 0
np.random.seed(seed)
torch.manual_seed(seed)
def chance(prob: float) -> bool:
"""
Returns True with given probability.
Parameters
----------
prob : float
Probability of returning True. Must be in the range [0, 1].
Returns
-------
bool
True with probability `prob`.
"""
if prob < 0.0 or prob > 1.0:
raise ValueError(f'chance() expected a value in the range [0, 1], but got {prob}')
return np.random.rand() < prob
def grid_coordinates(shape, device : torch.device = None) -> Tensor:
"""
TODOC
"""
ranges = [torch.arange(s, dtype=torch.float32, device=device) for s in shape]
meshgrid = torch.stack(torch.meshgrid(*ranges, indexing='ij'), dim=-1)
return meshgrid
def quantile(arr, q):
"""
TODOC
"""
if q < 0 or q > 1:
raise ValueError(f'quantile must be between 0 and 1, got {q}')
if q == 0:
return arr.min()
if q == 1:
return arr.max()
arr = arr.flatten()
if q > 0.5:
k = int(arr.numel() * (1.0 - q)) + 1
return arr.topk(k, largest=True, sorted=False).values.min()
else:
k = int(arr.numel() * q) + 1
return arr.topk(k, largest=False, sorted=False).values.max()
|