forked from amirhosseinh77/NARX-Transformer-SoH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
95 lines (75 loc) · 3.48 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
import torch
import torch.nn as nn
class NARX_Transformer(nn.Module):
def __init__(self, feature_dim1,feature_dim2, num_attention, num_cycles, num_preds):
super(NARX_Transformer, self).__init__()
self.num_cycles = num_cycles
self.num_preds = num_preds
self.cap_linear_layer = nn.Linear(self.num_cycles-1, feature_dim2)
self.final_linear_layer = nn.Linear(feature_dim2, 1)
# self.conv_layer = nn.Conv1d(3, 512, kernel_size=16, stride=8)
self.conv_layer = nn.Conv2d(num_cycles, feature_dim1, kernel_size=3, stride=1,padding=1)
self.conv_layer2 = nn.Conv2d(feature_dim1,feature_dim2,kernel_size=3)
self.encoder_layer = nn.TransformerEncoderLayer(d_model=feature_dim2, nhead=num_attention, batch_first=True)
self.decoder_layer = nn.TransformerDecoderLayer(d_model=feature_dim2, nhead=num_attention, batch_first=True)
def forward(self, my_data, capacity):
embedded_data = self.conv_layer(my_data)
embedded_data = self.conv_layer2(embedded_data).squeeze(-1)
embedded_data = embedded_data.permute(0, 2, 1)
encoded_data = self.encoder_layer(embedded_data)
tgt = self.cap_linear_layer(capacity)
tgt = tgt.unsqueeze(1)
decoded_data = self.decoder_layer(tgt, encoded_data)
decoded_data = decoded_data.squeeze(1)
output_cap = self.final_linear_layer(decoded_data)
return output_cap
def pred_sequence(self, my_data, capacity):
pred_caps = torch.stack([capacity[:,i] for i in range(self.num_cycles-1)], axis=-1)
for cycle in range(self.num_preds):
pred = self.forward(my_data[:,cycle:cycle+self.num_cycles], pred_caps[:,-self.num_cycles+1:])
pred_caps = torch.cat([pred_caps, pred], axis=-1)
return pred_caps
class GRU_CNN(nn.Module):
def __init__(self):
super(GRU_CNN, self).__init__()
self.conv_block = nn.Sequential(
nn.Conv1d(3, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 64, kernel_size=32, padding='same'),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2)
)
self.flatten = nn.Flatten()
self.dense1 = nn.Linear(256, 64)
self.gru = nn.GRU(3, 256, batch_first=True)
self.dense2 = nn.Linear(256, 64)
self.concat = nn.Linear(128, 1)
def forward(self, input_stream):
x1 = self.conv_block(input_stream)
x1 = self.flatten(x1)
x1 = self.dense1(x1)
_, x2 = self.gru(input_stream.permute(0, 2, 1))
x2 = x2.squeeze(0)
x2 = self.dense2(x2)
combined = torch.cat((x1, x2), dim=1)
output = self.concat(combined)
return output