Module flowcon.transforms.lipschitz.util
Adapted from Implicit Normalizing Flow (ICLR 2021). Link: https://github.com/thu-ml/implicit-normalizing-flows/blob/master/lib/layers/broyden.py
Functions
def exists(val)
def find_fixed_point(f, x0, threshold=1000, eps=1e-05)
def find_fixed_point_noaccel(f, x0, threshold=1000, eps=1e-05)
Classes
class BiasedParameterGenerator (n_power_series)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class BiasedParameterGenerator(ParameterGenerator): def __init__(self, n_power_series): super().__init__() self.n_power_series = n_power_series def sample_parameters(self, training=True): def coeff_fn(k): return 1 return coeff_fn, self.n_power_series
Ancestors
- ParameterGenerator
- torch.nn.modules.module.Module
Class variables
var call_super_init : bool
var dump_patches : bool
var training : bool
Methods
def sample_parameters(self, training=True)
Inherited members
class GeometricSampler (geom_p)
-
Expand source code
class GeometricSampler(Sampler): def __init__(self, geom_p): self.geom_p = expit(geom_p) def sample_fn(self, m): return self.geometric_sample(self.geom_p, m) def rcdf_fn(self, k, offset): return self.geometric_1mcdf(self.geom_p, k, offset) @staticmethod def geometric_sample(p, n_samples): return np.random.geometric(p, n_samples) @staticmethod def geometric_1mcdf(p, k, offset): if k <= offset: return 1. else: k = k - offset """P(n >= k)""" return (1 - p) ** max(k - 1, 0)
Ancestors
Static methods
def geometric_1mcdf(p, k, offset)
def geometric_sample(p, n_samples)
Methods
def rcdf_fn(self, k, offset)
def sample_fn(self, m)
class ParameterGenerator (*args, **kwargs)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class ParameterGenerator(torch.nn.Module): def sample_parameters(self, training=True) -> Tuple[Callable, int]: pass
Ancestors
- torch.nn.modules.module.Module
Subclasses
Class variables
var call_super_init : bool
var dump_patches : bool
var training : bool
Methods
def forward(self, *input: Any) ‑> None
-
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them. def sample_parameters(self, training=True) ‑> Tuple[Callable, int]
class Sampler
-
Expand source code
class Sampler(): def rcdf_fn(self, k, offset): pass def sample_fn(self, m): pass @classmethod def build_sampler(cls, n_dist, **kwargs): if n_dist == 'geometric': return GeometricSampler(kwargs.get("geom_p")) elif n_dist == 'poisson': return GeometricSampler(kwargs.get("lamb")) else: raise NotImplementedError(f"Unknown sampler '{n_dist}'.")
Subclasses
Static methods
def build_sampler(n_dist, **kwargs)
Methods
def rcdf_fn(self, k, offset)
def sample_fn(self, m)
class Sine (w0=1.0)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class Sine(nn.Module): def __init__(self, w0=1.): super().__init__() self.w0 = w0 def forward(self, x): return torch.sin(self.w0 * x) / self.w0
Ancestors
- torch.nn.modules.module.Module
Class variables
var call_super_init : bool
var dump_patches : bool
var training : bool
Methods
def forward(self, x) ‑> Callable[..., Any]
-
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
class UnbiasedParameterGenerator (n_exact_terms, n_samples)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class UnbiasedParameterGenerator(ParameterGenerator): geom_p = 0.5 geom_p = np.log(geom_p) - np.log(1. - geom_p) def __init__(self, n_exact_terms, n_samples): super().__init__() self.sampler = GeometricSampler(self.geom_p) self.n_exact_terms = n_exact_terms self.n_samples = n_samples # store the samples of n. # self.register_buffer('last_n_samples', torch.zeros(self.n_samples)) def sample_parameters(self, training=True): n_samples = self.sampler.sample_fn(m=self.n_samples) # n_samples = sample_fn(self.n_samples) n_power_series = max(n_samples) + self.n_exact_terms if not training: n_power_series += 20 def coeff_fn(k): rcdf_term = self.sampler.rcdf_fn(k, self.n_exact_terms) return 1 / rcdf_term * sum(n_samples >= k - self.n_exact_terms) / len(n_samples) return coeff_fn, n_power_series
Ancestors
- ParameterGenerator
- torch.nn.modules.module.Module
Class variables
var call_super_init : bool
var dump_patches : bool
var geom_p
var training : bool
Methods
def sample_parameters(self, training=True)
Inherited members