-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrae_model.py
238 lines (186 loc) · 9.18 KB
/
vrae_model.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import numpy as np
import torch
from torch import nn, optim
from torch import distributions
from torch.utils.data import DataLoader
from torch.autograd import Variable
import os
from sklearn.base import BaseEstimator as SklearnBaseEstimator
class BaseEstimator(SklearnBaseEstimator):
def summarize(self):
return NotImplementedError
class Encoder(nn.Module):
"""
Encoder network containing enrolled LSTM/GRU
:param number_of_features: number of input features
:param hidden_size: hidden size of the RNN
:param hidden_layer_depth: number of layers in RNN
:param latent_length: latent vector length
:param dropout: percentage of nodes to dropout
:param block: LSTM/GRU block
"""
def __init__(self, number_of_features, hidden_size, hidden_layer_depth, latent_length, dropout, block = 'LSTM'):
super(Encoder, self).__init__()
self.number_of_features = number_of_features
self.hidden_size = hidden_size
self.hidden_layer_depth = hidden_layer_depth
self.latent_length = latent_length
self.block = block
if block == 'LSTM':
self.model = nn.LSTM(self.number_of_features, self.hidden_size, self.hidden_layer_depth, dropout = dropout)
elif block == 'GRU':
self.model = nn.GRU(self.number_of_features, self.hidden_size, self.hidden_layer_depth, dropout = dropout)
else:
raise NotImplementedError
def forward(self, x):
"""Forward propagation of encoder. Given input, outputs the last hidden state of encoder
:param x: input to the encoder, of shape (sequence_length, batch_size, number_of_features)
:return: last hidden state of encoder, of shape (batch_size, hidden_size)
"""
if self.block == 'LSTM':
_, (h_end, c_end) = self.model(x)
else:
h_end = self.model(x)
h_end = h_end[-1, :, :]
return h_end
class Lambda(nn.Module):
"""Lambda module converts output of encoder to latent vector
:param hidden_size: hidden size of the encoder
:param latent_length: latent vector length
"""
def __init__(self, hidden_size, latent_length):
super(Lambda, self).__init__()
self.hidden_size = hidden_size
self.latent_length = latent_length
self.hidden_to_mean = nn.Linear(self.hidden_size, self.latent_length)
self.hidden_to_logvar = nn.Linear(self.hidden_size, self.latent_length)
nn.init.xavier_uniform_(self.hidden_to_mean.weight)
nn.init.xavier_uniform_(self.hidden_to_logvar.weight)
def forward(self, cell_output):
"""Given last hidden state of encoder, passes through a linear layer, and finds the mean and variance
:param cell_output: last hidden state of encoder
:return: latent vector
"""
self.latent_mean = self.hidden_to_mean(cell_output)
self.latent_logvar = self.hidden_to_logvar(cell_output)
if self.training:
std = torch.exp(0.5 * self.latent_logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(self.latent_mean)
else:
return self.latent_mean
class Decoder(nn.Module):
"""Converts latent vector into output
:param sequence_length: length of the input sequence
:param batch_size: batch size of the input sequence
:param hidden_size: hidden size of the RNN
:param hidden_layer_depth: number of layers in RNN
:param latent_length: latent vector length
:param output_size: 2, one representing the mean, other log std dev of the output
:param block: GRU/LSTM - use the same which you've used in the encoder
:param dtype: Depending on cuda enabled/disabled, create the tensor
"""
def __init__(self, sequence_length, batch_size, hidden_size, hidden_layer_depth, latent_length, output_size, dtype, block='LSTM'):
super(Decoder, self).__init__()
self.hidden_size = hidden_size
self.batch_size = batch_size
self.sequence_length = sequence_length
self.hidden_layer_depth = hidden_layer_depth
self.latent_length = latent_length
self.output_size = output_size
self.dtype = dtype
if block == 'LSTM':
self.model = nn.LSTM(1, self.hidden_size, self.hidden_layer_depth)
elif block == 'GRU':
self.model = nn.GRU(1, self.hidden_size, self.hidden_layer_depth)
else:
raise NotImplementedError
self.latent_to_hidden = nn.Linear(self.latent_length, self.hidden_size)
self.hidden_to_output = nn.Linear(self.hidden_size, self.output_size)
self.decoder_inputs = torch.zeros(self.sequence_length, self.batch_size, 1, requires_grad=True).type(self.dtype)
self.c_0 = torch.zeros(self.hidden_layer_depth, self.batch_size, self.hidden_size, requires_grad=True).type(self.dtype)
nn.init.xavier_uniform_(self.latent_to_hidden.weight)
nn.init.xavier_uniform_(self.hidden_to_output.weight)
def forward(self, latent):
"""Converts latent to hidden to output
:param latent: latent vector
:return: outputs consisting of mean and std dev of vector
"""
h_state = self.latent_to_hidden(latent)
if isinstance(self.model, nn.LSTM):
h_0 = torch.stack([h_state for _ in range(self.hidden_layer_depth)])
decoder_output, _ = self.model(self.decoder_inputs, (h_0, self.c_0))
elif isinstance(self.model, nn.GRU):
h_0 = torch.stack([h_state for _ in range(self.hidden_layer_depth)])
decoder_output, _ = self.model(self.decoder_inputs, h_0)
else:
raise NotImplementedError
out = self.hidden_to_output(decoder_output)
return out
def _assert_no_grad(tensor):
assert not tensor.requires_grad, \
"nn criterions don't compute the gradient w.r.t. targets - please " \
"mark these tensors as not requiring gradients"
class VRAE(BaseEstimator, nn.Module):
"""Variational recurrent auto-encoder. This module is used for dimensionality reduction of timeseries
:param sequence_length: length of the input sequence
:param number_of_features: number of input features
:param hidden_size: hidden size of the RNN
:param hidden_layer_depth: number of layers in RNN
:param latent_length: latent vector length
:param batch_size: number of timeseries in a single batch
:param learning_rate: the learning rate of the module
:param block: GRU/LSTM to be used as a basic building block
:param n_epochs: Number of iterations/epochs
:param dropout_rate: The probability of a node being dropped-out
:param optimizer: ADAM/ SGD optimizer to reduce the loss function
:param loss: SmoothL1Loss / MSELoss / ReconLoss / any custom loss which inherits from `_Loss` class
:param boolean cuda: to be run on GPU or not
:param print_every: The number of iterations after which loss should be printed
:param boolean clip: Gradient clipping to overcome explosion
:param max_grad_norm: The grad-norm to be clipped
:param dload: Download directory where models are to be dumped
"""
def __init__(self, sequence_length, number_of_features, hidden_size=128, hidden_layer_depth=1, latent_length=20, learning_rate=0.005, block='LSTM', dropout_rate = 0., cuda=True, batch_size=32):
super(VRAE, self).__init__()
self.use_cuda = cuda
self.dtype = torch.FloatTensor
if self.use_cuda:
self.dtype = torch.cuda.FloatTensor
self.encoder = Encoder(number_of_features=number_of_features,
hidden_size=hidden_size,
hidden_layer_depth=hidden_layer_depth,
latent_length=latent_length,
dropout=dropout_rate,
block=block)
self.lmbd = Lambda(hidden_size=hidden_size,
latent_length=latent_length)
self.decoder = Decoder(sequence_length=sequence_length,
batch_size = batch_size,
hidden_size=hidden_size,
hidden_layer_depth=hidden_layer_depth,
latent_length=latent_length,
output_size=number_of_features,
block=block,
dtype=self.dtype)
self.sequence_length = sequence_length
self.hidden_size = hidden_size
self.hidden_layer_depth = hidden_layer_depth
self.latent_length = latent_length
# if self.use_cuda:
# self.cuda()
def forward(self, x):
"""
Forward propagation which involves one pass from inputs to encoder to lambda to decoder
:param x:input tensor
:return: the decoded output, latent vector
"""
input_traj = x['train_agent']
if self.use_cuda:
input_traj = input_traj.cuda()
cell_output = self.encoder(input_traj)
print (cell_output.shape)
latent = self.lmbd(cell_output)
print (latent.shape)
x_decoded = self.decoder(latent)
return x_decoded, latent, self.lmbd.latent_mean, self.lmbd.latent_logvar