paraqeet.optimization_map.OptimizationMap#
- class paraqeet.optimization_map.OptimizationMap#
Bases:
objectOptimization parameter map utility class.
Utility class that collects all parameters that shall be considered during optimization and associates them with the corresponding Optimizable interface. With this class, Quantities can be traced back to the Optimizable to which they belong. Before optimization, an instance of this class needs to be filled and passed to the optimizer.
- __init__()#
Methods
__init__()add(optimizable[, optimizable_quantities])Add an optimizable object and a list of its quantities to the map.
append(optimizable[, optimizable_quantities])Append an optimizable object and a list of its quantities to the map.
filter_by_name(name)Filter parameters by name of parameter.
filter_parameters(filter_function)Filter parameters using filter function.
from_dict(data)Restores the values of all optimized quantities that are in the dictionary.
Return all parameters that were added to the system map.
Return all optimizable objects that were added to this map.
get_parameters(optimizable)Return all quantities associated with the given parameter.
Register optimizable parameters with the system.
remove(optimizable[, params])Remove the given optimizable or parameter(s) from the optimization map.
remove_by_name(name)Remove parameters by name of parameter.
replace(optimizable, old_parameters, ...)Perform an in-place substitution of the old and new parameters.
to_dict()Create a dictionary represenation of the optimization map.
- add(optimizable, optimizable_quantities=None)#
Add an optimizable object and a list of its quantities to the map.
The list contains all parameters of the optimizable object that shall be considered during the optimization. If the list is empty, all parameters of the class will be used. If the object was already added, the list of quantities will be overwritten.
- Parameters:
optimizable (Optimizable) – Input Optimizable object for adding to the map.
optimizable_quantities (Quantity | list[Quantity] | None = None) – List of all parameters of the optimizable object considered for optimization.
- append(optimizable, optimizable_quantities=None)#
Append an optimizable object and a list of its quantities to the map.
This method is similar to the add method, but instead of overwriting the existing entries, this appends the specified list of quantities to the already existing quantities.
- filter_by_name(name)#
Filter parameters by name of parameter.
- Parameters:
name (str) – Name of parameter to be filtered with.
- filter_parameters(filter_function)#
Filter parameters using filter function.
Updates the list of parameters for all Optimizables in this map using a filter function. Only parameters for which the filter function returns true will remain in this map.
- Parameters:
filter_function (Callable) – Filter function that maps quantities to boolean values.
- Return type:
None
- from_dict(data)#
Restores the values of all optimized quantities that are in the dictionary. The format of the dictionary needs to be in the same format as generated by the toDict function.
- Parameters:
data (dict) – All quantities that should be restored.
- Raises:
SerialisationException – If the dict contains an Optimizable or a Quantity that does not exist in this optimization map.
- Return type:
None
- get_all_parameters()#
Return all parameters that were added to the system map.
- Returns:
All parameters that were added to the map.
- Return type:
List[Quantity]
- get_optimizables()#
Return all optimizable objects that were added to this map.
- Returns:
Set of all optimizable objects from the map.
- Return type:
set[Optimizable]
- get_parameters(optimizable)#
Return all quantities associated with the given parameter.
- Parameters:
optimizable (Optimizable) – Input optimizable object.
- Returns:
List of parameters or None (if the optimizable has not been added yet).
- Return type:
list[Quantity] | None
- register_params_with_optimizables()#
Register optimizable parameters with the system.
Utility function that synchronises the list of parameters with each optimizable class. This needs to be called by the optimizer before gradient based optimization to tell the layers which gradients to compute.
- Return type:
None
- remove(optimizable, params=None)#
Remove the given optimizable or parameter(s) from the optimization map.
If params is None, it removes the optimizable from the optimization map. Else it only removes the specific parameter from the optimization map.
- remove_by_name(name)#
Remove parameters by name of parameter.
- Parameters:
name (str) – Name of parameter to be filtered with.
- replace(optimizable, old_parameters, new_parameters)#
Perform an in-place substitution of the old and new parameters. This helps to keeps the ordering of parameters the same while replacing parameters.
- Parameters:
optimizable (Optimizable)
- Return type:
None
- to_dict()#
Create a dictionary represenation of the optimization map.
Creates a dictionary that contains the values of all quantities that are being optimized, sorted by the Optimizable instances to which they belong. The returned dictionary is meant for export using the serialisation package. It uses the names of Optimizables and Quantities and assumes that those are unique and not None. The format of the dict will be
"optimizable name": { "quantity name": { "unit": string, "shape": tuple[int, ...], "twoPi": bool, "value': Array, "min": Array, "max": Array } }
where the innermost part is generated by Quantity’s toDict function.
- Returns:
all optimized quantities in an exportable format
- Return type:
dict
- Raises:
SerialisationException – If the name of any Optimizable or Quantity is None or not unique.