forked from TangneyT/RepresentationLearning_FRBs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.py
More file actions
662 lines (492 loc) · 22.8 KB
/
Network.py
File metadata and controls
662 lines (492 loc) · 22.8 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 10 11:10:13 2023
@author: tesstangney
"""
'''
Causal, Exponentially Dilated Variational Auto Encoder,
adapted from Franceschi et al. 2019
https://github.com/White-Link/UnsupervisedScalableRepresentationLearningTimeSeries/tree/master
'''
# MIT License
#
# Copyright (c) [2023] [Tess Tangney]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Encoder classes adapted from Franceschi et al. 2019
# https://github.com/White-Link/UnsupervisedScalableRepresentationLearningTimeSeries/tree/master
# Created with the following license
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Implementation of causal CNNs partly taken and modified from
# https://github.com/locuslab/TCN/blob/master/TCN/tcn.py, originally created
# with the following license.
# MIT License
# Copyright (c) 2018 CMU Locus Lab
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import numpy
import torch
import pandas as pd
import matplotlib as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#%%
class Chomp1d(torch.nn.Module):
"""
Removes the last elements of a time series.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L - s`) where `s`
is the number of elements to remove.
@param chomp_size Number of elements to remove.
"""
def __init__(self, chomp_size):
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size]
class SqueezeChannels(torch.nn.Module):
"""
Squeezes, in a three-dimensional tensor, the third dimension.
"""
def __init__(self):
super(SqueezeChannels, self).__init__()
def forward(self, x):
return x.squeeze(2)
class CausalConvolutionBlock(torch.nn.Module):
"""
Causal convolution block, composed sequentially of two causal convolutions
(with leaky ReLU activation functions), and a parallel residual connection.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L`).
@param in_channels Number of input channels.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
@param dilation Dilation parameter of non-residual convolutions.
@param final Disables, if True, the last activation function.
"""
def __init__(self, in_channels, out_channels, kernel_size, dilation,
final=False):
super(CausalConvolutionBlock, self).__init__()
# Computes left padding so that the applied convolutions are causal
padding = (kernel_size - 1) * dilation
# First causal convolution
conv1 = torch.nn.utils.weight_norm(torch.nn.Conv1d(
in_channels, out_channels, kernel_size,
padding=padding, dilation=dilation
))
# The truncation makes the convolution causal
chomp1 = Chomp1d(padding)
relu1 = torch.nn.LeakyReLU()
# Second causal convolution
conv2 = torch.nn.utils.weight_norm(torch.nn.Conv1d(
out_channels, out_channels, kernel_size,
padding=padding, dilation=dilation
))
chomp2 = Chomp1d(padding)
relu2 = torch.nn.LeakyReLU()
# Causal network
self.causal = torch.nn.Sequential(
conv1, chomp1, relu1, conv2, chomp2, relu2
)
# Residual connection
'''They are used to allow gradients to flow through a network
directly, without passing through non-linear activation functions. '''
self.upordownsample = torch.nn.Conv1d(
in_channels, out_channels, 1
) if in_channels != out_channels else None
# Final activation function
self.relu = torch.nn.LeakyReLU() if final else None
def forward(self, x):
print(numpy.shape(x))
out_causal = self.causal(x)
print('out_caual', numpy.shape(out_causal))
res = x if self.upordownsample is None else self.upordownsample(x)
if self.relu is None:
return out_causal + res
else:
return self.relu(out_causal + res)
class CausalCNN(torch.nn.Module):
"""
Causal CNN, composed of a sequence of causal convolution blocks.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C_out`, `L`).
@param in_channels Number of input channels.
@param channels Number of channels processed in the network and of output
channels.
@param depth Depth of the network.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
"""
def __init__(self, in_channels, channels, depth, out_channels,
kernel_size):
super(CausalCNN, self).__init__()
layers = [] # List of causal convolution blocks
dilation_size = 1 # Initial dilation size
for i in range(depth):
in_channels_block = in_channels if i == 0 else channels
layers += [CausalConvolutionBlock(
in_channels_block, channels, kernel_size, dilation_size
)]
dilation_size *= 2 # Doubles the dilation size at each step
# Last layer
layers += [CausalConvolutionBlock(
channels, out_channels, kernel_size, dilation_size, final = True
)]
self.network = torch.nn.Sequential(*layers)
def forward(self, x):
return self.network(x)
class CausalCNNVariationalEncoder(torch.nn.Module):
"""
Encoder of a time series using a causal CNN: the computed representation is
the output of a fully connected layer applied to the output of an adaptive
max pooling layer applied on top of the causal CNN, which reduces the
length of the time series to a fixed size.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`).
Addition: Reparametrised to make the latent space variational
@param in_channels Number of input channels.
@param channels Number of channels manipulated in the causal CNN.
@param depth Depth of the causal CNN.
@param reduced_size Fixed length to which the output time series of the
causal CNN is reduced.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
"""
def __init__(self, in_channels, channels, depth, reduced_size,
out_channels, kernel_size):
super(CausalCNNVariationalEncoder, self).__init__()
causal_cnn = CausalCNN(
in_channels, channels, depth, reduced_size, kernel_size
)
reduce_size = torch.nn.AdaptiveMaxPool1d(1)
squeeze = SqueezeChannels() # Squeezes the third dimension (time)
self.mu = torch.nn.Linear(reduced_size, out_channels)
self.log_var = torch.nn.Linear(reduced_size, out_channels)
self.kl = 0 # Keep track of KL Divergence
self.network = torch.nn.Sequential(
causal_cnn, reduce_size, squeeze
)
print("ENCODER", self.network)
def forward(self, x):
x = self.network(x)
mu = self.mu(x)
sigma = torch.exp(self.log_var(x))
z = mu + sigma*torch.randn_like(sigma) # Reparametrisation
self.kl = (sigma**2 + mu**2 - torch.log(sigma) - 1/2).sum() # KL divergence
return z, mu
#%%
'''DECOCER'''
class DeChomp1d(torch.nn.Module):
"""
Removes the last elements of a time series.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L - s`) where `s`
is the number of elements to remove.
@param chomp_size Number of elements to remove.
"""
def __init__(self, chomp_size):
super(DeChomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[: , : , self.chomp_size:]
class UnSqueezeChannels(torch.nn.Module):
"""
Expandes to a thrid dimension
"""
def __init__(self):
super(UnSqueezeChannels, self).__init__()
def forward(self, x):
return x.unsqueeze(2)
class TransposeCausalConvolutionBlock(torch.nn.Module):
"""
Causal convolution block, composed sequentially of two causal convolutions
(with leaky ReLU activation functions), and a parallel residual connection.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L`).
@param in_channels Number of input channels.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
@param dilation Dilation parameter of non-residual convolutions.
@param final Disables, if True, the last activation function.
"""
def __init__(self, in_channels, out_channels, kernel_size, dilation,
final=False):
super(TransposeCausalConvolutionBlock, self).__init__()
# Computes padding to be half the dilation
padding = int(dilation/2)
# First causal deconvolution
self.deconv1 = torch.nn.utils.weight_norm(torch.nn.ConvTranspose1d(
in_channels, out_channels, kernel_size=3,
padding=padding, dilation=dilation
))
# The truncation makes the deconvolution causal
self.chomp1 = DeChomp1d(2*padding)
self.relu1 = torch.nn.LeakyReLU()
# Second causal deconvolution
self.deconv2 = torch.nn.utils.weight_norm(torch.nn.ConvTranspose1d(
out_channels, out_channels, kernel_size=3,
padding=padding, dilation=dilation
))
self.chomp2 = DeChomp1d(2*padding)
# Residual connection
'''They are used to allow gradients to flow through a network
directly, without passing through non-linear activation functions. '''
self.upordownsample = torch.nn.ConvTranspose1d(
in_channels, out_channels, 1
) if in_channels != out_channels else None
# Final activation function
self.relu = None if final else torch.nn.LeakyReLU()
self.causal = torch.nn.Sequential(self.deconv1, self.chomp1, self.relu1, self.deconv2, self.chomp2)
def forward(self, x):
out_causal = self.causal(x)
res = x if self.upordownsample is None else self.upordownsample(x)
if self.relu is None:
return out_causal + res
else:
return self.relu(out_causal + res)
class CausalDeCNN(torch.nn.Module):
"""
Causal DeCNN, composed of a sequence of causal deconvolution blocks.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C_out`, `L`).
@param in_channels Number of input channels.
@param channels Number of channels processed in the network and of output
channels.
@param depth Depth of the network.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
"""
def __init__(self, in_channels, channels, depth,
out_channels, kernel_size):
super(CausalDeCNN, self).__init__()
layers = [] # List of causal convolution blocks
dilation_size = 2**(depth+1) # Initial dilation size
for i in range(depth):
in_channels_block = in_channels if i == 0 else channels
layers += [TransposeCausalConvolutionBlock(
in_channels_block, channels, kernel_size, dilation_size
)]
dilation_size = int(dilation_size/2) # Halves the dilation size at each step
# Last layer
layers += [TransposeCausalConvolutionBlock(
channels, 1, kernel_size, dilation_size, final=True
)]
self.network = torch.nn.Sequential(*layers)
def forward(self, x):
return self.network(x)
class CausalCNNDecoder(torch.nn.Module):
"""
De-encoder of a time series using a causal DeCNN: the computed representation is
the output of a fully connected layer applied to the output of an adaptive
max pooling layer applied on top of the causal CNN, which reduces the
length of the time series to a fixed size.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`).
@param in_channels Number of input channels.
@param channels Number of channels manipulated in the causal CNN.
@param depth Depth of the causal CNN.
@param reduced_size Fixed length to which the output time series of the
causal CNN is reduced.
@param out_channels Number of output channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
ADDITION data_length : The length of the timeseries data so that the
decoder knows what length of timesereis to recosntruct
"""
def __init__(self, in_channels, channels, depth, reduced_size,
out_channels, kernel_size,data_length):
super(CausalCNNDecoder, self).__init__()
self.causal_cnn = CausalDeCNN(
in_channels, channels, depth,
out_channels, kernel_size)
# len of data
self.increase_size = torch.nn.AdaptiveMaxPool1d(data_length)
self.unsqueeze = UnSqueezeChannels() # Adds 3rd dimension
# linear_in, linear_out
self.linear = torch.nn.Linear(out_channels, in_channels) # fliped dimensions
self.network = torch.nn.Sequential(self.linear, self.unsqueeze,
self.increase_size, self.causal_cnn)
def forward(self, x):
return self.network(x)
#%%
class Causal_VAE(torch.nn.Module):
"""Combins the Encoder and Decoder classes to form a variational
Auto-Encoder
Returns
-------
self.decoder(z) : Tensor
Lightcurve generated by the decoder
mu : float
The encoded mu value which was used to reparametrise
"""
def __init__(self, depth, latent_vars, data_length):
"""Constructor
Parameters
----------
nhidden : int
The number of nodes in the hidden layer
latent_vars : int
The number of latent variables to encode the timeseries to
"""
super(Causal_VAE, self).__init__()
self.encoder = CausalCNNVariationalEncoder(in_channels = 1, channels=40, reduced_size=60,
depth=depth, out_channels=latent_vars, kernel_size=3)
self.decoder = CausalCNNDecoder(in_channels=60, channels=40, depth=depth,
reduced_size=1, out_channels=latent_vars,
kernel_size=3, data_length=data_length)
def forward(self, x):
z, mu = self.encoder(x)
return self.decoder(z), mu
#%%
''' Training '''
class Dataset(torch.utils.data.Dataset):
"""
PyTorch wrapper for a numpy dataset.
@param dataset Numpy array representing the dataset.
"""
def __init__(self, dataset):
self.dataset = dataset
def __len__(self):
return numpy.shape(self.dataset)[0]
def __getitem__(self, index):
return self.dataset[index]
def validation_minibatch(model, test_data):
"""Calculates the loss function of the model
Shuffles the data and takes a batch of 32 to validate the network using
the ELBO loss function
Parameters
----------
model : class
Variational Autoencoder
data : Tensor
timeseries data of length 85
Returns
-------
int, the normalised loss of the network
"""
test_torch_dataset = Dataset(test_data)
test_generator = torch.utils.data.DataLoader(test_torch_dataset,
batch_size=32, shuffle=True)
model.eval()
val_loss = 0
with torch.no_grad():
for i, batch in enumerate(test_generator):
recon, enco = model(batch.to(device))
loss = ((batch.to(device)-recon)**2).sum() + model.encoder.kl # ELBO Loss function
val_loss += loss.item()
return val_loss/(i+1)
def Train(DATA, MODEL, epochs=200):
"""Trains the MODEL using minibatches
Divids the data in to 80% training and 20% validation sets,
Shuffles the training data and takes a batch of 32 to train the network
using the ELBO loss function. Calls the validation_minibatch function and
passing the current model and testing set.
Parameters
----------
MODEL : class
Variational Autoencoder
DATA : Tensor
The full dataset of timeseries of length 85
epochs : int
default to 200, indicated the number of epochs to train for
Returns
-------
MODEL : object
The trained network
loss_hist : list
The loss of the training set for each epoch
val_loss : list
The loss of the validation set for each epoch
"""
length = len(DATA) # To split the data 80:20, train:test
train = DATA[:int(length*0.8)].astype(numpy.float32)
test = DATA[int(length*0.8):].astype(numpy.float32)
train_torch_dataset = Dataset(train)
train_generator = torch.utils.data.DataLoader(train_torch_dataset,
batch_size=32, shuffle=True)
MODEL = MODEL.to(device)
MODEL.train()
loss_hist = []
val_loss = []
optimizer = torch.optim.Adam(MODEL.parameters(), lr=1e-3) #.zero_grad()
for epoch in range(epochs):
print('Epoch: ', epoch)
cum_loss = 0
for i, batch in enumerate(train_generator):
optimizer.zero_grad()
recon, enco = MODEL(batch.to(device))
loss = ((batch.to(device)-recon)**2).sum() + MODEL.encoder.kl # ELBO Loss function
loss.backward()
cum_loss +=loss.item()
optimizer.step()
print('Loss: ', loss.item())
loss_hist.append(cum_loss/i) # Divide the cumulative loss by the number of batches
val_loss.append(validation_minibatch(MODEL, test))
return MODEL, loss_hist, val_loss
#%%
''' Example of how to train the network '''
filepath = "Path to dataset "
df = pd.read_csv(filepath, sep=" ", header=None)
# remove any headers or columns as needed then transform to numpy array
timeseries = df.to_numpy()
# Normalising the Data with sklearn
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
timeseries_norm = scaler.fit_transform(timeseries)
# Expand to 3 dimensions
timeseries_3d = numpy.expand_dims(timeseries_norm[:,:],1)
# Call the training function
trained_model , loss, validation_loss = Train(DATA = timeseries_3d,
MODEL=Causal_VAE(depth=4,
latent_vars = 6,
data_length=85),
epochs = 30)