text_plain_output_1.png
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import MinMaxScaler

from tensorflow.python.framework.ops import disable_eager_execution

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

df = pd.read_parquet('/kaggle/input/sampled-datasets-v2/NF-UNSW-NB15-V2.parquet')
columns_to_remove = ['L4_SRC_PORT', 'L4_DST_PORT', 'Label', 'Attack']
train, test = train_test_split(df, test_size=0.2, shuffle=True)
y_test = np.array(test['Label'], dtype=np.uint8)
train_benign_idx = train['Label'] == 0
train_attack_idx = train['Label'] == 1
train.drop(columns=columns_to_remove, axis=1, inplace=True)
test.drop(columns=columns_to_remove, axis=1, inplace=True)
train_normal = train[train_benign_idx].values
train_attack = train[train_attack_idx].values
scaler = MinMaxScaler()
train = scaler.fit_transform(train_normal)
train_attack = scaler.transform(train_attack)
train, validation = train_test_split(train, test_size=0.2)

disable_eager_execution()
original_dim = train.shape[1]
input_shape = (original_dim,)
intermediate_dim = int(original_dim / 2)
latent_dim = int(original_dim / 3)
def sample(args):
    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    epsilon = K.random_normal(shape=(batch, dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon
inputs = tf.keras.layers.Input(shape=input_shape, name='encoder_input')
x = tf.keras.layers.Dense(int(original_dim / 2), activation='relu')(inputs)
x = tf.keras.layers.Dense(int(original_dim / 3), activation='relu')(x)
z_mean = tf.keras.layers.Dense(int(original_dim / 4), name='z_mean')(x)
z_log_var = tf.keras.layers.Dense(int(original_dim / 4), name='z_log_var')(x)
z = tf.keras.layers.Lambda(sample, output_shape=(input_shape,), name='z')([z_mean, z_log_var])
encoder = tf.keras.Model(inputs, z, name='encoder')
encoder.summary()

latent_inputs = tf.keras.layers.Input(shape=(int(original_dim / 4),), name='z_sampling')
x = tf.keras.layers.Dense(int(original_dim / 3), activation='relu')(latent_inputs)
x = tf.keras.layers.Dense(int(original_dim / 2), activation='relu')(x)
outputs = tf.keras.layers.Dense(original_dim, activation='sigmoid')(x)
decoder = tf.keras.Model(latent_inputs, outputs, name='decoder')
decoder.summary()

outputs = decoder(encoder(inputs))
vae_model = tf.keras.Model(inputs, outputs, name='vae')
vae_model.summary()