-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
81 lines (59 loc) · 2.79 KB
/
models.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
import torch
import torch.nn as nn
class CNN_RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super(CNN_RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
# 1D Convolution
self.conv = nn.Conv1d(in_channels=input_size, out_channels=hidden_size, kernel_size=2)
# RNN
self.rnn = nn.RNN(input_size=hidden_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)
# Fully connected layer
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
# Input shape: (batch_size, seq_len, features)
# Permute to (batch_size, features, seq_len) for Conv1d
x = x.permute(0, 2, 1)
x = self.conv(x) # Apply convolution
x = x.permute(0, 2, 1) # Permute back to (batch_size, seq_len, features)
# Initialize hidden state
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)
# RNN forward pass
out, _ = self.rnn(x, h0)
# Extract the output at the last time step
last_outputs = out[:, -1, :] # (batch_size, hidden_size)
# Fully connected layer
out = self.fc(last_outputs)
return out
class CNN_LSTM(nn.Module):
def __init__(self, input_size, seq_len, hidden_size, num_layers, output_size):
super(CNN_LSTM, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
# Convolutional layer
self.conv1d = nn.Conv1d(in_channels=input_size, out_channels=hidden_size, kernel_size=3, padding=1)
self.relu = nn.ReLU()
self.pool = nn.MaxPool1d(kernel_size=2)
# LSTM layer
self.lstm = nn.LSTM(hidden_size, hidden_size, num_layers, batch_first=True)
# Fully connected layer
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
# Rearrange dimensions for Conv1D
x = x.permute(0, 2, 1) # (batch_size, input_size, seq_len)
# Pass through Conv1D
x = self.conv1d(x)
x = self.relu(x)
x = self.pool(x) # (batch_size, hidden_size, reduced_seq_len)
# Rearrange dimensions for LSTM
x = x.permute(0, 2, 1) # (batch_size, reduced_seq_len, hidden_size)
# Pass through LSTM
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)
out, _ = self.lstm(x, (h0, c0)) # (batch_size, reduced_seq_len, hidden_size)
# Take the last time step
out = out[:, -1, :] # (batch_size, hidden_size)
# Fully connected layer
out = self.fc(out) # (batch_size, output_size)
return out