Skip to content

viberl.utils.common

Functions:

Name Description
set_seed

Set random seed for reproducibility.

get_device

Get the best available device (CUDA if available, else CPU).

normalize_returns

Normalize returns to zero mean and unit variance.

set_seed

set_seed(seed: int) -> None

Set random seed for reproducibility.

Parameters:

Name Type Description Default
seed int

Random seed value

required
Source code in viberl/utils/common.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def set_seed(seed: int) -> None:
    """
    Set random seed for reproducibility.

    Args:
        seed: Random seed value
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

get_device

get_device() -> device

Get the best available device (CUDA if available, else CPU).

Returns:

Type Description
device

PyTorch device

Source code in viberl/utils/common.py
22
23
24
25
26
27
28
29
def get_device() -> torch.device:
    """
    Get the best available device (CUDA if available, else CPU).

    Returns:
        PyTorch device
    """
    return torch.device('cuda' if torch.cuda.is_available() else 'cpu')

normalize_returns

normalize_returns(returns: ndarray) -> ndarray

Normalize returns to zero mean and unit variance.

Parameters:

Name Type Description Default
returns ndarray

Array of returns

required

Returns:

Type Description
ndarray

Normalized returns

Source code in viberl/utils/common.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def normalize_returns(returns: np.ndarray) -> np.ndarray:
    """
    Normalize returns to zero mean and unit variance.

    Args:
        returns: Array of returns

    Returns:
        Normalized returns
    """
    if len(returns) == 0:
        return returns

    mean = np.mean(returns)
    std = np.std(returns)

    if std == 0:
        return returns - mean

    return (returns - mean) / (std + 1e-8)