-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_cnn.py
134 lines (100 loc) · 3.12 KB
/
test_cnn.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
# coding: utf-8
# In[1]:
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.preprocessing import sequence
from keras.optimizers import RMSprop
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Convolution1D, MaxPooling1D
# In[84]:
def transIP(ip):
tmp = ip.split(".")
res = ""
for str in tmp:
if len(str) == 1:
str = "00" + str
else :
if len(str) == 2:
str = "0" + str
res += str
return int(res)/1000000
# In[89]:
# set parameters:
max_features = 900000
maxlen = 4
batch_size = 32
embedding_dims = 100
nb_filter = 250
filter_length = 3
hidden_dims = 250
nb_epoch = 3
# In[90]:
import numpy as np
x_train = []
y_train = []
x_test = []
y_test = []
counter = 0;
data = open("csv/output.csv", "r")
for line in data:
tmp = line.split(',')
#print(tmp[0])
tmp[0] = int(float(tmp[0]) * 100)
#print(tmp[0])
tmp[1] = transIP(tmp[1])
tmp[2] = transIP(tmp[2])
x_train.append(tmp[0:3])
y_train.append(1)
counter += 1
if(counter > 500):break
counter = 0
for line in data:
tmp = line.split(',')
tmp[0] = int(float(tmp[0]) * 100)
tmp[1] = transIP(tmp[1])
tmp[2] = transIP(tmp[2])
x_test.append(tmp[0:3])
y_test.append(1)
counter += 1
if(counter > 100):break
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print(x_train[1])
print("Pad sequences (samples x time)")
X_train = sequence.pad_sequences(x_train, maxlen=maxlen)
X_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
# In[91]:
print(X_train[1])
# In[92]:
print('Build model...')
model = Sequential()
# we start off with an efficient embedding layer which maps
# our vocab indices into embedding_dims dimensions
model.add(Embedding(max_features, embedding_dims, input_length=maxlen))
model.add(Dropout(0.25))
# we add a Convolution1D, which will learn nb_filter
# word group filters of size filter_length:
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length,
border_mode="valid",
activation="relu",
subsample_length=1))
# we use standard max pooling (halving the output of the previous layer):
model.add(MaxPooling1D(pool_length=2))
# We flatten the output of the conv layer, so that we can add a vanilla dense layer:
model.add(Flatten())
# We add a vanilla hidden layer:
model.add(Dense(hidden_dims))
model.add(Dropout(0.25))
model.add(Activation('relu'))
# We project onto a single unit output layer, and squash it with a sigmoid:
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', class_mode="binary")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, validation_data=(X_test, y_test))