param_utils
¶
This module provides utility functions for handling PyTorch models, including optimization, parameter management, and gradient retrieval.
Functions: create_optimizer: Instantiates the desired optimizer for the given model. get_num_parameters: Returns the number of trainable parameters in the model. set_parameters: Sets the parameters of the model from a given array of values. get_parameters: Gets the parameters of the model. get_gradients: Gets the gradients of the model parameters.
Functions:
-
fix_invalid_gradient_values
–Fixes invalid gradient values in the model's parameters.
-
get_gradients
–Gets the gradients of the model parameters.
-
get_num_parameters
–Returns the number of trainable parameters in the model.
-
get_parameters
–Gets the parameters of the model.
-
set_parameters
–Sets the parameters of the model from a given array of values.
fix_invalid_gradient_values
¶
fix_invalid_gradient_values(model: Module) -> None
Fixes invalid gradient values in the model's parameters.
This function iterates over all parameters of the given model and sets the gradient values to zero where they are not finite (i.e., NaN or infinity).
Parameters:
-
model
(Module
) –The neural network model whose gradient values need to be fixed.
Returns:
-
None
–This function modifies the gradients in place and does not return anything.
Source code in src/autoden/models/param_utils.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
get_gradients
¶
get_gradients(
model: Module, flatten: bool = True
) -> tuple[NDArray, Sequence[tuple[str, Sequence[int]]]]
Gets the gradients of the model parameters.
Parameters:
-
model
(Module
) –The model to get the gradients from.
-
flatten
(bool
, default:True
) –If True, flattens the gradients, by default True.
Returns:
-
tuple[NDArray, Sequence[tuple[str, Sequence[int]]]]
–A tuple containing the gradient values and their shapes.
Source code in src/autoden/models/param_utils.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
get_num_parameters
¶
Returns the number of trainable parameters in the model.
Parameters:
-
model
(Module
) –The model to count the parameters for.
-
verbose
(bool
, default:False
) –If True, prints the number of parameters, by default False.
Returns:
-
int
–The number of trainable parameters.
Source code in src/autoden/models/param_utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
get_parameters
¶
get_parameters(
model: Module,
parameter_type: str | None = None,
filter_params: bool = True,
) -> tuple[NDArray, Sequence[tuple[str, Sequence[int]]]]
Gets the parameters of the model.
Parameters:
-
model
(Module
) –The model to get the parameters from.
-
parameter_type
(str | None
, default:None
) –The type of parameters to filter, by default None.
-
filter_params
(bool
, default:True
) –If True, filters the parameters based on the parameter_type, by default True.
Returns:
-
tuple[NDArray, Sequence[tuple[str, Sequence[int]]]]
–A tuple containing the parameter values and their shapes.
Source code in src/autoden/models/param_utils.py
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 |
|
set_parameters
¶
set_parameters(
model: Module,
values: NDArray,
info: Sequence[tuple[str, Sequence[int]]],
) -> None
Sets the parameters of the model from a given array of values.
Parameters:
-
model
(Module
) –The model to set the parameters for.
-
values
(NDArray
) –The array of parameter values.
-
info
(Sequence[tuple[str, Sequence[int]]]
) –Information about the parameter names and shapes.
Raises:
-
ValueError
–If the length of the values array does not match the total number of parameters.
Source code in src/autoden/models/param_utils.py
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 |
|