-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvd_ai.py
175 lines (129 loc) · 3.65 KB
/
nvd_ai.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
import os
import tensorflow as tf
import shutil
import json
import random
import datetime
import nvd_helper
import importlib
def helper_reload():
importlib.reload(nvd_helper)
# model_type='int' 'string' 'bool'
# put everything in classes
# class for raw nvd
# class for datasets and preprocessing (int and string superclasses)
# class for model
string=['cvssv3_properties']
label=['cvssv3_base_score']
nvd_properties=(string, label)
directory='./raw_nvd/'
modelstr='nvd'
for i in nvd_properties:
for j in i:
modelstr+='_'+j
nvd=nvd_helper.load_json_nvd(directory)
cve_item=nvd['nvdcve-1.1-2019.json']['CVE_Items'][0]
items_vector=nvd_helper.extract(nvd, nvd_properties)
# labelmap=list(set([i[1] for i in items_vector]))
labelmap=list(set([i[1][0] for i in items_vector]))
labelmap={labelmap[i]:i for i in range(len(labelmap))}
print(labelmap)
dataset_blncd=nvd_helper.balance(labelmap, items_vector)
"""
legacy code
legacy = tf.keras.preprocessing.text_dataset_from_directory(
F"NVD_severity/train",
label_mode='int',
batch_size=32,
validation_split=0.2, # 80% will be used for training, 20% will be used for validation
subset='training',
seed=42)
"""
# variables/settings
batch_size= 32
shuffle_size= 5000
max_features= 5000
embedding_dim= 64
epoch_num= 3
# make dataset
# average length of strings
def avgLen(x): return len(x[0])
ds_len=list(map(avgLen, dataset_blncd))
train_ds, val_ds, test_ds = nvd_helper.make_ds(dataset_blncd, batch_size, shuffle_size)
# preprocess
train_ds, val_ds, test_ds = nvd_helper.preprocess(train_ds, val_ds, test_ds, batch_size, ds_len, max_features)
# compile the model
model = tf.keras.Sequential([
tf.keras.layers.Embedding(max_features + 1, embedding_dim), # embedding layer creates an efficient, dense representation in which similar words have a similar encoding, improves through training
tf.keras.layers.Dropout(0.2),
tf.keras.layers.GlobalAveragePooling1D(), # reduces the length of each input vector to the average sequence length of all vectors
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(len(labelmap.keys()))])
model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer='adam',
metrics=['categorical_accuracy'])
# model.summary()
history=model.fit(
train_ds,
epochs=epoch_num,
validation_data=val_ds)
# import instead of train
# model = tf.keras.models.load_model('nvd_sev.h5')
# evaluate the model
loss, accuracy = model.evaluate(test_ds)
print("Loss: ", loss)
print("Accuracy: ", accuracy)
# export the model
# includes the TextVectorization layer in the model
def export():
export_model = tf.keras.Sequential([
vectorize_layer,
model,
layers.Activation('sigmoid')])
export_model.compile(
loss=losses.BinaryCrossentropy(from_logits=False), optimizer="adam", metrics=['accuracy'])
return export_model
# save model
model.save(F'{modelstr}.h5')
"""
legacy code
# manually confirm accuracy
highPred=export_model.predict(class2)
medPred=export_model.predict(class1)
lowPred=export_model.predict(class0)
numItems=len(highPred)
stats=[0,0,0]
for i in highPred:
if i[0] == max(i):
stats[0]+=1
for i in medPred:
if i[1] == max(i):
stats[1]+=1
for i in lowPred:
if i[2] == max(i):
stats[2]+=1
avg_p=sum(stats)/3/14672
"""
"""
Settings results
0.3689
3 epoch
max_features = 5000
embedding_dim = 64
0.3130
3 epoch
max_features = 10000
embedding_dim = 128
0.3418
3 epoch
max_features= 100000
embedding_dim= 512
0.3703
3 epoch
max_features = 5000
embedding_dim = 64
2x additional 64 nodes
Todo
"""