Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
136 lines (129 loc) · 3.91 KB
/
Copy pathmodel.py
File metadata and controls
136 lines (129 loc) · 3.91 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import torch
from torch import nn as nn
from torch.nn import functional as F
from petastorm import make_reader
from petastorm.pytorch import DataLoader
from pathlib import Path
from pyarrow import filesystem
# base on paper in 2020
class CNN(nn.Module):
def __init__(self, batch_size=16):
super().__init__()
self.batch_size = batch_size
self.conv1 = nn.Sequential(
nn.Conv1d(in_channels=1, out_channels=200, kernel_size=4, stride=3),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv1d(in_channels=200, out_channels=100, kernel_size=5, stride=1),
nn.ReLU()
)
self.max_pooling = nn.MaxPool1d(kernel_size=2,stride=1)
# flatten, calculate the output size of max pool
# use a dummy input to calculate
# dummy_x = torch.rand(1, 1, 1500, requires_grad=False)
# dummy_x = self.conv1(dummy_x)
# dummy_x = self.conv2(dummy_x)
# dummy_x = self.max_pooling(dummy_x)
# print("dx:", dummy_x.shape)
# max_pool_out = dummy_x.view(1, -1).shape[1]
# max_pool_out = dummy_x.shape[2]
# print('mo:', max_pool_out)
self.fc0 = nn.Sequential(
# nn.Linear(49400, 600),
nn.Linear(49400, 600),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc1 = nn.Sequential(
nn.Linear(600, 500),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc2 = nn.Sequential(
nn.Linear(500, 400),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc3 = nn.Sequential(
nn.Linear(400, 300),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc4 = nn.Sequential(
nn.Linear(300, 200),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc5 = nn.Sequential(
nn.Linear(200, 100),
nn.Dropout(0.05),
nn.ReLU()
)
self.fc6 = nn.Sequential(
nn.Linear(100, 50),
nn.Dropout(0.05),
nn.ReLU()
)
self.out = nn.Sequential(
nn.Linear(50, 17),
# nn.Softmax(dim=1)
)
def forward(self, x):
# the input is in [batch_size, channel, signal_length]
# where channel is 1
# signal_length is 1500 by default
# batch_size = x.shape[0]
# input: torch.Size([256, 1, 1500])
# c1: torch.Size([256, 200, 499])
# cs: torch.Size([256, 100, 495])
# pool: torch.Size([256, 100, 247])
# view: torch.Size([256, 24700])
# fc0: torch.Size([256, 600])
# fc1: torch.Size([256, 500])
# fc2: torch.Size([256, 400])
# out: torch.Size([256, 17])
# conv1D 1
# print("input:", x.shape)
x = self.conv1(x)
# print("c1:", x.shape)
# conv1D 2
x = self.conv2(x)
# print("cs:",x.shape)
# Max Pooling
x = self.max_pooling(x)
# print("pool:",x.shape)
# Flatten
# x = torch.squeeze(x)
# x = x.reshape(self.batch_size, -1)
x = x.view(x.size(0), -1)
# print("view:", x.shape)
x = self.fc0(x)
# print("fc0:", x.shape)
# 7 FC Layer
x = self.fc1(x)
# print("fc1:", x.shape)
x = self.fc2(x)
# print("fc2:", x.shape)
x = self.fc3(x)
x = self.fc4(x)
x = self.fc5(x)
x = self.fc6(x)
# print(x.shape)
# softmax layer
x = self.out(x)
# print("out:",x.shape)
return x
# model = CNN().float()
# learning_rate = 0.05
# epoch = 200
# loss_func = F.cross_entropy()
# optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# for i in epoch:
# y_pred = model(x)
# loss = loss_func(y_pred, y)
# if i % 100 == 99:
# print(i, loss.item())
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()