Traffic anomaly detection method and system based on improved BERT fusion contrast learning

By improving the BERT model and using weight sharing and comparative learning technology, the problem of excessive resource consumption is solved, the performance and robustness of traffic anomaly detection are improved, and it is suitable for resource-constrained IoT environments.

CN118013201BActive Publication Date: 2024-10-01JINAN UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410258967.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-03-07
Publication Date
2024-10-01
Estimated Expiration
2044-03-07

AI Technical Summary

Technical Problem

Existing traffic anomaly detection methods based on deep learning, especially those based on BERT, consume too much resources and cannot be effectively used in resource-constrained IoT environments, and it is difficult to deploy traditional defense measures in IoT devices, resulting in Insufficient network security management capabilities.

Method used

By improving the BERT model, using weight sharing and contrastive learning technology, we build an improved BERT model, combine the total loss function of cross-entropy loss and contrastive loss, conduct unsupervised pre-training and fine-tuning, and improve the generalization ability and resource utilization of the model. efficiency.

Benefits of technology

It effectively reduces the resource consumption of the model, improves the generalization ability and accuracy, improves the detection performance in resource-constrained environments, and enhances the robustness and accuracy of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118013201B_ABST
    Figure CN118013201B_ABST
Patent Text Reader

Abstract

The present invention discloses a flow anomaly detection method and system based on improved BERT fusion contrast learning, the method comprising the following steps: obtaining flow data and performing data preprocessing; constructing an improved BERT model, including an embedding layer and an encoder network of 12 Transformers, performing weight sharing operations on the encoders of the first 6 Transformers and the encoder networks of the last 6 Transformers respectively; constructing a classification network; constructing a total loss function based on cross entropy loss and contrast loss; performing unsupervised pre-training on the improved BERT model; performing fine-tuning training on the improved BERT model; updating model parameters by back propagation to obtain a trained improved BERT model; inputting the flow data to be tested into the trained improved BERT model to obtain a flow detection result. The present invention can effectively improve the generalization ability of the model while maintaining a stable accuracy rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of equipment flow detection, and in particular to a flow anomaly detection method and system based on improved BERT fusion contrast learning. Background Art

[0002] With the widespread application of IoT technology, a smarter, more efficient and more convenient living and working environment has been created. However, the development of IoT technology has also brought severe challenges to network security. The computing and storage resources of IoT devices are usually limited, so it is difficult to deploy traditional defense measures such as antivirus software and firewalls. After an IoT device is attacked, not only will its own functions be affected, but it may also become a springboard for attackers to further attack other devices. Therefore, it is an urgent need to use traffic anomaly detection technology to timely discover abnormal behaviors in the network and improve security management capabilities. In the past, abnormal traffic detection methods based on deep learning mainly include CNN-based methods and RNN-based methods, but both methods have certain limitations. Network traffic data usually has complex spatiotemporal dynamic characteristics. CNN-based methods are difficult to mine global time series features of traffic data. RNN-based methods can mine global time series features, but cannot achieve parallel computing. In recent years, BERT-based methods can capture long-distance dependencies in traffic data, so they can achieve excellent detection performance, but they require more computing resources and memory and cannot be used in resource-constrained environments. Summary of the invention

[0003] In order to overcome the defects and shortcomings of the prior art, the present invention provides a traffic anomaly detection method and system based on improved BERT fusion contrast learning. The present invention improves the BERT model and utilizes weight sharing to solve the problem of excessive resource consumption when using the BERT model, effectively improving the generalization ability of the model while maintaining a stable accuracy rate.

[0004] In order to achieve the above object, the present invention adopts the following technical solutions:

[0005] The present invention provides a traffic anomaly detection method based on improved BERT fusion contrast learning, comprising the following steps:

[0006] Acquire traffic data, perform data preprocessing on the traffic data, and obtain a byte sequence;

[0007] An improved BERT model is constructed. The improved BERT model includes an embedding layer and an encoder network of 12 Transformers. The byte sequence is input into the embedding layer. The byte sequence is regarded as a piece of text and each byte is regarded as a word. The Token Embedding, Segment Embedding, and Position Embedding representations of the word are extracted and added to obtain the corresponding vector. The encoder networks of the first 6 Transformers and the encoder networks of the last 6 Transformers are respectively weight-shared. The improved BERT model outputs a vector representation of each byte containing traffic context feature information.

