$$\rightleftharpoonup{xx}$$
$$\longleftharp{xx}$$,
$$\longrightharp{xx}$$,
This section discusses attack detection system-related research materials and methods in the IoT environment. In order to replicate the experiments on the CIC-IoT-2023 dataset, this protocol outlines specific procedures. The subsequent Table of Materials contains a list of all the hardware and software used. Python (pandas, scikit-learn, and TensorFlow Keras) is used to implement the pipeline, which is run on Google Colab with PySpark available for large-file preprocessing. A detailed description of the data preprocessing procedures has been provided.
Dataset
The CIC-IoT-2023 dataset, which was released by the University of New Brunswick's Canadian Institute for Cybersecurity. "CIC IoT dataset 2023" (UNB CIC datasets page) and the dataset article34 are the official CIC dataset page and paper. The dataset can be downloaded from the CIC website and is accessible to the general public. Instead of using raw PCAPs, the pre-extracted feature CSV files (flows/features) are used in this research. Wireshark/mergecap and CICFlowMeter are used in the raw experiments that generated the dataset; featured CSVs are used in this work. This dataset is derived from real IoT devices, is used in this research. Included in the data set are records from 33 known attacks on 105 different IoT devices. Unlike previous IoT datasets, CIC-IoT-2023 includes a wide variety of attack types. The amount of each label linked to benign traffic is shown in Figure 1. A total of 46 attributes and 1 label make up this dataset. Compared to CSE-CIC-IDS 2018, which had 84 features, CIC-IoT-2023 has 37 fewer features.

Figure 1: Count of attacks in the CICIoT2023 dataset. The count names of different types of attacks and benign records in the CICIot2023 dataset are described. These different types of attacks are broadly categorized into 7 attack classes. So there are a total of 8 classes, including benign in multiclass classification. Please click here to view a larger version of this figure.
Data preprocessing
Datasets should not be utilized in deep learning algorithms without sufficient preprocessing. Preprocessing is done to provide the algorithm with finer data, ultimately making the model more efficient. The output of the preprocessing pipeline-after label encoding, feature scaling, and feature selection-is termed "Final Data" and serves as the input to both CNN and Transformer components. The following steps are in Google Colab using Python. This research used Python 3.8.10, pandas 1.3.4, NumPy 1.21.4, scikit-learn 1.0.2, matplotlib 3.4.3, and TensorFlow/Keras 2.6.0 . The random seed was set to 42 for all runs.
Data acquisition
This step includes cleaning the data acquired from real-world contexts, since it often contains many errors and inconsistencies. If the dataset contains text values, for instance, it is impossible to utilise such values in deep learning training without first converting them to numerical form. When working with the dataset, the first thing that is done is to remove blank values and delete cells with no data in them. Rows with missing data were eliminated to avoid any adverse effects on the model.
The CIC-IoT-2023 dataset is loaded using pd.read_csv('ciciot2023_features_part.csv'). This research used Google Colab for model training and PySpark for initial cleaning. To detect missingness, the following methods are used: df.shape, df.info(), df.isnull().sum(), and df.duplicated().sum() for duplicates. Duplicates are eliminated using df.drop_duplicates(inplace=True). Numeric column type is verified using the formula df[col] = pd.to_numeric(df[col], errors='coerce'). If new NaNs appear, missing-value handling is reapplied. Features that are constant or nearly constant are also eliminated using df.drop(columns=low_variance_cols, inplace=True).
Label encoding
The next step is to transform the text labels into a numerical representation so the model can understand them. For binary classification, there are two separate kinds of labels. There are 46,686,579 records, with 45,588,384 labeled as malicious attacks. With 1,098,195 records included, the benign label is set to 0. There are seven separate types of attacks in multiclass classification. There are eight labels altogether, including the benign traffic. Figure 2 shows the count of each category of these labels. Multiclass mapping used in this research is: 0 for Benign, 1 for DoS, 2 for DDoS, 3 for recon, 4 for web-based attack, 5 for spoofing, 6 for bruteForce, and 7 for mirai attack.

