首页 > 解决方案 > Keras 模型无法预测预期输出

问题描述

我正在构建 Keras 模型来检测 ddos​​ 攻击。我已经用数据集训练了模型,使其具有如下的损失和准确率

x_train = [bunch of float values]
# Like below
[6.99757033e-01 5.10532289e-06 7.74600714e-06 ... 2.93996180e-05
  6.57591467e-01 2.34854095e-02]
 [4.99968509e-01 7.93600808e-03 7.93600808e-03 ... 4.99968509e-01
  0.00000000e+00 0.00000000e+00]
 [8.72862575e-01 3.66688893e-06 1.10006668e-05 ... 1.10006668e-05
  2.45681559e-04 2.45681559e-04]
 ...
 [4.99036039e-01 1.37855260e-03 1.37855260e-03 ... 4.99036039e-01
  0.00000000e+00 0.00000000e+00]

y_train = [0 or 1] 1 for ddos, 0 for benign
# Like below:
[[1.]
 [1.]
 [1.]
 ...
 [0.]
 [0.]
 [0.]]
[[1]]

当 epoch 结束时,它给出

2000000/2000000 [==============================] - 22s 11us/step - loss: 0.1930 - accuracy: 0.9381

但是,当模型预测输入数据时,它应该给出接近 1 的数字,但实际上它给出的值几乎是 0。我不确定它是如何预测错误值的,尽管它具有相当高的准确度。

我的整个代码如下:

import numpy as np
import pandas as pd
from sklearn import preprocessing

from keras.models import Sequential

model = Sequential()

from keras.layers import Dense, Dropout
from keras.optimizers import Adam

model.add(Dense(units=64, activation='relu', input_dim=10))  # Input Layer
model.add(Dropout(0.5))

model.add(Dense(units=32, activation='relu'))   # hidden Layer
model.add(Dropout(0.2))

model.add(Dense(units=32, activation='relu'))   # hidden Layer
model.add(Dropout(0.2))

model.add(Dense(units=1, activation='sigmoid'))     # Last Layer for output

model.compile(loss='binary_crossentropy',
              optimizer=Adam(learning_rate=0.001),
              metrics=['accuracy'])

CSV_FILE = "ddos.csv"
df = pd.read_csv(CSV_FILE)
df.loc[(df.Label == "ddos"), "Label"] = 1.
df.loc[(df.Label == "Benign"), "Label"] = 0.
#df=df.dropna(axis="columns", how="any")

# Data set

x_train = np.array(df[["Flow Duration", "Tot Fwd Pkts", "Tot Bwd Pkts", "TotLen Fwd Pkts",
                       "Flow IAT Mean","Flow IAT Std" ,"Flow IAT Max", "Flow IAT Min",
                       "Fwd IAT Tot", "Fwd IAT Mean"]])

x_train = x_train.astype(float)

normalized_x = preprocessing.normalize(x_train)


y_train = np.array(df[["Label"]])
y_train = np.array(y_train, dtype = 'float32')

normalized_y = preprocessing.normalize(y_train)



model.fit(normalized_x, normalized_y, epochs=2, batch_size=128)




x_test = np.array([["3974862.0", "29.0","44.0","86.0" ,"55206.42", "1.954783e+05","1566821.0", "167.0",
                    "3735347", "133405.25"]])  # ddos data

x_test = preprocessing.normalize(x_test)

classes = model.predict(x_test)
threshold_output = np.where(classes > 0.5, 1, 0)
print(threshold_output)

标签: tensorflowmachine-learningkeras

解决方案


你的数据集不平衡吗?也许您的数据集中的 ddos​​ 标签很少出现并且模型没有学习它。我认为您应该使用其他指标(例如 F1 分数)进行评估,并尝试其他机器学习模型,例如随机森林、XGboost。


推荐阅读