Module flowcon.CNF.neural_odes.odefunc
Classes
class ODEfunc (diffeq, divergence_fn='approximate', residual=False, rademacher=False)
-
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 ODEfunc(nn.Module): def __init__(self, diffeq, divergence_fn="approximate", residual=False, rademacher=False): super(ODEfunc, self).__init__() assert divergence_fn in ("brute_force", "approximate") # self.diffeq = diffeq_layers.wrappers.diffeq_wrapper(diffeq) self.diffeq = diffeq self.residual = residual self.rademacher = rademacher if divergence_fn == "brute_force": self.divergence_fn = divergence_bf elif divergence_fn == "approximate": self.divergence_fn = divergence_approx self.register_buffer("_num_evals", torch.tensor(0.)) self.before_odeint() def before_odeint(self, e=None): self._e = e self._num_evals.fill_(0) def num_evals(self): return self._num_evals.item() def forward(self, t, states): assert len(states) >= 2 y = states[0] # increment num evals self._num_evals += 1 # convert to tensor if not torch.is_tensor(t): t = torch.tensor(t).type_as(y) else: t = t.type_as(y) batchsize = y.shape[0] # Sample and fix the noise. if self._e is None: if self.rademacher: self._e = sample_rademacher_like(y) else: self._e = sample_gaussian_like(y) with torch.set_grad_enabled(True): y.requires_grad_(True) t.requires_grad_(True) for s_ in states[2:]: s_.requires_grad_(True) dy = self.diffeq(t, y, *states[2:]) # Hack for 2D data to use brute force divergence computation. if not self.training and dy.view(dy.shape[0], -1).shape[1] == 2: divergence = divergence_bf(dy, y).view(batchsize, 1) else: if self.training: divergence = self.divergence_fn(dy, y, e=self._e).view(batchsize, 1) else: divergence = divergence_bf(dy, y, e=self._e).view(batchsize, 1) if self.residual: dy = dy - y divergence -= torch.ones_like(divergence) * torch.tensor(np.prod(y.shape[1:]), dtype=torch.float32 ).to(divergence) return tuple([dy, -divergence] + [torch.zeros_like(s_).requires_grad_(True) for s_ in states[2:]])
Ancestors
- torch.nn.modules.module.Module
Class variables
var call_super_init : bool
var dump_patches : bool
var training : bool
Methods
def before_odeint(self, e=None)
def forward(self, t, states) ‑> 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 num_evals(self)
class ODEnet (hidden_dims, input_shape, strides, conv, layer_type='concat', nonlinearity='softplus', num_squeeze=0, act_norm=False, scale_output=1)
-
Helper class to make neural nets for use in continuous normalizing flows
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class ODEnet(nn.Module): """ Helper class to make neural nets for use in continuous normalizing flows """ def __init__( self, hidden_dims, input_shape, strides, conv, layer_type="concat", nonlinearity="softplus", num_squeeze=0, act_norm=False, scale_output=1 ): super(ODEnet, self).__init__() self.act_norm = act_norm if act_norm: self.t_actnorm = ActNorm(1) self.x_actnorm = ActNorm(input_shape[0]) self.scale_output = scale_output self.num_squeeze = num_squeeze if conv: assert len(strides) == len(hidden_dims) + 1 base_layer = { "ignore": diffeq_layers.IgnoreConv2d, "hyper": diffeq_layers.HyperConv2d, "squash": diffeq_layers.SquashConv2d, "concat": diffeq_layers.ConcatConv2d, "concat_v2": diffeq_layers.ConcatConv2d_v2, "concatsquash": diffeq_layers.ConcatSquashConv2d, "blend": diffeq_layers.BlendConv2d, "concatcoord": diffeq_layers.ConcatCoordConv2d, }[layer_type] else: strides = [None] * (len(hidden_dims) + 1) base_layer = { "ignore": diffeq_layers.IgnoreLinear, "hyper": diffeq_layers.HyperLinear, "squash": diffeq_layers.SquashLinear, "concat": diffeq_layers.ConcatLinear, "concat_v2": diffeq_layers.ConcatLinear_v2, "concatsquash": diffeq_layers.ConcatSquashLinear, "blend": diffeq_layers.BlendLinear, "concatcoord": diffeq_layers.ConcatLinear, }[layer_type] # build layers and add them layers = [] activation_fns = [] hidden_shape = input_shape for dim_out, stride in zip(hidden_dims + (input_shape[0],), strides): if stride is None: layer_kwargs = {} elif stride == 1: layer_kwargs = {"ksize": 3, "stride": 1, "padding": 1, "transpose": False} elif stride == 2: layer_kwargs = {"ksize": 4, "stride": 2, "padding": 1, "transpose": False} elif stride == -2: layer_kwargs = {"ksize": 4, "stride": 2, "padding": 1, "transpose": True} else: raise ValueError('Unsupported stride: {}'.format(stride)) layer = base_layer(hidden_shape[0], dim_out, **layer_kwargs) layers.append(layer) activation_fns.append(NONLINEARITIES[nonlinearity]) hidden_shape = list(copy.copy(hidden_shape)) hidden_shape[0] = dim_out if stride == 2: hidden_shape[1], hidden_shape[2] = hidden_shape[1] // 2, hidden_shape[2] // 2 elif stride == -2: hidden_shape[1], hidden_shape[2] = hidden_shape[1] * 2, hidden_shape[2] * 2 self.layers = nn.ModuleList(layers) self.activation_fns = nn.ModuleList(activation_fns[:-1]) def forward(self, t, y): if self.act_norm: t = self.t_actnorm(t.view(-1, 1))[0].view(t.shape) y, _ = self.x_actnorm(y) dx = y # squeeze for _ in range(self.num_squeeze): dx = squeeze(dx, 2) for l, layer in enumerate(self.layers): dx = layer(t, dx) # if not last layer, use nonlinearity if l < len(self.layers) - 1: dx = self.activation_fns[l](dx) # unsqueeze for _ in range(self.num_squeeze): dx = unsqueeze(dx, 2) return dx * self.scale_output
Ancestors
- torch.nn.modules.module.Module
Class variables
var call_super_init : bool
var dump_patches : bool
var training : bool
Methods
def forward(self, t, y) ‑> 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.