Figure 2: The percentage of attack classes including benign traffic. There are seven separate types of attacks in multiclass classification. There are eight labels altogether, including the benign traffic. The figure shows the count of each category of these labels. Please click here to view a larger version of this figure.
Feature scaling
Feature scaling is a common method for enhancing the overall performance of deep learning models. Feature scaling in this study is accomplished using Min Max Scaler and Standard Scaler technologies. The research did not use them for testing; instead, it only used the scaler on the training set to prevent data leakage. Data may be normalized to a zero-mean, one-standard-deviation distribution with the help of the Standard Scaler method. One way to do this is to divide the original number by the standard deviation. For this StandardScaler() function is called by importing StandardScaler from sklearn.preprocessing. By applying a range, often between 0 and 1, the Min Max Scaler normalizes the data. MinMaxScaler() function from scikit-learn was used to implement this.
As indicated in Eq. (1), the normalizing formula is:
(1)
Where X stands for the original point value and Xnormalized for its transformed form, Xmin represents the lowest value of the variable, while Xmax denotes the highest value of the variable within the data set.
Feature selection
Incorporating all characteristics from large datasets into training is not always helpful. Features inside the dataset may exhibit correlation and may not enhance the outcome. Furthermore, an excessive number of values escalates the expense of training. To find features that are not needed, we calculate their correlation matrices and exclude the ones with strong correlations from the dataset. Pearson's correlation coefficient, which quantifies the linear relationship between two features, is used to compute the correlation. A value between -1 and +1 is obtained by dividing the covariance of the two variables by the product of their standard deviations.This is optional and performed in the pipeline with Recursive Feature Elimination (RFE) for feature selection with a decision tree classifier. Recursive feature elimination repeatedly finds the most relevant features by training the classifier model and discarding the least significant ones. The code utilises RFE from sklearn.feature_selection with a Decision Tree Classifier as the estimator. Following the implementation of recursive feature elimination, 30 of the 46 characteristics have been picked for each of the seven attack categories, as shown in Table 2. A feature may be categorised under many attack classifications. Following the completion of the process outlined in Figure 3, the process of selecting the attack classes included within the dataset is carried out. Modern CNNs excel at learning hierarchical representations from raw data, but pre-selecting a small, high-quality feature set using RFE has significant advantages. First, RFE with a Decision Tree classifier quickly removes noisy or redundant data that might delay convergence during early training cycles. Second, centring the CNN on a smaller pool of actually valuable signals reduces overfitting, which is crucial in IoT traffic datasets with highly skewed benign and harmful patterns. The decision tree's recursive elimination method ranks features for better model explainability and identifying domain-specific biases before deep training. Finally, initializing the CNN-Transformer on this pruned feature set yields faster convergence and lower GPU memory use, proving that RFE actively accelerates and stabilizes end-to-end feature learning.
Table 2: The selected features of the dataset "CIC-IoT-2023". 30 of the 46 characteristics have been picked for each of the seven attack categories in this research using RFE. The table describes the selected 30 features. Please click here to download this Table.

