Keras模型无法预测预期的输出

时间:2020-05-05 09:10:56

标签: tensorflow machine-learning 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]]

纪元结束时,它会给出

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

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

我的整个代码如下:

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)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

您可以尝试将输出标签一次热编码为(0,1)和(1,0),而不是0和1。

相关问题