[0008] Build a classification network;

[0009] Construct a total loss function based on cross entropy loss and contrast loss;

[0010] After performing masking on the byte sequence, the byte sequence is input into the improved BERT model for unsupervised pre-training, the probability distribution of the bytes at the masked positions is forward calculated, and the difference between the predicted probability distribution and the true label is calculated based on the cross entropy loss function;

[0011] The encoder network output vector of the 12th layer Transformer of the improved BERT model is passed to the classification network. The classification network outputs a probability distribution with the same number of category labels. Based on the contrast loss, the contrast loss is calculated according to the encoder network output vector of the 10th layer Transformer of the improved BERT model.

[0012] Update the model parameters through back propagation to obtain the trained improved BERT model;

[0013] Obtain the traffic data to be tested, input the traffic data to be tested into the trained improved BERT model, and obtain the traffic detection result.

[0014] As a preferred technical solution, the data preprocessing of the traffic data includes data segmentation, word list expansion, data cleaning, and data length unification;

[0015] In the step of splitting the data, the original traffic data set is split based on the network session as a segmentation condition to obtain a data stream set, wherein a data stream is a sequence consisting of multiple data packets, each data packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol} and is arranged in chronological order, and a data packet is a sequence consisting of multiple bytes;

[0016] In the step of expanding the vocabulary, new words are added to the BERT vocabulary;

[0017] In the data cleaning step, delete the information that does not meet the set conditions;

[0018] The step of unifying the data length includes a step of unifying the data stream length and a step of unifying the data packet length.

[0019] As a preferred technical solution, the classification network includes several fully connected layers and softmax layers;

[0020] The vector representation output by the improved BERT model is input into the classification network, and a numerical distribution list of traffic classification is generated through several fully connected layers;

[0021] The softmax layer performs softmax calculation on the numerical distribution list and converts it into a probability distribution of traffic classification.

[0022] As a preferred technical solution, the total loss function is constructed based on the cross entropy loss and the contrast loss, which is specifically expressed as:

[0023] Loss=λ·CELoss+(1-λ)·SCLoss

[0024]

[0025]

[0026] Among them, CELoss represents cross entropy loss, SCLoss represents contrast loss, λ represents the weight used to control the cross entropy loss and contrast loss, t represents the true label of the network flow, y represents the probability result calculated by the softmax layer, σ represents the sigmoid function, x represents the 10th layer vector of the hidden layer vector output by the improved BERT model, and S represents the weight x. i 、x j The similarity function between y and x i 、x j The result of determining whether they belong to the same category, τ is a hyperparameter.

[0027] As a preferred technical solution, after obtaining the trained improved BERT model step, a model evaluation step is also included, using accuracy, recall, precision and F1 score to evaluate the model performance, which is specifically expressed as follows:

[0028]

[0029]

[0030]

[0031]

[0032] Among them, Accuracy represents accuracy, Recall represents recall rate, Precision represents precision, F1_score represents F1 score, TP represents that the improved BERT model correctly predicts the actual abnormal traffic as abnormal traffic, TN represents that the improved BERT model correctly predicts the actual normal traffic as normal traffic, FP represents that the improved BERT model incorrectly predicts the actual normal traffic as abnormal traffic, and FN represents that the improved BERT model incorrectly predicts the actual abnormal traffic as normal traffic.

[0033] The present invention also provides a traffic anomaly detection system based on improved BERT fusion contrast learning, including: a traffic data acquisition module, a data preprocessing module, an improved BERT model construction module, a classification network construction module, a total loss function construction module, an unsupervised pre-training module, a fine-tuning training module, and a traffic detection result output module;

[0034] The flow data acquisition module is used to acquire flow data;

[0035] The data preprocessing module is used to perform data preprocessing on the traffic data to obtain a byte sequence;

[0036] The improved BERT model construction module is used to construct an improved BERT model. The improved BERT model includes an embedding layer and an encoder network of 12 Transformers. The embedding layer takes a byte sequence as a piece of text and each byte as a word. The Token Embedding, Segment Embedding, and Position Embedding representations of the word are extracted and added to obtain the corresponding vector. The encoder networks of the first 6 Transformers and the encoder networks of the last 6 Transformers of the improved BERT model perform weight sharing operations respectively. The improved BERT model outputs a vector representation of each byte containing traffic context feature information;

[0037] The classification network construction module is used to construct a classification network;

