在Keras的数值数据集上使用Autoencoder

时间:2017-05-02 13:31:56

标签: dataset deep-learning keras autoencoder

我正在尝试使用Keras开发基于深度学习的入侵检测系统。

我们模拟了NORMAL网络流量,我在CSV文件中准备了它(网络数据包字段的数字数据集(IP源,端口等))。但我没有ABNORMAL(恶意)数据包来训练神经网络。

我搜索了类似的问题,我发现Autoencoder在无监督学习中是一种很好的方法,但问题是我是深度学习的新手,我只发现了这个例子https://blog.keras.io/building-autoencoders-in-keras.html,他们使用Autoencoder在图像数据集上。

我想使用自动编码器(或我的情况下有用的任何东西)和数字CSV数据集,以预测传入数据包是正常的还是恶意的。

任何建议?

2 个答案:

答案 0 :(得分:0)

我找到了答案:

您可以使用以下方法将数值数据集加载到python中numpy加载文字。然后,指定编码器和解码器网络(基本上只使用Keras层模块来设计神经网络)。确保编码器的输入层接受您的数据,并且解码器的输出层具有相同的尺寸。然后,再次使用Keras损失指定适当的损失函数(最小二乘,交叉熵等)。最后,使用(惊奇!)Keras优化器指定优化器。

多数民众赞成,你做完了!点击“运行”,然后观察你的自动编码器自动编码(因为这就是自动编码器的作用)。如果你想要一个关于如何构建它的精彩教程。

答案 1 :(得分:0)

from keras.layers import Input,Dense
from keras.models import Model

# number of neurons in the encoding hidden layer
encoding_dim = 5
# input placeholder
input_data = Input(shape=(6,)) # 6 is the number of features/columns
# encoder is the encoded representation of the input
encoded = Dense(encoding_dim, activation ='relu')(input_data)
# decoder is the lossy reconstruction of the input
decoded = Dense(6, activation ='sigmoid')(encoded) # 6 again number of features and should match input_data


# this model maps an input to its reconstruction
autoencoder = Model(input_data, decoded)




# this model maps an input to its encoded representation
encoder = Model(input_data, encoded)
# model optimizer and loss
autoencoder = Model(input_data, decoded)

# loss function and optimizer
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

# train test split
from sklearn.model_selection import train_test_split
x_train, x_test, = train_test_split(data, test_size=0.1, random_state=42)


# train the model
autoencoder.fit(x_train,
                x_train,
                epochs=50,
                batch_size=256,
                shuffle=True)

autoencoder.summary()

# predict after training
# note that we take them from the *test* set
encoded_data = encoder.predict(x_test)