Module flowcon.transforms.UMNN.MonotonicNormalizer
Classes
class ELUPlus
-
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 ELUPlus(nn.Module): def __init__(self): super().__init__() self.elu = nn.ELU() def forward(self, x): return self.elu(x) + 1.
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 IntegrandNet (hidden, cond_in)
-
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 IntegrandNet(nn.Module): def __init__(self, hidden, cond_in): super(IntegrandNet, self).__init__() l1 = [1 + cond_in] + hidden l2 = hidden + [1] layers = [] for h1, h2 in zip(l1, l2): layers += [nn.Linear(h1, h2), nn.ReLU()] layers.pop() layers.append(ELUPlus()) self.net = nn.Sequential(*layers) def forward(self, x, h): nb_batch, in_d = x.shape x = torch.cat((x, h), 1) x_he = x.view(nb_batch, -1, in_d).transpose(1, 2).contiguous().view(nb_batch * in_d, -1) y = self.net(x_he).view(nb_batch, -1) return y
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, h) ‑> 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 MonotonicNormalizer (integrand_net, cond_size, nb_steps=20, solver='CC')
-
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 MonotonicNormalizer(nn.Module): def __init__(self, integrand_net, cond_size, nb_steps=20, solver="CC"): super(MonotonicNormalizer, self).__init__() if type(integrand_net) is list: self.integrand_net = IntegrandNet(integrand_net, cond_size) else: self.integrand_net = integrand_net self.solver = solver self.nb_steps = nb_steps def forward(self, x, h, context=None): x0 = torch.zeros(x.shape).to(x.device) xT = x z0 = h[:, :, 0] h = h.permute(0, 2, 1).contiguous().view(x.shape[0], -1) if self.solver == "CC": z = NeuralIntegral.apply(x0, xT, self.integrand_net, _flatten(self.integrand_net.parameters()), h, self.nb_steps) + z0 elif self.solver == "CCParallel": z = ParallelNeuralIntegral.apply(x0, xT, self.integrand_net, _flatten(self.integrand_net.parameters()), h, self.nb_steps) + z0 else: return None return z, self.integrand_net(x, h) def inverse_transform(self, z, h, context=None): # Old inversion by binary search x_max = torch.ones_like(z) * 20 x_min = -torch.ones_like(z) * 20 z_max, _ = self.forward(x_max, h, context) z_min, _ = self.forward(x_min, h, context) for i in range(25): x_middle = (x_max + x_min) / 2 z_middle, _ = self.forward(x_middle, h, context) left = (z_middle > z).float() right = 1 - left x_max = left * x_middle + right * x_max x_min = right * x_middle + left * x_min z_max = left * z_middle + right * z_max z_min = right * z_middle + left * z_min return (x_max + x_min) / 2
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, h, context=None) ‑> 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. def inverse_transform(self, z, h, context=None)