File size: 2,390 Bytes
ff522d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from torch import nn
import torch.nn.functional as F


class NeuralNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        n_filters = 64
        self.conv_1 = nn.Conv1d(        1, n_filters, 8, stride=1, padding='same')
        self.conv_2 = nn.Conv1d(n_filters, n_filters, 5, stride=1, padding='same')
        self.conv_3 = nn.Conv1d(n_filters, n_filters, 3, stride=1, padding='same')
        self.conv_4 = nn.Conv1d(        1, n_filters, 1, stride=1, padding='same') # Expanding for addition

        self.conv_5 = nn.Conv1d(  n_filters, n_filters*2, 8, stride=1, padding='same')
        self.conv_6 = nn.Conv1d(n_filters*2, n_filters*2, 5, stride=1, padding='same')
        self.conv_7 = nn.Conv1d(n_filters*2, n_filters*2, 3, stride=1, padding='same')
        self.conv_8 = nn.Conv1d(  n_filters, n_filters*2, 1, stride=1, padding='same')
        
        self.conv_9  = nn.Conv1d(n_filters*2, n_filters*2, 8, stride=1, padding='same')
        self.conv_10 = nn.Conv1d(n_filters*2, n_filters*2, 5, stride=1, padding='same')
        self.conv_11 = nn.Conv1d(n_filters*2, n_filters*2, 3, stride=1, padding='same')
        self.conv_12 = nn.Conv1d(n_filters*2, n_filters*2, 1, stride=1, padding='same')

        self.classifier = nn.Linear(128, 5)
        self.log_softmax = nn.LogSoftmax(dim=1)

    def forward(self, x):
        x = x.float()
        
        # Block 1
        a = self.conv_1(x)
        a = F.relu(a)
        b = self.conv_2(a)
        b = F.relu(b)
        c = self.conv_3(b)
        shortcut = self.conv_4(x)
        output_1 = torch.add(c, shortcut)
        output_1 = F.relu(output_1)
        
        #Block 2
        a = self.conv_5(output_1)
        a = F.relu(a)
        b = self.conv_6(a)
        b = F.relu(b)
        c = self.conv_7(b)
        shortcut = self.conv_8(output_1)
        output_2 = torch.add(c, shortcut)
        output_2 = F.relu(output_2)
        
        #Block 3
        a = self.conv_9(output_2)
        a = F.relu(a)
        b = self.conv_10(a)
        b = F.relu(b)
        c = self.conv_11(b)
        shortcut = self.conv_12(output_2)
        output_3 = torch.add(c, shortcut)
        output_3 = F.relu(output_3)
        res = self.classifier(output_3.mean((2,)))
        logits = self.log_softmax(res)
        return logits


if __name__ == '__main__':
    model = NeuralNetwork()
    print(model)