[0038] The total loss function building module is used to build a total loss function based on the cross entropy loss and the contrast loss;

[0039] The unsupervised pre-training module is used to input the byte sequence into the improved BERT model for unsupervised pre-training after masking, forward calculate the probability distribution of the bytes at the masked positions, and calculate the difference between the predicted probability distribution and the true label based on the cross entropy loss function;

[0040] The fine-tuning training module is used to pass the encoder network output vector of the 12th layer Transformer of the improved BERT model to the classification network, the classification network outputs a probability distribution with the same number of category labels, and based on the contrast loss, calculates the contrast loss according to the encoder network output vector of the 10th layer Transformer of the improved BERT model;

[0041] Update the model parameters through back propagation to obtain the trained improved BERT model;

[0042] The flow detection result output module is used to input the flow data to be tested into the trained improved BERT model to obtain the flow detection result.

[0043] As a preferred technical solution, the data preprocessing module is used to preprocess the traffic data to obtain a byte sequence, specifically including data segmentation, vocabulary expansion, data cleaning, and unified data length;

[0044] In the data segmentation, the original traffic data set is segmented based on the network session as the segmentation condition to obtain a data stream set, where a data stream is a sequence of multiple data packets, each data packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol} and is arranged in chronological order, and a data packet is a sequence of multiple bytes;

[0045] Add new words to the BERT vocabulary in the expanded vocabulary;

[0046] Delete information that does not meet the set conditions during data cleaning;

[0047] The step of unifying the data length includes a step of unifying the data stream length and a step of unifying the data packet length.

[0048] As a preferred technical solution, the classification network includes several fully connected layers and softmax layers;

[0049] The plurality of fully connected layers convert the vectors output by the improved BERT model into a numerical distribution list of traffic classification;

[0050] The softmax layer performs softmax calculation on the numerical distribution list and converts it into a probability distribution of traffic classification.

[0051] As a preferred technical solution, the total loss function construction module is used to construct a total loss function based on the cross entropy loss and the contrast loss. The total loss function is specifically expressed as:

[0052] Loss=λ·CELoss+(1-λ)·SCLoss

[0053]

[0054]

[0055] Among them, CELoss represents cross entropy loss, SCLoss represents contrast loss, λ represents the weight used to control the cross entropy loss and contrast loss, t represents the true label of the network flow, y represents the probability result calculated by the softmax layer, σ represents the sigmoid function, x represents the 10th layer vector of the hidden layer vector output by the improved BERT model, and S represents the weight x. i 、x j The similarity function between y and x i 、x j The result of determining whether they belong to the same category, τ is a hyperparameter.

[0056] As a preferred technical solution, a model evaluation module is also included, which uses accuracy, recall, precision and F1 score to evaluate model performance, which is specifically expressed as follows:

[0057]

[0058]

[0059]

[0060]

[0061] Among them, Accuracy represents accuracy, Recall represents recall rate, Precision represents precision, F1_score represents F1 score, TP represents that the improved BERT model correctly predicts the actual abnormal traffic as abnormal traffic, TN represents that the improved BERT model correctly predicts the actual normal traffic as normal traffic, FP represents that the improved BERT model incorrectly predicts the actual normal traffic as abnormal traffic, and FN represents that the improved BERT model incorrectly predicts the actual abnormal traffic as normal traffic.

[0062] Compared with the prior art, the present invention has the following advantages and beneficial effects:

[0063] (1) The present invention improves the BERT model and utilizes weight sharing to solve the problem of excessive resource consumption when using the BERT model, effectively improving the generalization ability of the model while maintaining a stable accuracy rate.

[0064] (2) Due to the diversity of bytecode combinations, existing models are more prone to poor robustness. The present invention incorporates the idea of ​​comparative learning to effectively improve the robustness and accuracy of the model. BRIEF DESCRIPTION OF THE DRAWINGS

[0065] Figure 1 It is a flow chart of a traffic anomaly detection method based on improved BERT fusion contrast learning of the present invention;

[0066] Figure 2 A schematic diagram of the framework flow of data preprocessing of the present invention;

[0067] Figure 3 This is a schematic diagram of the data flow hierarchy of the present invention;

[0068] Figure 4 This is a schematic diagram of the embedding layer structure of the BERT model of the present invention;

