Utilities
The npfl138
package also provides a few utilities for startup initialization,
parameter initialization override, and for version management.
npfl138.first_time
Returns True
when first called with the given tag
.
Source code in npfl138/first_time.py
10 11 12 13 14 15 16 |
|
npfl138.global_keras_initializers
global_keras_initializers(
parameter_initialization: bool = True,
batchnorm_momentum_override: bool = True,
) -> None
Change default PyTorch initializers to Keras defaults.
The following initializers are used:
Linear
,Conv1d
,Conv2d
,Conv3d
,ConvTranspose1d
,ConvTranspose2d
,ConvTranspose3d
,Bilinear
: Xavier uniform for weights, zeros for biases.Embedding
,EmbeddingBag
: Uniform [-0.05, 0.05] for weights.RNN
,RNNCell
,LSTM
,LSTMCell
,GRU
,GRUCell
: Xavier uniform for input weights, orthogonal for recurrent weights, zeros for biases (with LSTM forget gate bias set to 1).
Furthermore, for batch normalization layers, the default momentum value is changed from 0.1 to 0.01.
Parameters:
-
parameter_initialization
(bool
, default:True
) –If True, override the default PyTorch initializers with Keras defaults.
-
batchnorm_momentum_override
(bool
, default:True
) –If True, override the default momentum value of 0.1 in batch normalization layers to 0.01.
Source code in npfl138/initializers_override.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
npfl138.require_version
require_version(required_version: str) -> None
Make sure the installed version is at least required_version
.
If not, show a nice error message with the instructions how to upgrade.
Parameters:
-
required_version
(str
) –The required version of the npfl138 package, as a string in the format "semester.week" or "semester.week.patch".
Source code in npfl138/version.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
npfl138.rl_utils
npfl138.rl_utils.EvaluationEnv
Bases: Wrapper
A wrapper over gym environments capable of performing evaluation on demant.
In addition to a standard gym envrironment, it provides the following features:
- The
reset
method can be called asreset(start_evaluation=False)
to start the evaluation. - The
env.episode
property returns the number of completed episodes.
Source code in npfl138/rl_utils.py
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 |
|
npfl138.rl_utils.typed_torch_function
typed_torch_function(device, *types, via_np=True)
Typed Torch function decorator.
The positional input arguments are converted to torch Tensors of the given types and on the given device; for NumPy arrays on the same device, the conversion should not copy the data.
The torch Tensors generated by the wrapped function are converted back to Numpy arrays before returning (while keeping original tuples, lists, and dictionaries).
Source code in npfl138/rl_utils.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
npfl138.startup
startup(
seed: int | None = None,
threads: int | None = None,
forkserver_instead_of_fork: bool = False,
recodex: bool = False,
) -> None
Initialize the environment.
- Allow using TF32 for matrix multiplication.
- Set the random seed if given.
- Set the number of threads if given.
- Use
forkserver
instead offork
if requested. - Optionally run in ReCodEx mode for better replicability. In this mode,
- Layer initialization does not depend on the global random seed generator (it is deterministic and depends only on the parameter shape).
- Every dataloader uses its own random generator.
- However, the deterministic layer initialization decreases performance of trained models, so it should be used only for running tests.
Parameters:
-
seed
(int | None
, default:None
) –If not
None
, set the Python, Numpy, and PyTorch random seeds to this value. -
threads
(int | None
, default:None
) –If not
None
of 0, set the number of threads to this value. Otherwise, use as many threads as cores. -
forkserver_instead_of_fork
(bool
, default:False
) –If
True
, useforkserver
instead offork
as the default multiprocessing method. This will be the default in Python 3.14. -
recodex
(bool
, default:False
) –If
True
, run in ReCodEx mode for better replicability of tests.
Source code in npfl138/startup.py
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 |
|
npfl138.__version__
module-attribute
__version__ = '2425.14.2'