Figure 3: Working of the proposed hybrid model. The completion of the process is outlined in the figure. The process of selecting the attack classes included within the dataset is carried out. Please click here to view a larger version of this figure.
Split the dataset into an 80% train and a 20% test set by following the step: train_test_split (X_train, y_train, test_size=0.20, random_state=42). The 80% training set is further split using train_test_split(X_train, y_train, test_size=0.20,random_state=42). All model selection and hyperparameter tuning are done using these precise splits. The synthetic minority oversampling technique (SMOTE) is used when there is a class imbalance problem. Only the training set was subjected to SMOTE following splitting and scaling. The effect of SMOTE is presented in the results. Following RFE, the study parallels the CNN and Transformer branches using the same 30 features that were chosen for each sample. The further details of processing these 30 features are mentioned in the proposed methodology section.
Proposed methodology
An effective security framework for IoT systems that can more accurately detect attacks will be built with the help of a deep learning model. A hybrid security strategy that combines convolutional neural networks (CNNs) with transformer models is proposed. This method entails training the CNN architecture using weights from a bigger dataset and then fine-tuning the parameters of the model using a smaller dataset as a goal. The primary goal of this model is to improve the efficiency of intrusion detection in the IoT.
This research used PySpark, a Google Colab platform that allows users to run Python programs on Apache Spark. Using the Scikit-learn and Keras packages, deep learning algorithms have been developed. To train and test the model, the following configuration was used: Operating system: Mac OS v12.6; - M2 Apple Silicon; - 13.3-inch display; - 8-core CPU; - 8-core GPU; - 32 GB RAM; - 256 GB SSD.
The Canadian Institute for Cybersecurity (CIC) has created a comprehensive IoT threat dataset to advance security analysis applications in practical IoT environments34. There were a total of 33 attacks on a network of 105 connected devices in an IoT system. With a total number of 46,179,314 incidents, attacks are categorized into seven groups: DDoS, Recon, DoS, web-based, spoofing, and brute force, in addition to Mirai. There are 37,349,263 instances in the training set, 8,830,051 instances in the test set, and a total of 46 characteristics in the dataset. In every instance, malevolent IoT devices launch attacks on other IoT devices. A total of 67 IoT devices and 38 Zigbee and Z-Wave devices connected to 5 hubs were affected by the attacks. A network of connected sensors, cameras, microcontrollers, and smart home devices may be set up to execute different types of attacks and record the traffic that results from them. Wireshark captures network traffic in PCAP format for further analysis. Since each experiment stores two streams of data, mergecap is used to combine the PCAP files. Various IoT devices were used to compile the dataset, including audio players, video recorders, hubs, power outlets, home automation systems, lighting, sensors, and NextGen gadgets.
This section gives a detailed research methodology consisting of many sequential phases used in the study. Furthermore, the part defines the proposed algorithm sequentially and provides a detailed flowchart of the research process.
CNN
The CNN algorithm uses an artificial neural network, a deep learning approach. Figure 4 illustrates the underlying algorithm that CNN uses: fully connected layers, pooling, flattening, and convolution are all a part of it. A CNN cannot function without the convolution layer. Receiving cell data is processed by the convolutional layer.
In Equation (2), the output volume (Vo) is determined by P (stride), Vi (input volume), S (convolutional layer neuron kernel size), and M (zero padding).
(2)
After, a filter is used to extract the properties of the input value during convolution. This layer creates a feature map. Reducing input data size by pooling simplifies training and reduces the number of parameters to compute. The pooling layer may be chosen using either the largest value within the given size or the average of the values. The developed technique includes two layers of convolution and two levels of pooling. The feature extraction process begins here. The feature data that was obtained is made available for computation. CNN algorithms are available in one, two, or three dimensions. The model employed a convolutional neural network with just one dimension. The layers in the classifying area are known as flattened, fully connected. The data has to be flattened after the convolutional phase so it may be used in the fully connected phase. The flatten layer is where this process is carried out. Within the fully linked layer, the conventional paradigm of artificial neural networks is used. To apply CNN to the data that is not image-based, the input dimensions must be transformed. As a result, CNN may use one-dimensional convolutional layers that are one-dimensional35.