[0069] Figure 5 Schematic diagram of the encoder network structure of the BERT model of the present invention. DETAILED DESCRIPTION

[0070] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0071] Example 1

[0072] like Figure 1 As shown, this embodiment provides a traffic anomaly detection method based on improved BERT fusion contrast learning, including the following steps:

[0073] S1: Data preprocessing: Data preprocessing is the process of preprocessing traffic data, processing the traffic data set into the format required by the model input. The main steps include data segmentation, vocabulary expansion, data cleaning, and data length unification. The unified data length includes unified data stream length and unified data packet length.

[0074] like Figure 2 As shown, the data preprocessing in step S1 specifically includes the following steps:

[0075] S11: Split data:

[0076] The original traffic data set is segmented based on the network session, and the data set is segmented to obtain a data stream set S = {data stream 1, data stream 2, data stream 3, ...}, where any data stream i is a sequence of multiple data packets. i ={packet 1, packet 2, packet 3, ...}, each packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol}, and is arranged in chronological order; and any packet j is a sequence of multiple bytes, where packet j= {byte 1, byte 2, byte 3, ...}, all bytes are saved in their hexadecimal text form, such as Figure 3 As shown, the data flow hierarchy is obtained;

[0077] S12: Expand the vocabulary:

[0078] Add new words to the BERT vocabulary, that is, all hexadecimal strings that can be obtained by 1 byte, for example: '0x00', '0x01', ..., '0xfe', '0xff';

[0079] S13: Data cleaning:

[0080] Delete information that is useless for training, such as data packets with only IP header information but no payload; at the same time, replace the bytes where the IP address and MAC address of each data packet are located with special characters;

[0081] S14: Unified data stream length:

[0082] Since the subsequent model training requires a unified input format, the data stream length (i.e., the number of packets) threshold is set to N, and the packet length (i.e., the number of bytes) threshold is set to K bytes. The data stream with a length greater than N is truncated to N; if the number of packets in the data stream is less than N, a packet of length K composed of special characters is used to fill N packets. Since the input length of the BERT model is 512, the product of N times K must be less than 512;

[0083] S15: Unified data packet length:

[0084] Referring to step S14, the data packets with a length greater than K bytes are truncated to K bytes, and all data packets with a length less than K bytes are also padded to K bytes with special characters;

[0085] S2: Build an improved BERT model: The improved BERT model includes an embedding layer and 12 Transformer encoder networks. The input is a byte sequence preprocessed by step S1, and the output is a vector representation sequence corresponding to the byte sequence. Among them, the weight sharing operation is performed on the first 6 encoder networks and the last 6 encoder networks respectively, which reduces the number of parameters and improves the generalization ability of the model;

[0086] In this embodiment, the BERT layer network includes an embedding layer and a BERT encoder network;

[0087] S21: Figure 4 As shown, the embedding layer is specifically:

[0088] The output of step S1, that is, the text representation of the byte sequence, is used as input. The byte sequence is regarded as a piece of text, each byte is regarded as a word, and the word vector dimension is 768. The token embedding, segment embedding, and position embedding representation of the word are extracted, and they are added together to obtain a 768-dimensional vector.

[0089] S22: Figure 5 As shown, the encoder network of BERT is specifically:

[0090] An improved encoder network is constructed so that the first 6 encoders share one parameter and the last 6 encoders share one parameter. The output of step S21 is passed through 12 encoders in sequence. The input and output dimensions of each encoder are 768 dimensions. Therefore, a 768-dimensional vector representation containing traffic context feature information is finally obtained for each byte.

[0091] S3: Build a classification network: Take the 768-dimensional [CLS] vector of the last layer of the hidden layer vector output in step S22 as input, pass it through several fully connected layers, and then pass it through the softmax layer.

[0092] S31: Fully connected layer: Taking the feature vector output in step S22 as input, a numerical distribution list of traffic classification is generated through several fully connected layers.

[0093] S32: softmax layer: perform softmax calculation on the numerical distribution list output from step S31 to convert it into a probability distribution of traffic classification.

[0094] S4: Construct an improved loss function: Integrate contrastive learning into model fine-tuning training. Based on the improved loss function, network flow samples of the same category are more similar in the embedding space, and network flow samples of different categories are more dissimilar, which improves the generalization ability and robustness of the model. The final total loss function Loss includes two parts: cross entropy loss and contrast loss:

[0095] Loss=λ·CELoss+(1-λ)·SCLoss

