Source code for eleanor.torch.variability
from collections.abc import Sequence
import torch
[docs]
def update_d2d_variability(model: torch.nn.Module, shape: Sequence[int]) -> None:
"""
Update the device to device variability of all
the D2DVar parameters with a new key.
Parameters
==========
model: PyTrtorch.nn.Moduleee
Model with the parameters that want to update the D2D.
shape: Sequence[int]
Shape of the variability array.
"""
for module in model.modules():
if isinstance(module, D2DVar):
module.update_variability(shape)
[docs]
def update_d2d_variability_name(
model: torch.nn.Module, name: str, shape: Sequence[int]
) -> None:
"""
Update the device to device variability of all
the D2DVar parameters with the same name..
Parameters
==========
model: PyTrtorch.nn.Moduleee
Model with the parameters that want to update the D2D.
name: Array
Name of the parameter to update.
shape: Sequence[int]
Shape of the variability array.
"""
for module in model.modules():
if isinstance(module, D2DVar) and module.name == name:
module.update_variability(shape)
[docs]
def set_d2d_variability(
model: torch.nn.Module, variability: float | Sequence[float]
) -> None:
"""
Set a new variability value to all the D2D objects of a model
Parameters
==========
model: PyTrtorch.nn.Moduleee
Model that contains the D2D objects.
variability: float | Sequence[float]
New variability value/s.
"""
modules = []
for module in model.modules():
if isinstance(module, D2DVar):
modules.append(module)
if not isinstance(variability, Sequence):
variability = [variability] * len(modules)
assert len(variability) == len(modules), (
"Number of modules does not match the number of variabilities"
)
for var, module in zip(variability, modules):
module.set_variability(var)
[docs]
def set_d2d_variability_name(
model: torch.nn.Module, name: str, variability: float | Sequence[float]
) -> None:
"""
Set a new variability value to all the D2D objects of a model with
a given `name`
Parameters
==========
model: PyTrtorch.nn.Moduleee
Model that contains the D2D objects.
name: str
Name of the D2D parameter.
variability: float | Sequence[float]
New variability value/s.
"""
modules = []
for module in model.modules():
if isinstance(module, D2DVar) and module.name == name:
modules.append(module)
if not isinstance(variability, Sequence):
variability = [variability] * len(modules)
assert len(variability) == len(modules), (
"Number of modules does not match the number of variabilities"
)
for var, module in zip(variability, modules):
module.set_variability(var)
[docs]
class D2DVar(torch.nn.Module):
"""Device to device variability for eleanor models
Apply a percentage of variability to a parameter of the models
Attributes
==========
name: str
Name of the variability parameter.
variability: float
Percentage of variability.
Example
-------
>>> param_var = D2DVar("param", 0.1)
>>> param_with_variability = param_var(param, (10,))
"""
_rval: torch.Tensor
def __init__(self, name: str, variability: float):
super().__init__()
self.name = name
self.variability = variability
self.register_buffer("_rval", torch.zeros(0), False)
[docs]
def update_variability(self, shape: Sequence[int]) -> None:
"""
Update the D2D variability array.
Parameters
----------
mu: torch.Tensor
Mean value of the parameter to apply the variability.
shape: Sequence[int]
Output shape of the array.
"""
self._rval = torch.randn(shape, device=self._rval.device)
[docs]
def set_variability(self, variability: float) -> None:
"""
Set the D2D variability percentage.
Parameters
----------
variability: float
Variability percentage.
"""
self.variability = variability
[docs]
def __call__(self, mu: torch.Tensor, shape: Sequence[int]) -> torch.Tensor:
"""
Apply D2D variability into the input parameter.
Parameters
----------
mu: torch.Tensor
Mean value of the parameter to apply the variability.
shape: Sequence[int]
Output shape of the array.
Returns
-------
torch.Tensor with coefficient of variation
:math:`\\text{variability} = \\sigma/\\mu`
"""
if self._rval.shape != shape:
self.update_variability(shape)
return mu * (1 + self.variability * self._rval)
[docs]
class C2CVar(torch.nn.Module):
"""Cycle to cycle variability for eleanor models
Apply a percentage of variability to a parameter of the models on evey call
Attributes
==========
name: str
Name of the variability parameter.
variability: float
Percentage of variability.
Example
-------
>>> param_var = C2CVar("param", 0.1)
>>> param_with_variability = param_var(param)
"""
def __init__(self, name: str, variability: float):
super().__init__()
self.name = name
self.variability = variability
[docs]
def set_variability(self, variability: float) -> None:
"""
Set the D2D variability percentage.
Parameters
----------
variability: float
Variability percentage.
"""
self.variability = variability
[docs]
def __call__(self, mu: torch.Tensor, shape: Sequence[int]) -> torch.Tensor:
"""
Apply C2C variability into the input parameter.
Parameters
----------
mu: torch.Tensor
Mean value of the parameter to apply the variability.
shape: StateShape
Output shape of the array.
key: PRNGKey
Key to generate random variability
Returns
-------
Array with coefficient of variation :math:`\\text{variability} = \\sigma/\\mu`
"""
return mu * (1 + self.variability * torch.randn(shape))