mpcrl.LearnableParametersDict#
- class mpcrl.LearnableParametersDict(pars=None)[source]#
Bases:
dict[str,LearnableParameter]dict-based collection ofLearnableParameterinstances that simplifies the process of managing and updating these. The dict contains pairs of parameter’s name and parameter’s instance.Parameters can be retrieved as a normal dictionary by their names, but the class also offers several properties that are useful for managing the parameters in bulk, such as
lb,ub,value,value_as_dict. With a single call toupdate_values, the values of the parameters can also be updated.- Parameters:
- parsiterable of
LearnableParameter, optional An optional iterable of parameters to insert into the dict by their names.
- parsiterable of
Notes
To speed up computations, properties of this class are often cached for faster calls to the same methods. However, these are automatically cleared when the underlying dict is modified.
Methods
clear()Remove all items from the dict.
copy([deep, invalidate_caches])Creates a shallow or deep copy of the dict of learnable parameters.
fromkeys(iterable[, value])Create a new dictionary with keys from iterable and values set to value.
get(key[, default])Return the value for key if key is in the dictionary, else default.
items(/)Return a set-like object providing a view on the dict's items.
keys(/)Return a set-like object providing a view on the dict's keys.
pop(k[,d])If the key is not found, return the default if given; otherwise, raise a KeyError.
popitem()Remove and return a (key, value) pair as a 2-tuple.
setdefault(par)Insert key with a value of default if key is not in the dictionary.
stringify([summarize, precision, ddof])Returns a string representing the dict of learnable parameters.
update(pars, *args)If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
update_values(new_values, **is_close_kwargs)Updates the value of each parameter
values(/)Return an object providing a view on the dict's values.
Attributes
Gets the lower bound of all the learnable parameters, concatenated.
Gets the overall size of all the learnable parameters.
Gets the upper bound of all the learnable parameters, concatenated.
Gets the values of all the learnable parameters, concatenated.
Gets the values of all the learnable parameters as a
dict.- clear()#
Remove all items from the dict.
- copy(deep=False, invalidate_caches=True)[source]#
Creates a shallow or deep copy of the dict of learnable parameters.
- Parameters:
- deepbool, optional
If
True, a deepcopy of the dict and its parameters is returned; otherwise, the copy is only shallow.- invalidate_cachesbool, optional
If True, methods decorated with
csnlp.core.cache.invalidate_cacheare called to clear cached properties/lru caches in the copied instance. Otherwise, caches in the copy are not invalidated. By default,True. Only relevant whendeep=True.
- Returns:
- LearnableParametersDict
A copy of the dict of learnable parameters.
- Return type:
- classmethod fromkeys(iterable, value=None, /)#
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)#
Return the value for key if key is in the dictionary, else default.
- items(/)#
Return a set-like object providing a view on the dict’s items.
- keys(/)#
Return a set-like object providing a view on the dict’s keys.
- property lb: ndarray[tuple[Any, ...], dtype[floating]][source]#
Gets the lower bound of all the learnable parameters, concatenated.
- pop(k[, d]) v, remove specified key and return the corresponding value.#
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()#
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(par)[source]#
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- Return type:
- stringify(summarize=True, precision=3, ddof=0)[source]#
Returns a string representing the dict of learnable parameters.
- Parameters:
- summarizebool, optional
If
True(default), array parameters are summarized; otherwise, the entire array is printed.- precisionint, optional
The printing precision of floating point numbers.
- ddofint, optional
Degrees of freedom for computing standard deviations (see
numpy.std).
- Returns:
- str
A string representing the dict and its parameters.
- Return type:
- property ub: ndarray[tuple[Any, ...], dtype[floating]][source]#
Gets the upper bound of all the learnable parameters, concatenated.
- update(pars, *args)[source]#
If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- Return type:
- update_values(new_values, **is_close_kwargs)[source]#
Updates the value of each parameter
- Parameters:
- new_valuesarray_like or dict of (str, array_like)
The parameters’ new values, either as a single concatenated array (which will be splitted according to the sizes and each piece sequentially assigned to each parameter), or as a dict of parameter’s name vs parameter’s new value.
- is_close_kwargs
Additional kwargs for
numpy.isclose, e.g.,rtolandatol, for checking numerical values of parameters close to a bound.
- Raises:
- ValueError
In case of array-like, raises if
new_valuescannot be split according to the sizes of parameters; or if the new values cannot be broadcasted to 1D vectors according to each parameter’s size; or if the new values lie outside either the lower or upper bounds of each parameter.
- Return type:
- property value: ndarray[tuple[Any, ...], dtype[floating]][source]#
Gets the values of all the learnable parameters, concatenated.
- property value_as_dict: dict[str, ndarray[tuple[Any, ...], dtype[floating]]][source]#
Gets the values of all the learnable parameters as a
dict.
- values(/)#
Return an object providing a view on the dict’s values.