[0096] Among them, λ is used to control the weight between the cross entropy loss and the contrast loss.

[0097] The cross entropy loss CELoss function formula is as follows:

[0098]

[0099] Among them, t is the true label of the network flow, and y is the probability result calculated by the softmax layer.

[0100] The contrast loss SCLoss formula is as follows:

[0101]

[0102] Where σ represents the sigmoid function; x is the 10th layer 768-dimensional [CLS] vector of the hidden layer vector output by the step-improved BERT model; S is the measure of x i 、x j Similarity function between i 、x j The result of whether they belong to the same category (0 or 1), τ is a hyperparameter.

[0103] S5: Model training and evaluation: Divide the preprocessed data into training set, validation set and test set, and perform model training and evaluation;

[0104] S51: Model unsupervised pre-training: Use large-scale traffic data to perform unsupervised pre-training on the improved BERT model;

[0105] S511: Mask processing: For each preprocessed byte sequence (length 512), randomly select some bytes at a ratio of 15% for mask processing. For the randomly selected bytes, the input corresponding to 10% of the bytes is randomly replaced with the input corresponding to another byte, the input corresponding to 80% of the bytes is replaced with a special mark "[MASK]", and the input corresponding to 10% of the bytes remains unchanged;

[0106] S512: Unsupervised training: Input the masked data packet sequence into the BERT model improved by S2, forward calculate the probability distribution of the bytes at the masked position, and use the cross entropy loss function to calculate the difference between the probability distribution predicted by the model and the true label;

[0107] S513: Back propagation: Update the model parameters through back propagation so that the loss gradually approaches 0;

[0108] S514: Unsupervised training stop condition: when the loss reaches a preset level or reaches a preset number of training rounds, stop the unsupervised pre-training and save the unsupervised trained model parameters;

[0109] S52: Model fine-tuning: Use the improved BERT model and loss function to fine-tune the model;

[0110] S521: Input the preprocessed byte sequence (length is 512) into the improved BERT model, perform feature encoding on the input sequence, and finally obtain a 768-dimensional feature vector representation (length is 768*512) corresponding to each byte;

[0111] S522: Pass the 768-dimensional [CLS] vector output by the 12th layer encoder network of the improved BERT model to the classification network, and the classification network will output a probability distribution with the same number of category labels;

[0112] S523: Forward calculation: Use the improved loss function in step S4 to calculate the model loss. The improved loss function takes into account the special nature of the traffic task, incorporates the contrastive learning idea on the basis of the cross entropy loss CELoss, and adds the contrastive loss SCLoss. The contrastive loss SCLoss calculates the contrastive loss based on the 768-dimensional [CLS] vector output by the 10th layer encoder network of the BERT model;

[0113] S524: Back propagation: The model parameters are continuously updated through back propagation (Adam optimizer) to gradually reduce SCLoss and CELoss. As SCLoss decreases, network flow samples of the same category will be closer in the embedding space, and network flow samples of different categories will be farther away, and the robustness and accuracy of the model will be effectively improved;

[0114] S525: fine-tuning training stop condition: by monitoring the loss on the training set and the validation set, when the loss on the validation set reaches a preset level or reaches a preset number of training rounds, stop fine-tuning and save the trained model;

[0115] S53: Model evaluation: Use the test set to evaluate the performance of the trained model. Freeze the trained model parameters, and the rest of the process is the same as the fine-tuning training in step S52. Accuracy, recall, precision, and F1 score are used to evaluate the model performance. The following is the calculation formula:

[0116]

[0117]

[0118]

[0119]

[0120] Among them, TP means that the model correctly predicts the actual abnormal traffic as abnormal traffic, TN means that the model correctly predicts the actual normal traffic as normal traffic, FP means that the model incorrectly predicts the actual normal traffic as abnormal traffic, and FN means that the model incorrectly predicts the actual abnormal traffic as normal traffic.

[0121] S6: Obtain the traffic data to be tested, input the traffic data to be tested into the trained improved BERT model, and obtain the traffic detection result.

[0122] In order to complete the task of device traffic anomaly detection and improve the model capabilities, the present invention improves the BERT model, reduces resource consumption, and improves the generalization ability of the model, while effectively solving the problem of lack of parallel computing capabilities and low accuracy when using traditional recurrent neural networks; in addition, the present invention incorporates the idea of ​​contrastive learning to effectively improve the robustness and accuracy of the model.

