-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1D-CNN v7.py
236 lines (184 loc) · 6.75 KB
/
1D-CNN v7.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
from torch.autograd import Variable
import matplotlib.pyplot as plt
import numpy as np
import os
import torch.nn.functional as F
from prettytable import PrettyTable
import time
timePoint1 = time.time()
print("timePoint1 is {}.\n".format(timePoint1))
# predefine some const
EPOCH = 2
BATCH_SIZE = 200
LEARNING_RATE = 0.001
trainDIR = "C:/testData/very small test data set/training data set"
testDIR = "C:/testData/very small test data set/test data set"
modelDIR = "C:/testData/very small test data set"
modelFILE = "1D-CNNv5.pth"
modelPATH = modelDIR + '/' + modelFILE
# print the seperating line
def print_line(char,string):
print(char*33,string,char*32)
# define the neural network
class myCNN1D(nn.Module):
def num_flat_features(self,x):
size=x.size()[1:] # all dimensions except the batch dimension
num_features=1
for s in size:
num_features*=s
return num_features
def __init__(self):
super(myCNN1D, self).__init__()
self.layer1_conv = nn.Sequential(
nn.Conv1d(in_channels=1, out_channels=2, kernel_size=5, stride=1, padding=0, dilation=1, groups=1,
bias=True),
nn.ReLU(),
# nn.MaxPool1d(2)
)
self.layer2_conv = nn.Sequential(
nn.Conv1d(in_channels=2, out_channels=3, kernel_size=6, stride=1, padding=0, dilation=1, groups=1,
bias=True),
nn.ReLU()
)
self.layer3_fc1 = nn.Linear(3*5*6, 100)
self.layer4_output = nn.Linear(100, 1)
def forward(self, x):
out = self.layer1_conv(x)
out = self.layer2_conv(out)
out = out.view(-1, self.num_flat_features(out))
out = self.layer3_fc1(out)
out = self.layer4_output(out)
return out
# build a CNN
myConv = myCNN1D()
# Define Loss and Optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(myConv.parameters(), lr=LEARNING_RATE)
# prepare train data set
trainFILES = os.listdir(trainDIR)
trainMatrix = np.zeros(shape=(1,1,40))
for f in trainFILES:
if f[0]=='.':
continue
tempFILE = trainDIR + '/' + f
tempMatrix = np.loadtxt(tempFILE)
print("The shape of {0} is {1}".format(f, tempMatrix.shape))
tempMatrix = tempMatrix[:,np.newaxis]
trainMatrix = np.vstack((trainMatrix,tempMatrix))
print("The shape of trainMatrix is {}".format(trainMatrix.shape))
x_train = trainMatrix[:,:,1:]
y_train = trainMatrix[:,:,0]
y_train = torch.from_numpy(y_train).float()
x_train = torch.from_numpy(x_train).float()
trainDataSet = Data.TensorDataset(x_train, y_train)
trainLoader = Data.DataLoader(
dataset=trainDataSet, # torch TensorDataset format
batch_size=BATCH_SIZE, # mini batch size
shuffle=True, # 要不要打乱数据
#num_workers=1, # 多线程来读数据
)
timePoint2 = time.time()
print("{} seconds have passed from timePoint1.\n".format(timePoint2-timePoint1))
# Train the Model
print_line('*','Training Begin')
for epoch in range(EPOCH):
for i, (x_train, y_train) in enumerate(trainLoader):
x_train = Variable(x_train)
y_train = Variable(y_train)
# Forward + Backward + Optimize
optimizer.zero_grad()
outputs = myConv(x_train)
# y_train = y_train.type(torch.LongTensor)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
print('Epoch [{}/{}] Loss: {:.8f}'.format(epoch + 1, EPOCH, loss.data))
#开始打印表格
title = ['Training Results '+str(epoch+1)]
for i in range(5):
title.append('Sample '+str(i+1))
table = PrettyTable(title)
nn_output_list = ["Neural Network output"]
for idx,nn_output in enumerate(outputs):
if(idx == 5):
break
nn_output_list.append(np.round(np.array(nn_output.data[0]),decimals=4))
table.add_row(nn_output_list)
label_list = ["True Answer"]
for idx, label in enumerate(y_train):
if(idx == 5):
break
label_list.append(np.array(label.data[0]))
table.add_row(label_list)
print(table)
timePoint3 = time.time()
print("{} seconds have passed from timePoint2.\n".format(timePoint3-timePoint2))
# save and reload the model
torch.save(myConv, modelPATH)
reload_model = torch.load(modelPATH)
# Test the Model
print_line('*','Testing Begin')
testFILES = os.listdir(testDIR)
resultList = []
for f in testFILES:
if f[0]=='.':
continue
resultList.append(f[:7])
tempFILE = testDIR + '/' + f
tempMatrix = np.loadtxt(tempFILE)
print("The shape of {0} is {1}".format(f, tempMatrix.shape))
testMatrix = np.zeros(shape=(1,1,40))
testMatrix = tempMatrix[:,np.newaxis]
print("The shape of testMatrix is {}".format(testMatrix.shape))
x_test = testMatrix[:,:,1:]
y_test = testMatrix[:,:,0]
y_test = torch.from_numpy(y_test).float()
x_test = torch.from_numpy(x_test).float()
testDataSet = Data.TensorDataset(x_test, y_test)
testLoader = Data.DataLoader(
dataset=testDataSet, # torch TensorDataset format
batch_size=BATCH_SIZE, # mini batch size
shuffle=True, # 要不要打乱数据 (打乱比较好)
#num_workers=1, # 多线程来读数据
)
correct = 0
total = 0
for x_test, y_test in testLoader:
x_test = Variable(x_test)
outputs = reload_model(x_test)
outputs = outputs.data.squeeze()
for (output, label) in zip(outputs, y_test):
total += 1
if (output - 0) ** 2 - (output - 1) ** 2 < 0:
predicted = 0
else:
predicted = 1
if (predicted - label)**2<0.00001:
correct+=1
resultList.append(100 * correct / total)
print('Test Accuracy of the model for {0} is {1}%.'.format(f,(100 * correct / total)))
#开始打印表格
title = ['Test Results']
for i in range(5):
title.append('Sample '+str(i+1))
table = PrettyTable(title)
nn_output_list = ["Neural Network output"]
for idx,nn_output in enumerate(outputs):
if(idx == 5):
break
nn_output_list.append(np.round(np.array(nn_output.data),decimals=4))
table.add_row(nn_output_list)
label_list = ["True Answer"]
for idx, label in enumerate(y_test):
if(idx == 5):
break
label_list.append(np.array(label.data[0]))
table.add_row(label_list)
print(table)
print(resultList)
timePoint4 = time.time()
print("{} seconds have passed from timePoint3.\n".format(timePoint4-timePoint3))