-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIDS.py
141 lines (104 loc) · 3.75 KB
/
NIDS.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
import torch
from torch import nn
import numpy as np
from sklearn.model_selection import train_test_split
import gzip
batch = 34
batchTest = 21
def PreProcessing(f):
data = []
dataLabels = []
for line in f:
x = line.decode().strip()
x = x.split(',')
temp = []
if x[len(x)-1]== 'normal.':
x[len(x)-1] = '1.0'
else :
x[len(x)-1] = '0.0'
for i in x:
if i[0].isdigit():
temp.append(float(i))
elif i.isdecimal():
temp.append(float(i))
else:
temp2 = 0;
for j in i:
temp2 = temp2 + ord(j)
temp.append(temp2)
dataLabels.append(int(temp[-1]))
temp = temp[:-1]
data.append(temp)
return data, dataLabels
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.linear1 = nn.Linear(41,25).double()
self.relu1 = nn.ReLU()
self.linear2 = nn.Linear(25,10).double()
self.relu2 = nn.ReLU()
self.linear3 = nn.Linear(10,2).double()
def forward(self, x):
x = self.linear1(x)
x = self.relu1(x)
x = self.linear2(x)
x = self.relu2(x)
x = self.linear3(x)
return x
model = NeuralNetwork()
def train_loop(trainingNu,trainingLabelNu, model, loss_fn, optimizer):
for i in range(0,len(trainingNu),batch):
res = torch.empty((batch,2))
label = torch.empty(batch, dtype=torch.long)
for j in range(0,batch):
if (j+i<len(trainingNu)):
pred = model(trainingNu[j+i])
res[j] = pred
label[j] = trainingLabelNu[j+i]
else:
continue
loss = loss_fn(res, label)
# Backpropagation - 'timwria' neurwnikou. DEN allazoun autew oi grammes
optimizer.zero_grad()
loss.backward()
optimizer.step()
def test_loop(testNu,testLabelNu, model, loss_fn):
test_loss, correct = 0, 0
with torch.no_grad():
y = 0
for i in range(0,len(testNu),batchTest):
res = torch.empty((batchTest,2))
label = torch.empty(batchTest, dtype=torch.long)
y = y + 1
for j in range(0,batchTest):
if (j+i<len(testNu)):
pred = model(testNu[j+i])
res[j] = pred
label[j] = testLabelNu[j+i]
else:
continue
test_loss += loss_fn(res,label).item()
correct += (res.argmax(1) == label).type(torch.float).sum().item()
correct /= len(testNu)
test_loss /=y
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
#training data
with gzip.open('kddcup.data_10_percent.gz', 'rb') as f:
data, dataLabels = PreProcessing(f)
Nu = np.array(data)
LabelNu = np.array(dataLabels)
X_train, X_test, y_train, y_test = train_test_split( Nu, LabelNu, test_size=0.20, random_state=42)
X_train = torch.from_numpy(X_train)
y_train = torch.from_numpy(y_train)
X_test = torch.from_numpy(X_test)
y_test = torch.from_numpy(y_test)
# Initialize the loss function
learning_rate = 10**(-3)
epochs = 10
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train_loop(X_train,y_train , model, loss_fn, optimizer)
test_loop(X_test, y_test, model, loss_fn)
print("Done!")