Figure 4: CNN basic algorithm. The basic working and components of the CNN model are described. Please click here to view a larger version of this figure.
Transformer learning
Furthermore, in order to carry out further experiments, we make use of the transformer modelling framework. The model can simultaneously handle all points in the sequence due to the transformer's self-attention mechanism. The result is a faster training time for the model and better use of computer resources by the transformer during inference and training. The transformer is primarily composed of a series of stacked encoders and decoders. The process of encoding involves changing one language into another, but the process of decoding involves determining the likelihood of a different language using previous outputs. Since encoders are mostly responsible for feature extraction, the suggested model exclusively uses encoder components. The transformer encoder consists of input/position embedding, a feedforward neural network, layer normalization, multi-head self-attention, and a residual network.
To prepare the input data for processing by the transformer, an embedding layer is used to convert the categorical characteristics into dense vector representations. Position embedding shows how features are connected sequentially, while input embedding shows how different inputs are related in a common space. Before the transformer processes categorical characteristics, this research uses input embedding to transform them into dense vectors.
An expansion of the attention mechanism, the multihead self-attention mechanism is the foundation of the transformer architecture. Using a scaled dot product, this research employs the multi-headed self-attention mechanism.
(3)
where the query matrix (Q), key matrix (K), value matrix (V), and key matrix dimension (dk) are respectively denoted. By allowing the model to concentrate on data from various features mapped to separate subspaces, resulting in distinct attention values, a multi-head scaled dot-product attention mechanism varies from the scaled dot-product attention mechanism. Overfitting is less likely when attention levels are computed independently for each head. The specific formulation is as follows:
(4)
(5)
Where h is the number of attention heads, dv and dmodel represent the dimension of v and model. Also,

Next, layer normalization is used to stabilize the training process. To prevent gradient explosion, layer normalization limits the output of each layer to a certain range. Both the model's convergence and training speed may be improved by this. While batch normalization takes batch data into account, Layer normalization does not.
CNN-Transformer proposed model
This research offers a hybrid CNN-Transformer technique for detecting intrusions in IoT networks, considering the distinct benefits of both architectures. Figure 5 shows the main components of the proposed CNN-Transformer hybrid technique. After performing all preprocessing stages, including cleaning, normalization, label encoding, and feature selection, each sample in the dataset is represented by a 30-dimensional feature vector, which is called Final Data. The same Final Data is duplicated and fed in parallel to both branches of the hybrid model. Then, this Final Data is sent to both the CNN and Transformer blocks simultaneously. CNN and transformer do not rely on one another in any way; they may both work on the same input data at the same time. The flattened outputs of the two branches, CNN and Transformer, are concatenated to produce a fused feature vector, which is passed to the dense layers, which classify them. The CNN block changes the input shape to (num_features, 1) so that convolutional operations can be done across feature dimensions to find local spatial patterns. At the same time, the Transformer branch changes the same input into a sequential format and sends it to a higher-dimensional embedding space, followed by positional encoding. This lets the transformer pay self-attention in the feature space and find global contextual relationships. The transformer's unique design is used for feature extraction, while the sigmoid function and completely linked layers provide the mapping connection between features and labels. The CNN-Transformer model is structurally represented by Figure 5. The result is an eight-category system for classifying attacks, with DDoS, Recon, DoS, Benign, Web-based, Spoofing, Brute Force, and Mirai as component parts. The evaluation process of the proposed CNN-Transformer is shown in Figure 5.

