File size: 2,867 Bytes
25e7dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn


class LSTMClassifier(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, dropout, output_size=1):
        super(LSTMClassifier, self).__init__()
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, dropout=dropout, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        h_0 = torch.zeros(self.lstm.num_layers, x.size(0), self.lstm.hidden_size).to(x.device)
        c_0 = torch.zeros(self.lstm.num_layers, x.size(0), self.lstm.hidden_size).to(x.device)

        out, _ = self.lstm(x, (h_0, c_0))
        out = self.fc(out[:, -1, :])
        out = torch.sigmoid_(out)
        return out


class LSTMClassifierB(nn.Module):
    def __init__(self, input_size, hidden_size, dropout, num_layers):
        super(LSTMClassifierB, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, dropout=dropout, batch_first=True)

        fc_layers = []
        input_dim = hidden_size

        fc_layers.append(nn.Linear(input_dim, input_dim))
        fc_layers.append(nn.ReLU())
        fc_layers.append(nn.Linear(input_dim, 1))  # Output layer for binary classification
        self.fc = nn.Sequential(*fc_layers)

    def forward(self, x):
        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))
        out = self.fc(out[:, -1, :])  # Only take the output from the last time step
        out = torch.sigmoid(out)  # Apply sigmoid activation
        return out


class LSTMClassifierC(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, sequence_length):
        super(LSTMClassifierC, self).__init__()
        self.hidden_size = hidden_size         # Number of features in the hidden state
        self.num_layers = num_layers           # Number of recurrent layers in the LSTM
        self.sequence_length = sequence_length # Length of the input sequences

        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)

        # Single linear layer after flattening the LSTM output
        self.fc = nn.Linear(hidden_size * sequence_length, 1)

    def forward(self, x):
        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))
        out = out.reshape(out.size(0), -1)  # Flatten the sequence
        out = self.fc(out)  # Pass through the single linear layer
        out = torch.sigmoid(out)  # Apply sigmoid activation
        return out