-
Notifications
You must be signed in to change notification settings - Fork 28
/
model_utils.py
94 lines (77 loc) · 3.15 KB
/
model_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchdiffeq import odeint, odeint_adjoint
MAX_NUM_STEPS = 1000 # Maximum number of steps for ODE solver
class ODEBlock(nn.Module):
def __init__(self, odefunc, tol=1e-3, adjoint=False):
"""
Code adapted from https://github.com/EmilienDupont/augmented-neural-odes
Utility class that wraps odeint and odeint_adjoint.
Args:
odefunc (nn.Module): the module to be evaluated
tol (float): tolerance for the ODE solver
adjoint (bool): whether to use the adjoint method for gradient calculation
"""
super(ODEBlock, self).__init__()
self.adjoint = adjoint
self.odefunc = odefunc
self.tol = tol
def forward(self, x, eval_times=None):
# Forward pass corresponds to solving ODE, so reset number of function
# evaluations counter
self.odefunc.nfe = 0
if eval_times is None:
integration_time = torch.tensor([0, 1]).float().type_as(x)
else:
integration_time = eval_times.type_as(x)
if self.adjoint:
out = odeint_adjoint(self.odefunc, x, integration_time,
rtol=self.tol, atol=self.tol, method='dopri5',
options={'max_num_steps': MAX_NUM_STEPS})
else:
out = odeint(self.odefunc, x, integration_time,
rtol=self.tol, atol=self.tol, method='dopri5',
options={'max_num_steps': MAX_NUM_STEPS})
if eval_times is None:
return out[1] # Return only final time
else:
return out
def trajectory(self, x, timesteps):
integration_time = torch.linspace(0., 1., timesteps)
return self.forward(x, eval_times=integration_time)
class Conv2dTime(nn.Conv2d):
def __init__(self, in_channels, *args, **kwargs):
"""
Code adapted from https://github.com/EmilienDupont/augmented-neural-odes
Conv2d module where time gets concatenated as a feature map.
Makes ODE func aware of the current time step.
"""
super(Conv2dTime, self).__init__(in_channels + 1, *args, **kwargs)
def forward(self, t, x):
# Shape (batch_size, 1, height, width)
t_img = torch.ones_like(x[:, :1, :, :]) * t
# Shape (batch_size, channels + 1, height, width)
t_and_x = torch.cat([t_img, x], 1)
return super(Conv2dTime, self).forward(t_and_x)
def get_nonlinearity(name):
"""Helper function to get non linearity module, choose from relu/softplus/swish/lrelu"""
if name == 'relu':
return nn.ReLU(inplace=True)
elif name == 'softplus':
return nn.Softplus()
elif name == 'swish':
return Swish(inplace=True)
elif name == 'lrelu':
return nn.LeakyReLU()
class Swish(nn.Module):
def __init__(self, inplace=False):
"""The Swish non linearity function"""
super().__init__()
self.inplace = True
def forward(self, x):
if self.inplace:
x.mul_(F.sigmoid(x))
return x
else:
return x * F.sigmoid(x)