我正在尝试构建入侵检测系统(IDS),并预测标签是良性还是DDoS攻击。但在各个Epoch中,我得到的准确率始终相同。
代码:
from tensorflow import keras import numpy as np import datetime import time from tensorflow.keras.optimizers import Adam from keras.models import Sequential from keras.layers import Dense, Dropout from keras import callbacks x=pd.DataFrame(X) x = x.values sample = x.shape[0] features = x.shape[1] #Train: convert 2D to 3D for input RNN x_train = np.reshape(x,(sample,features,1)) #shape = (125973, 18, 1) #Test: convert 2D to 3D for input RNN x_test=pd.DataFrame(X_test) x_test = x_test.values x_test = np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))Model = keras.Sequential([ keras.layers.LSTM(80,input_shape=(features,x_train.shape[2]), activation='sigmoid',recurrent_activation='hard_sigmoid'), keras.layers.Dense(1,activation="softmax") ])Model.compile(optimizer='rmsprop',loss='mse', metrics=['accuracy'])#Training the modelModel.fit(x_train, y, epochs=10, batch_size= 32) Model.summary()# Final evaluation of the modelscores = Model.evaluate(x_test, y_test, verbose=0)print('/n')print("Accuracy: %.2f%%" % (scores[1]*100))Epoch 1/101074/1074 [==============================] - 92s 83ms/step - loss: 0.4180 - accuracy: 0.5820Epoch 2/101074/1074 [==============================] - 79s 74ms/step - loss: 0.4180 - accuracy: 0.5820Epoch 3/101074/1074 [==============================] - 81s 76ms/step - loss: 0.4180 - accuracy: 0.5820
解决方案是什么?
回答:
因为使用1个神经元的"softmax"
激活函数总是输出1。你的神经元无法调整其输出以降低损失;从数学上讲,它只能返回1。