tensorflow - 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)
解决方案
你的数据集不平衡吗?也许您的数据集中的 ddos 标签很少出现并且模型没有学习它。我认为您应该使用其他指标(例如 F1 分数)进行评估,并尝试其他机器学习模型,例如随机森林、XGboost。
推荐阅读
- npm - npm pack,包含来自monorepo中包根文件夹的文件
- php - 在插件中调用使用 Woocommerce 条件的函数的哪个操作?
- geocoding - 如何从经纬度位置找到正确的澳大利亚地方政府区域 (LGA)?
- java - 如何让我的 React Native 应用出现在 android 的分享列表中
- python - 如何从字符串中给出数据框名称,而不是通过硬编码
- java - 如何在 Java 中过滤带有度量的单词
- asp.net-mvc - 如果我在索引视图中放入表格的最后一列,为什么标签不起作用?
- javascript - 获取数据并显示在弹出组件上
- javascript - 在 HTML 上使用 JS 创建带有 href 的动态列表
- php - 虽然我的 wamp 服务器安装在 C: 驱动器中并且我正在我的 I: 驱动器中安装我的 laravel 项目这是成功部署我的项目的权利吗