-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmodel.py
99 lines (79 loc) · 2.83 KB
/
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
#!/usr/bin/env python
# coding: utf-8
import os
import logging
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
class Encoder(nn.Module):
def __init__(self, encoder_path, signal_shape=100):
super(Encoder, self).__init__()
self.signal_shape = signal_shape
self.lstm = nn.LSTM(input_size=self.signal_shape, hidden_size=20, num_layers=1, bidirectional=True)
self.dense = nn.Linear(in_features=40, out_features=20)
self.encoder_path = encoder_path
def forward(self, x):
x = x.view(1, 64, self.signal_shape).float()
x, (hn, cn) = self.lstm(x)
x = self.dense(x)
return (x)
class Decoder(nn.Module):
def __init__(self, decoder_path, signal_shape=100):
super(Decoder, self).__init__()
self.signal_shape = signal_shape
self.lstm = nn.LSTM(input_size=20, hidden_size=64, num_layers=2, bidirectional=True)
self.dense = nn.Linear(in_features=128, out_features=self.signal_shape)
self.decoder_path = decoder_path
def forward(self, x):
x, (hn, cn) = self.lstm(x)
x = self.dense(x)
return (x)
class CriticX(nn.Module):
def __init__(self, critic_x_path, signal_shape=100):
super(CriticX, self).__init__()
self.signal_shape = signal_shape
self.dense1 = nn.Linear(in_features=self.signal_shape, out_features=20)
self.dense2 = nn.Linear(in_features=20, out_features=1)
self.critic_x_path = critic_x_path
def forward(self, x):
x = x.view(1, 64, self.signal_shape).float()
x = self.dense1(x)
x = self.dense2(x)
return (x)
class CriticZ(nn.Module):
def __init__(self, critic_z_path):
super(CriticZ, self).__init__()
self.dense1 = nn.Linear(in_features=20, out_features=1)
self.critic_z_path = critic_z_path
def forward(self, x):
x = self.dense1(x)
return (x)
def unroll_signal(self, x):
x = np.array(x).reshape(100)
return np.median(x)
def test(self):
"""
Returns a dataframe with original value, reconstructed value, reconstruction error, critic score
"""
df = self.test_dataset.copy()
X_ = list()
RE = list() #Reconstruction error
CS = list() #Critic score
for i in range(0, df.shape[0]):
x = df.rolled_signal[i]
x = tf.reshape(x, (1, 100, 1))
z = encoder(x)
z = tf.expand_dims(z, axis=2)
x_ = decoder(z)
re = dtw_reconstruction_error(tf.squeeze(x_).numpy(), tf.squeeze(x).numpy()) #reconstruction error
cs = critic_x(x)
cs = tf.squeeze(cs).numpy()
RE.append(re)
CS.append(cs)
x_ = unroll_signal(x_)
X_.append(x_)
df['generated_signals'] = X_
return df