I am trying to build a model which classify network attacks (DDoS or BENIGN attack). For this, I am using "ISCX 2017" dataset from https://www.unb.ca/cic/datasets/ids-2017.html. Everything goes fine until I fit the model.
I am getting this error ValueError: A target array with shape (180568, 80) was passed for an output of shape (None, 8, 80) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
I can not find any information online on how to resolve it. I am brand new to Keras and I will appreciate any guidance or advice.
Here is my code snippet
import os
import math
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler, RobustScaler
from tensorflow import keras
from numpy import mean
from numpy import std
layers = keras.layers
from keras.models import Sequential
from keras.layers import Dense, Conv1D, Conv2D, MaxPool1D, Dropout, Lambda, MaxPooling1D, Flatten, GlobalAveragePooling1D
from tensorflow.keras import datasets, layers, models
from tflearn.layers.normalization import local_response_normalization
from keras.layers import Dropout
# The next step is to split training and testing data. For this we will use sklearn function train_test_split().
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=.2)
features_train.shape, features_test.shape, labels_train.shape, labels_test.shape
((180568, 80), (45143, 80), (180568,), (45143,))
n_timesteps, n_features, n_outputs = features_train.shape[0], features_train.shape[1], labels_train.shape[0]
X_train = np.zeros((180568, 80, 1))
y_train = np.zeros((180568, 80))
n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], y_train.shape[1]
n_samples = 1000
X = np.random.uniform(0,1, (n_samples, n_timesteps, n_features))
y = pd.get_dummies(np.random.randint(0,n_outputs, n_samples)).values
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(input_shape=(n_timesteps, n_features), activation='relu', kernel_size=2, filters=32),
tf.keras.layers.MaxPooling1D(strides=3),
#tf.nn.local_response_normalization((1, 1, 1, 1), depth_radius=5, bias=1, alpha=1, beta=0.5, name=None),
tf.keras.layers.LayerNormalization(axis=1),
tf.keras.layers.Conv1D(input_shape=(n_timesteps, n_features), activation='relu', kernel_size=2, filters=64),
tf.keras.layers.MaxPooling1D(strides=3), # also GlobalMaxPooling1D() is ok
tf.keras.layers.LayerNormalization(axis=1),
tf.keras.layers.Dense(80, activation='softmax')
])
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 79, 32) 96
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 26, 32) 0
_________________________________________________________________
layer_normalization (LayerNo (None, 26, 32) 52
_________________________________________________________________
conv1d_1 (Conv1D) (None, 25, 64) 4160
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 8, 64) 0
_________________________________________________________________
layer_normalization_1 (Layer (None, 8, 64) 16
_________________________________________________________________
dense (Dense) (None, 8, 80) 5200
=================================================================
Total params: 9,524
Trainable params: 9,524
Non-trainable params: 0
______________________________
model.fit(X_train, y_train, epochs=10, verbose=1)