[0123] Example 2

[0124] This embodiment provides a traffic anomaly detection system based on improved BERT fusion contrast learning, which is used to implement the traffic anomaly detection method based on improved BERT fusion contrast learning in the above embodiment 1. The system includes: a traffic data acquisition module, a data preprocessing module, an improved BERT model construction module, a classification network construction module, a total loss function construction module, an unsupervised pre-training module, a fine-tuning training module, and a traffic detection result output module;

[0125] In this embodiment, the flow data acquisition module is used to acquire flow data;

[0126] In this embodiment, the data preprocessing module is used to perform data preprocessing on the traffic data to obtain a byte sequence;

[0127] In this embodiment, the improved BERT model construction module is used to construct an improved BERT model, which includes an embedding layer and an encoder network of 12 Transformers. The embedding layer takes the byte sequence as a piece of text, each byte as a word, extracts the Token Embedding, Segment Embedding, and Position Embedding representations of the word, and adds them to obtain the corresponding vector. The encoders of the first 6 Transformers and the encoder networks of the last 6 Transformers of the improved BERT model perform weight sharing operations respectively, and the improved BERT model outputs a vector representation of each byte containing traffic context feature information;

[0128] In this embodiment, the classification network construction module is used to construct a classification network;

[0129] In this embodiment, the total loss function construction module is used to construct a total loss function based on the cross entropy loss and the contrast loss;

[0130] In this embodiment, the unsupervised pre-training module is used to mask the byte sequence and input it into the improved BERT model for unsupervised pre-training, forward calculate the probability distribution of the bytes at the masked positions, and calculate the difference between the predicted probability distribution and the true label based on the cross entropy loss function;

[0131] In this embodiment, the fine-tuning training module is used to pass the encoder network output vector of the 12th layer Transformer of the improved BERT model to the classification network, the classification network outputs a probability distribution with the same number of category labels, and based on the contrast loss, the contrast loss is calculated according to the encoder network output vector of the 10th layer Transformer of the improved BERT model;

[0132] Update the model parameters through back propagation to obtain the trained improved BERT model;

[0133] In this embodiment, the traffic detection result output module is used to input the traffic data to be tested into the trained improved BERT model to obtain the traffic detection result.

[0134] In this embodiment, the data preprocessing module is used to perform data preprocessing on the traffic data to obtain a byte sequence, specifically including data segmentation, vocabulary expansion, data cleaning, and data length unification;

[0135] In the data segmentation, the original traffic data set is segmented based on the network session as the segmentation condition to obtain a data stream set, where a data stream is a sequence of multiple data packets, each data packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol} and is arranged in chronological order, and a data packet is a sequence of multiple bytes;

[0136] Add new words to the BERT vocabulary in the expanded vocabulary;

[0137] Delete information that does not meet the set conditions during data cleaning;

[0138] In this embodiment, unifying the data length includes a step of unifying the data stream length and a step of unifying the data packet length.

[0139] In this embodiment, the classification network includes several fully connected layers and softmax layers;

[0140] Several fully connected layers transform the vectors output by the improved BERT model into a numerical distribution list for traffic classification;

[0141] The softmax layer performs softmax calculation on the numerical distribution list and converts it into the probability distribution of traffic classification.

[0142] In this embodiment, the total loss function construction module is used to construct a total loss function based on the cross entropy loss and the contrast loss. The total loss function is specifically expressed as:

[0143] Loss=λ·CELoss+(1-λ)·SCLoss

[0144]

[0145]

[0146] Among them, CELoss represents cross entropy loss, SCLoss represents contrast loss, λ represents the weight used to control the cross entropy loss and contrast loss, t represents the true label of the network flow, y represents the probability result calculated by the softmax layer, σ represents the sigmoid function, x represents the 10th layer vector of the hidden layer vector output by the improved BERT model, and S represents the weight x. i 、x j The similarity function between y and x i 、x j The result of determining whether they belong to the same category, τ is a hyperparameter.

[0147] In this embodiment, a model evaluation module is also included, which uses accuracy, recall, precision and F1 score to evaluate model performance, which is specifically expressed as follows:

[0148]

[0149]

[0150]

[0151]