Figure 5: Architecture of proposed model. The figure shows the input shape and evaluation process of the proposed CNN-Transformer. Please click here to view a larger version of this figure.
Two one-dimensional convolutional neural networks (CNNs) with 64- and 128-unit filters and a 3-kernel size, two max-pooling layers, a flattening step, three dense layers with 256, 128, and 64 units, and three dropout layers make up the convolutional neural network (CNN) layer.
The input data is processed via a 1D convolutional layer with a ReLU activation function, 64 filters, a 3x3 kernel, and a 1x1 stride in the first layer. Following the preceding layer is a MaxPooling1D layer with a pool size of 2. In order to make the model more flexible and to decrease its computational cost, this layer decreases the spatial dimensions of the output area. Layer three is yet another 1D convolutional layer; it has 128 filters, a kernel of three sizes, a stride of one, and an activation function named ReLU. Using more filters to extract input characteristics, this layer is similar to the first max pooling layer. The first layer's output is then processed using the technique. Afterwards, a maximum pooling layer was added after the second convolution layer, and its pool size was also 2. Once again, this layer is used to downsample the feature maps. The fifth layer is a flattening layer, and it takes the output from the previous layer and makes it into a one-dimensional vector.
The obtained feature subsets are input embedded as a last step before becoming the transformer's input. In addition, multi-head attention -- a one-dimensional vector of eight heads with a 32-key dimension -- is used to assess the significance of each head. The multi-head attention layer is the main component of the transformer. This layer allows the model to learn from the embedded representations of different characteristics in an adaptive way. The multi-head attention layer is made up of several self-attention heads, which are also called "scaled dot-product attention". Subsequent to applying layer normalization with epsilon set to 1e-6, the objective is to eradicate scale discrepancies among various characteristics and ensure output stability. By keeping each layer's output within a predetermined range, layer normalization lessens the likelihood of gradient explosion. Following the transformer's output is a flattening layer, which simplifies its combination with the CNN by reducing it to a single vector.
The next step is to combine the CNN and Transformer's flattened outputs using a concatenation layer. Activation function "relu," L2 regularisation, and a dense layer with 256 units follow the flatten layer. This is followed by a dropout layer with a rate of 0.5, which helps avoid overfitting. An additional 128-unit dense layer using L2 regularization and the "relu" activation function follows. The dropout rate in an additional dropout layer is 0.3. ReLU activation function, L2 regularization, and a 64-unit dense layer comprise the subsequent layer. The next layer takes the form of a 0.2 rate dropout layer, randomly eliminating 20% of the units inside. A dense layer with a softmax activation function creates a probability distribution for the last layer's output classes. There are exactly as many units in this layer as there are output classes.
The suggested model may be expressed numerically as follows:
Let X be the CNN input data, where X
R(n×1), after reshaping, and n is the number of selected features, respectively. X is put into a higher-dimensional embedding space of shape R(n × d_model) for the Transformer block. Before it goes into the self-attention mechanism, positional encoding is used.
The following is the expression of the operation in the Conv1D layer, which uses 64 filters and a kernel size of 3:
(6)
where Yi,j is the output at position j of the ith filter, k is the kernel of size 3, b1
R64 is the bias term for each filter, Wk represents filter weights, and ReLU is the activation function. Next, the MaxPooling layer is applied with a pool size of 2, giving an output Z1,j.
(7)
An extra ID convolution layer with a 3 kernel size and 128 filters is included as the model's third layer; its expression is:
(8)
Where k is the kernel of size 3, b2
R128 is the bias term for each filter, and ReLU is the activation function. Another max pool layer is applied, getting the input from the second convolution layer with a pool size of 2. The output Z2,j is given by:
(9)
The fifth layer is a flattening layer expressed as:
(10)
Next are the transformer layers, which include an embedding layer, a multi-head attention layer, and layer normalization
(11)
E is the embedding vector for input class label c, and We is the weight matrix containing embedding vectors for all categories. This maps each integer in c to a dense vector of 32 real-valued components. Next is a multi-head attention layer, as formulated in Equation (3),(4) and (5).
The key matrix has a size of dk =32 and a dimension of h=8. dv and dmodel stand for v's and model's respective dimensions. Also, 
For an output x = MultiHead(Q,K,V), layer normalization provides:
(12)
Where y is the normalized output, γ and β are the learnable parameters, and µ and σ2 denote the mean and variance for x, respectively. Next is the flattening layer for the transformer, defined as

At the end of all the layers, the resultant expression is shown as








Where Z1
R256, Z2
R128, Z3
R64, and Y
R8. This hybrid model also calculates the loss function for multiclass classification, which can be expressed as

where:
Yc represents the true label (one-hot encoded) for class cc,
Y'c is the predicted probability for class c
The proposed algorithm is explained in Table 3 in detail.
Table 3: The proposed CNN-Transformer algorithm. The proposed algorithm is explained in Table 3 step by step. Please click here to download this Table.