[0152] Among them, Accuracy represents accuracy, Recall represents recall rate, Precision represents precision, F1_score represents F1 score, TP represents that the improved BERT model correctly predicts the actual abnormal traffic as abnormal traffic, TN represents that the improved BERT model correctly predicts the actual normal traffic as normal traffic, FP represents that the improved BERT model incorrectly predicts the actual normal traffic as abnormal traffic, and FN represents that the improved BERT model incorrectly predicts the actual abnormal traffic as normal traffic.

[0153] The above embodiments are preferred implementation modes of the present invention, but the implementation modes of the present invention are not limited to the above embodiments. Any other changes, modifications, substitutions, combinations, and simplifications that do not deviate from the spirit and principles of the present invention should be equivalent replacement methods and are included in the protection scope of the present invention.

Claims

1. A traffic anomaly detection method based on improved BERT fusion contrast learning, characterized in that: The steps include: Acquire traffic data, perform data preprocessing on the traffic data, and obtain a byte sequence; An improved BERT model is constructed. The improved BERT model includes an embedding layer and an encoder network of 12 Transformers. The byte sequence is input into the embedding layer. The byte sequence is regarded as a piece of text and each byte is regarded as a word. The Token Embedding, Segment Embedding, and Position Embedding representations of the word are extracted and added to obtain the corresponding vector. The encoder networks of the first 6 Transformers and the encoder networks of the last 6 Transformers are respectively weight-shared. The improved BERT model outputs a vector representation of each byte containing traffic context feature information. Build a classification network; The total loss function is constructed based on the cross entropy loss and contrast loss, which is specifically expressed as: Loss=λ·CELoss+(1-λ)·SCLoss Among them, CELoss represents the cross entropy loss, SCLoss represents the contrast loss, λ represents the weight used to control the cross entropy loss and the contrast loss, t is the true label of the network flow, is the probability result calculated by the softmax layer, σ represents the sigmoid function, x represents the 10th layer vector of the hidden layer vector output by the improved BERT model, and S represents the measure x i 、x j The similarity function between y and x i 、x j The result of whether they belong to the same category, τ is a hyperparameter; After performing masking on the byte sequence, the byte sequence is input into the improved BERT model for unsupervised pre-training, the probability distribution of the bytes at the masked positions is forward calculated, and the difference between the predicted probability distribution and the true label is calculated based on the cross entropy loss function; The encoder network output vector of the 12th layer Transformer of the improved BERT model is passed to the classification network. The classification network outputs a probability distribution with the same number of category labels. Based on the contrast loss, the contrast loss is calculated according to the encoder network output vector of the 10th layer Transformer of the improved BERT model. Update the model parameters through back propagation to obtain the trained improved BERT model; Obtain the traffic data to be tested, input the traffic data to be tested into the trained improved BERT model, and obtain the traffic detection result.

2. The traffic anomaly detection method based on improved BERT fusion contrast learning according to claim 1 is characterized in that: The data preprocessing of the traffic data includes data segmentation, word list expansion, data cleaning, and data length unification; In the step of splitting the data, the original traffic data set is split based on the network session as a segmentation condition to obtain a data stream set, wherein a data stream is a sequence consisting of multiple data packets, each data packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol} and is arranged in chronological order, and a data packet is a sequence consisting of multiple bytes; In the step of expanding the vocabulary, new words are added to the BERT vocabulary; In the data cleaning step, delete the information that does not meet the set conditions; The step of unifying the data length includes a step of unifying the data stream length and a step of unifying the data packet length.

3. The traffic anomaly detection method based on improved BERT fusion contrast learning according to claim 1 is characterized in that: The classification network includes several fully connected layers and softmax layers; The vector representation output by the improved BERT model is input into the classification network, and a numerical distribution list of traffic classification is generated through several fully connected layers; The softmax layer performs softmax calculation on the numerical distribution list and converts it into a probability distribution of traffic classification.

4. The traffic anomaly detection method based on improved BERT fusion contrast learning according to claim 1 is characterized in that: After obtaining the trained improved BERT model step, a model evaluation step is also included, using accuracy, recall, precision, and F1 score to evaluate the model performance, which is specifically expressed as: Among them, Accuracy represents accuracy, Recall represents recall rate, Precision represents precision, F1_score represents F1 score, TP represents that the improved BERT model correctly predicts the actual abnormal traffic as abnormal traffic, TN represents that the improved BERT model correctly predicts the actual normal traffic as normal traffic, FP represents that the improved BERT model incorrectly predicts the actual normal traffic as abnormal traffic, and FN represents that the improved BERT model incorrectly predicts the actual abnormal traffic as normal traffic.

5. A traffic anomaly detection system based on improved BERT fusion contrast learning, characterized in that: include: Traffic data acquisition module, data preprocessing module, improved BERT model construction module, classification network construction module, total loss function construction module, unsupervised pre-training module, fine-tuning training module, and traffic detection result output module; The flow data acquisition module is used to acquire flow data; The data preprocessing module is used to perform data preprocessing on the traffic data to obtain a byte sequence; The improved BERT model construction module is used to construct an improved BERT model. The improved BERT model includes an embedding layer and an encoder network of 12 Transformers. The embedding layer takes a byte sequence as a piece of text and each byte as a word. The Token Embedding, Segment Embedding, and Position Embedding representations of the word are extracted and added to obtain the corresponding vector. The encoder networks of the first 6 Transformers and the encoder networks of the last 6 Transformers of the improved BERT model perform weight sharing operations respectively. The improved BERT model outputs a vector representation of each byte containing traffic context feature information; The classification network construction module is used to construct a classification network; The total loss function construction module is used to construct a total loss function based on the cross entropy loss and the contrast loss. The total loss function is specifically expressed as: Loss=λ·CELoss+(1-λ)·SCLoss Among them, CELoss represents the cross entropy loss, SCLoss represents the contrast loss, λ represents the weight used to control the cross entropy loss and the contrast loss, t is the true label of the network flow, is the probability result calculated by the softmax layer, σ represents the sigmoid function, x represents the 10th layer vector of the hidden layer vector output by the improved BERT model, and S represents the measure x i 、x j The similarity function between y and x i 、x j The result of whether they belong to the same category, τ is a hyperparameter; The unsupervised pre-training module is used to input the byte sequence into the improved BERT model for unsupervised pre-training after masking, forward calculate the probability distribution of the bytes at the masked positions, and calculate the difference between the predicted probability distribution and the true label based on the cross entropy loss function; The fine-tuning training module is used to pass the encoder network output vector of the 12th layer Transformer of the improved BERT model to the classification network, the classification network outputs a probability distribution with the same number of category labels, and based on the contrast loss, calculates the contrast loss according to the encoder network output vector of the 10th layer Transformer of the improved BERT model; Update the model parameters through back propagation to obtain the trained improved BERT model; The flow detection result output module is used to input the flow data to be tested into the trained improved BERT model to obtain the flow detection result.

6. The traffic anomaly detection system based on improved BERT fusion contrast learning according to claim 5 is characterized in that: The data preprocessing module is used to preprocess the traffic data to obtain a byte sequence, specifically including data segmentation, word list expansion, data cleaning, and data length unification; In the data segmentation, the original traffic data set is segmented based on the network session as the segmentation condition to obtain a data stream set, where a data stream is a sequence of multiple data packets, each data packet in the data stream has the same five-tuple {source IP, destination IP, source port, destination port, network protocol} and is arranged in chronological order, and a data packet is a sequence of multiple bytes; Add new words to the BERT vocabulary in the expanded vocabulary; Delete information that does not meet the set conditions during data cleaning; The step of unifying the data length includes a step of unifying the data stream length and a step of unifying the data packet length.

7. The traffic anomaly detection system based on improved BERT fusion contrast learning according to claim 5 is characterized in that: The classification network includes several fully connected layers and softmax layers; The plurality of fully connected layers convert the vectors output by the improved BERT model into a numerical distribution list of traffic classification; The softmax layer performs softmax calculation on the numerical distribution list and converts it into a probability distribution of traffic classification.

8. The traffic anomaly detection system based on improved BERT fusion contrast learning according to claim 5 is characterized in that: It also includes a model evaluation module, which uses accuracy, recall, precision, and F1 score to evaluate model performance, specifically expressed as: Among them, Accuracy represents accuracy, Recall represents recall rate, Precision represents precision, F1_score represents F1 score, TP represents that the improved BERT model correctly predicts the actual abnormal traffic as abnormal traffic, TN represents that the improved BERT model correctly predicts the actual normal traffic as normal traffic, FP represents that the improved BERT model incorrectly predicts the actual normal traffic as abnormal traffic, and FN represents that the improved BERT model incorrectly predicts the actual abnormal traffic as normal traffic.