Attention state recognition neural network modeling and reasoning method based on electroencephalogram sequence
Through the combination of wavelet transformation and deep neural network model, the problem of insufficient modeling capabilities of EEG time series processing and insensitive to transition state recognition in the prior art is solved, and more efficient attention state recognition and individual differences are achieved.
Patent Information
- Application Number
- CN202510608088.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-13
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2045-05-13
Smart Images

Figure CN120123752A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer systems based on specific computing models, and in particular to a neural network modeling and reasoning method for attention state recognition based on EEG sequences. Background Art
[0002] In the existing technology, electroencephalogram (EEG) signals, as an important form of physiological data reflecting human brain activity, have been widely used in the fields of attention state recognition, fatigue monitoring, cognitive assessment, etc. Related research usually uses machine learning methods or shallow neural network models to classify the pre-processed EEG features to determine the individual's attention level. Some methods combine frequency domain analysis, time domain statistical features, or power spectrum density to construct input features, and are supplemented by classifiers such as support vector machines (SVM) and random forests (RF) to complete state recognition tasks.
[0003] However, existing technologies generally have problems such as limited modeling capabilities, insufficient adaptability to individual differences, and insensitivity to transition state recognition. Especially when processing EEG time series, many methods fail to effectively utilize its continuity and spatial distribution information, resulting in insufficient feature expression and unstable classification results. In addition, there is a lack of dynamic feedback mechanism during training, and the optimization strategy cannot be adaptively adjusted according to the recognition difficulty, which affects the generalization ability and practicality of the model.
[0004] In view of this, it is urgent to propose an EEG signal processing and reasoning method based on a specific computational model to more fully explore the spatiotemporal characteristics and individual differences of EEG time series. Summary of the invention
[0005] The present application provides a neural network modeling and reasoning method for attention state recognition based on EEG sequences to improve the accuracy of attention state recognition.
[0006] The present application provides a neural network modeling and reasoning method for attention state recognition based on EEG sequences, including: The EEG time series signals obtained by sampling are processed by wavelet transform to extract sub-band signals of different frequency bands and obtain frequency domain information with multi-scale resolution; Extracting time-frequency domain features of the subband signals, and encoding the extracted features into a multi-dimensional input tensor with channel embedding vectors according to the distribution position of each channel in the scalp space; Input the multi-dimensional input tensor into a multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit and a residual connection structure, wherein the spatial convolution unit is used to extract spatial correlation features between different channels, the gated recurrent unit is used to learn the evolution law of time series features, and the residual connection structure is used to alleviate the gradient vanishing problem in the deep network training process; During the model training process, a cross-time-step attention mechanism is introduced to weight the feature contribution of each time segment, and the category weight coefficient in the loss function is dynamically adjusted based on the classification confusion of the current training batch to enhance the recognition sensitivity of the attention transition state. After training is completed, target EEG time series data of arbitrary length is received, and the trained neural network model is used for inference to output the corresponding attention state classification label and its confidence score value; wherein the confidence score value is calculated based on the attention-weighted feature representation and the classification boundary distance, and is used to quantify the stability and credibility of the classification result.
[0007] The beneficial effects of the technical solution provided by this application include: (1) By constructing a deep neural network model that integrates spatial convolution, gated recurrent unit and residual connection structure, it can simultaneously capture the spatial dependency between channels and the evolution law of time series, which is significantly better than the traditional shallow classifier, reflecting the technical advantages of the present invention in the design of specific computing model structure, and belongs to the deep modeling innovation in the field of intelligent algorithms. (2) By introducing the cross-time step attention mechanism and combining the dynamic loss weight adjustment strategy based on classification confusion during training, the model can focus on key fragment features and adapt to the difficulty of distinguishing between samples, effectively solving the problem of fuzzy classification of intermediate states in the existing technology, and demonstrating the intelligent regulation ability of the neural network training mechanism. (3) By combining the feature representation weighted by the attention mechanism with the classification boundary distance and outputting the confidence score, it helps the downstream system to make intelligent decision scheduling based on the recognition credibility, and enhances the interpretability and system robustness of the neural network reasoning results. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] Figure 1 This is a flowchart of a neural network modeling and reasoning method for attention state recognition based on EEG sequences provided in the first embodiment of the present application. DETAILED DESCRIPTION
[0009] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0010] The first embodiment of the present application provides a neural network modeling and reasoning method for attention state recognition based on EEG sequences. Figure 1 , which is a schematic diagram of the first embodiment of the present application. Figure 1 The first embodiment of the present application provides a detailed description of a neural network modeling and reasoning method for attention state recognition based on EEG sequences.
[0011] Step S101: performing wavelet transform processing on the EEG time series signal obtained by sampling to extract sub-band signals of different frequency bands and obtain frequency domain information with multi-scale resolution.
[0012] The core of step S101 is to process the EEG time series signal obtained by continuous sampling with wavelet transform, so as to obtain sub-band signals with multi-scale frequency domain information. First, the EEG signal is continuously collected by setting a reasonable sampling frequency (for example, 256 Hz) to obtain time series data reflecting the brain's electrical activity. The signal usually comes from multiple electrode channels arranged on the surface of the scalp. The data collected by each channel is independent and synchronous, forming an original data matrix containing multiple channel signals.
[0013] The collected signal contains multiple frequency components, ranging from extremely low-frequency delta waves to higher-frequency beta or gamma waves. In order to effectively extract the characteristic information of these different frequency bands, wavelet transform is used to process the original EEG signal. Wavelet transform is a method commonly used in time-frequency analysis. It can decompose the original signal layer by layer from high to low frequency, and each layer corresponds to a specific frequency interval, thereby obtaining multiple sub-signals. These sub-signals represent the changing trends of the original EEG signal in different frequency bands, retaining the characteristics of the signal in two dimensions of time and frequency.
[0014] In the present invention, a three-layer wavelet decomposition structure is preferably used, which can effectively cover the main EEG frequency bands, such as delta, theta, alpha and beta bands. In each layer, the signal is decomposed into an approximate signal representing the low-frequency trend and a signal representing the high-frequency details, and it is iterated layer by layer until the set decomposition depth is reached. The result of this processing is a set of sub-band signals with shortened time length and different frequency distribution.
[0015] After the wavelet decomposition is completed, in order to unify the data scale and enhance the convergence of the subsequent model, it is recommended to normalize each sub-band signal. For example, the overall amplitude of each sub-band signal can be adjusted to a fixed range, or the DC offset and energy fluctuation can be removed, so that the signals of all frequency bands have a unified numerical feature standard, which is convenient for the subsequent processing of the neural network.
[0016] In summary, step S101 obtains a set of sub-band signals with clear spectrum distribution and good time resolution by performing wavelet transform and normalization on the original EEG time series signal, laying a solid foundation for feature extraction and neural network modeling in subsequent steps. This processing method not only retains the multi-frequency information in the EEG signal, but also enhances the model's ability to recognize complex attention states, and is a pre-processing step for intelligent computing.
[0017] Furthermore, the wavelet transform processing is performed on the EEG time series signal obtained by sampling to extract sub-band signals of different frequency bands and obtain frequency domain information with multi-scale resolution, including: After receiving the EEG time series signal, based on a predefined frequency sensitivity mapping function, a target frequency band group associated with the current task is determined, and according to the difference in response entropy of each frequency band group to the change of attention state in the training sample, the wavelet basis function with the strongest adaptability is selected as the wavelet transform mother wavelet of the current sample, wherein the frequency sensitivity mapping function is obtained by analyzing the discriminant information gain of the training set attention state labeled samples in each sub-frequency band, and is used to guide the wavelet function to match the feature expression structure; The wavelet basis function obtained by the selection is used to perform multi-scale decomposition on the EEG time series signal to generate Wave, Wave, Wave, wave and wave, and introduce a bandpass energy gating operation for each sub-band signal to filter out low-amplitude non-significant frequency bands, output an enhanced sub-band signal set, and use the enhanced sub-band signal set as frequency domain information with multi-scale resolution; The enhanced sub-band signal set is input into the time-frequency feature extraction module, and a frequency band-channel weight matrix is constructed according to the average energy density and channel signal-to-noise ratio of each sub-band. The frequency band-channel weight matrix is used to weight the expression ratio of different sub-band signals in the multi-channel structure to form a time-frequency feature tensor reflecting the spatial heterogeneity of the frequency band; The time-frequency feature tensor is used as the basis for constructing a multi-dimensional input tensor and is input into the multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit and a residual connection structure to enhance the attention shift sensitivity and channel heterogeneity modeling capability of feature expression.
[0018] In this implementation, the EEG time series signal needs to go through a multi-scale frequency domain feature extraction process before entering the deep neural network model to improve the model's sensitivity and discrimination ability to changes in attention states. This processing flow performs time-frequency analysis on the original EEG signal based on the wavelet transform method, aiming to obtain a sub-band signal set with high resolution and multi-band structure, and optimizes the selection of wavelet functions and their decomposition strategies through multiple information guidance mechanisms.
[0019] First, when the system receives the original EEG time series signal, it will call a pre-built frequency sensitivity mapping function to filter the frequency band range that needs to be focused on for the current task. This mapping function is constructed by statistically analyzing the discriminant information gain of samples with attention state labels in the training set in each frequency band. The calculation method of information gain can refer to the principle of information entropy difference, that is, calculating the mutual information between the spectral characteristics of the sample and its label in each frequency band, which is used to quantify the discriminant contribution of the frequency band to the classification task. Based on the results of this function, the system will select several target frequency band groups with the most information value for the current classification task.
[0020] After determining the target frequency band, the system will further analyze the response entropy of the samples corresponding to these frequency bands, that is, evaluate the stability and concentration of the attention state distinction of the samples in this frequency band. Among all the available wavelet basis function libraries (such as Daubechies, Symlet, Coiflet, etc.), the system combines multiple indicators such as frequency coverage, time domain compactness, and computational stability to select a wavelet basis function that best suits the current frequency band expression as the mother wavelet. This wavelet basis function will be used as the mother function in the wavelet decomposition process of the current sample to ensure that the signal has a stronger feature retention ability in the frequency focus area.
[0021] Subsequently, the system uses the selected wavelet basis function to perform multi-scale decomposition on the original EEG time series. This process expands the original signal at different scales to generate several sub-band signals covering from low frequency to high frequency, typically including standard EEG frequency bands such as delta wave (0.5–4 Hz), theta wave (4–8 Hz), alpha wave (8–13 Hz), beta wave (13–30 Hz) and gamma wave (30–100 Hz). In this process, the system further introduces a bandpass energy gating operation, performs energy statistics on each sub-band signal, and sets a gating threshold to filter out frequency components whose signal strength is significantly lower than the baseline. This operation can be achieved by comparing the local energy with the global average energy and setting a dynamic threshold. Its purpose is to eliminate the noise-dominated frequency band and output a set of enhanced sub-band signals with a clearer structure.
[0022] The above-mentioned enhanced sub-band signal set is input into the subsequent time-frequency feature extraction module, in which the system constructs a frequency band-channel weight matrix based on the average energy density of each sub-band on different channels and the signal-to-noise ratio (SNR) index in the historical records. Energy density reflects the stable activation degree of a certain frequency band signal on a specific channel, and SNR reflects the reliability of the channel signal. After normalization, the two indicators are combined into weight factors to form a matrix structure for weighted fusion of different frequency bands between channels. This matrix will be applied to the structural construction process of the sub-band signal to generate a set of tensors reflecting the spatial heterogeneity of the frequency band and the distribution state of the signal characteristics, called the time-frequency feature tensor. This frequency band-channel weight matrix can be simply understood as a two-dimensional table, where each row corresponds to a frequency band, such as δ waves, θ waves, α waves, β waves, γ waves, etc., and each column corresponds to an EEG channel, such as standard electrode positions such as F3, F4, C3, C4, Pz, etc. Each element in the matrix is a weight value, indicating the importance of the frequency band on the channel. These weights are derived from two factors: one is how strong the overall energy of the frequency band is on the channel, and the other is whether the signal quality of the channel has been stable in history, such as whether there is too much noise. The system normalizes these two indicators and mixes them together to get a final weight. The larger the weight, the more important the channel is for identifying the attention state on the frequency band.
[0023] Finally, the time-frequency feature tensor is input as a structural input into the backbone structure of the neural network, that is, a multi-channel convolutional recurrent neural network model composed of spatial convolution units, gated recurrent units, and residual connection structures. Since the structure of this tensor fully encodes task-related information in both the frequency dimension and the channel dimension, it can effectively enhance the network's ability to respond to the attention state transition interval and improve the expression and understanding of the heterogeneity of features between channels, thereby providing a feature input basis with clear structure and strong discrimination ability for the final reasoning process.
[0024] The implementation process of the frequency sensitivity mapping function is as follows. During the training phase, the system will perform multi-band decomposition on all labeled samples in turn and initially divide them into Standard EEG frequency bands such as . In each frequency band, statistical features such as energy density, spectral entropy, and power center are extracted, and these features are used to train a shallow classifier (such as a linear support vector machine or a lightweight neural network) on the frequency band to estimate its classification performance in the frequency band. The system then calculates the output entropy difference or mutual information gain of the classifier between different categories and sorts all frequency bands. The sorting result constitutes a frequency sensitivity mapping function, whose output is a set of numerical values corresponding to the discriminant efficiency scores of each frequency band. The higher the score of the frequency band, the more sensitive its spectral structure is to changes in the state of attention, that is, it contains more discriminative neural activity signals. With the frequency sensitivity score, the system selects a group of target frequency bands with the most discriminative value. After that, in order to match the spectral characteristics of these frequency bands, the system will select the most suitable mother wavelet function from the preset wavelet basis function library. In order to achieve a scientific and reasonable selection, the system needs to evaluate the indicators of each candidate wavelet function in three dimensions, namely frequency coverage, time domain compactness and computational stability.
[0025] Frequency coverage indicates the ability of the wavelet function to fit the target frequency band in the frequency domain, that is, whether its main frequency energy band covers and focuses on the sensitive frequency band of the current task. The evaluation method can obtain its main energy frequency band interval by Fourier transforming the spectral response of the wavelet function, and evaluate the intersection coverage with the task frequency band. Time domain compactness reflects the local concentration characteristics of the wavelet function on the time axis. The higher the compactness, the easier it is for the wavelet function to capture instantaneous local features and the more sensitive it is to sudden EEG state switching. This indicator is usually measured by calculating the non-zero support width or energy concentration interval length of the wavelet function on the time axis. The narrower the support, the higher the time domain compactness.
[0026] Computational stability focuses on whether the wavelet function is prone to abnormal behaviors such as edge effects, singular values or numerical overflow in actual decomposition. This indicator can be estimated by recording the error distribution and numerical stability (such as the maximum coefficient value range and the degree of boundary leakage) in the wavelet decomposition experiment of a large number of samples. Generally, smoother wavelets (such as Symlet) are more stable than Daubechies, but the frequency focusing may be weaker. After each wavelet function completes the scoring under these three indicators, the system will comprehensively normalize these scores, sort them by weighted scores, and select the function with the highest score as the mother wavelet of the wavelet transform of the current EEG sample. In actual engineering implementation, the score can be combined in a weighted manner, such as 40% frequency coverage, 30% time domain compactness, and 30% computational stability, to reconcile the importance of each dimension.
[0027] The selected mother wavelet will be used to perform multi-scale wavelet decomposition on the EEG time series and extract sub-band signals covering multiple frequency bands from δ to γ. This process combines task adaptability analysis and function adaptability search, thereby achieving signal enhancement based on classification requirements optimization in the frequency domain decomposition stage, laying a solid foundation for subsequent channel modeling and neural network reasoning.
[0028] Step S102: extracting time-frequency domain features from the sub-band signals, and encoding the extracted features into a multi-dimensional input tensor with channel embedding vectors according to the distribution position of each channel in the scalp space.
[0029] In step S102, the sub-band signals of each frequency band obtained after the wavelet transform processing will be used as the basis for feature extraction. It is necessary to further explore its discriminative features in the time-frequency dimension, and encode it in combination with the spatial layout of the EEG channel on the scalp surface, and finally generate a multi-dimensional input tensor that can be processed by the neural network. The core goal of this step is to organically integrate the multi-frequency bands, time continuity and spatial distribution information in the EEG signal, and construct structured input data to support the training and reasoning of subsequent deep models.
[0030] First, for each subband signal, common time-frequency domain feature parameters including but not limited to short-time energy, spectral entropy, root mean square amplitude, bandwidth center frequency, instantaneous phase, etc. can be extracted. The sliding time window method can be used to locally intercept the signal, and the corresponding statistical eigenvalues are calculated in each window to ensure that the dynamic changes of the EEG signal can be characterized. This window processing method can form a feature vector in each time slice, describing the activity pattern of the time period within a specific frequency range. It is usually recommended to set the window length to 256 milliseconds and the step size to 64 milliseconds to obtain sufficient frequency domain stability while ensuring time resolution.
[0031] Next, the channel position information is introduced into the extracted feature vector. Since the spatial distribution of EEG acquisition channels has a certain physiological correspondence, for example, the F3, F4, Pz and other electrodes in the international 10-20 system correspond to different areas of the cerebral cortex, which directly affects the spatial dependency structure of the EEG signal, it is necessary to encode the channel position into an embedding vector that can be used for model calculation. For example, the channel number can be mapped to a fixed-dimensional spatial position vector, or an embedding representation that reflects the spatial adjacency relationship can be constructed using methods such as Gaussian position mapping, so that the model can explicitly perceive the physical distance and topological structure between different channels.
[0032] Finally, the above-mentioned time series features extracted based on frequency bands are fused with the channel embedding vector to form a structured three-dimensional input tensor, whose dimension can be set as "time step × number of channels × feature dimension". Among them, the time step represents the number of continuous segments after being segmented in a sliding window manner, the number of channels corresponds to the number of acquisition electrodes, and the feature dimension includes all extracted time-frequency features and channel embedding vectors. The constructed tensor can be directly used as the input of the deep neural network to ensure that the input data not only retains the spectral information and time series structure, but also encodes the spatial topological relationship, thus providing a basis for subsequent spatial convolution and loop modeling.
[0033] Through this processing process, the original EEG signal is transformed from an unstructured one-dimensional time series into high-dimensional input data that integrates time, frequency domain and spatial information, which greatly improves the neural network's ability to model differences in attention states and effectively supports the implementation path of the neural network modeling and reasoning method in the present invention.
[0034] Furthermore, the time-frequency domain feature extraction is performed on the sub-band signal, and the extracted feature is encoded into a multi-dimensional input tensor with a channel embedding vector according to the distribution position of each channel in the scalp space, including: The distribution position of each channel in the scalp space is converted into a three-dimensional Cartesian coordinate form, and a channel space adjacency matrix is constructed based on the three-dimensional coordinates to reflect the physical neighbor relationship between the channels; The channel space adjacency matrix is input into a graph convolutional network as an edge connection weight of a graph structure. The graph convolutional network uses the frequency domain features of each channel as an initial node input, performs graph embedding modeling on the features of each channel, and generates a first channel embedding vector that integrates the channel time-frequency response and the spatial structure; Based on the average signal-to-noise ratio and power intensity index of each channel in the target sub-band frequency band recorded in the training phase, the characteristic quality descriptor of the channel in the frequency band is extracted and encoded into a set of spectral characteristic vectors; The first channel embedding vector is connected and fused with the corresponding spectral characteristic vector in dimension to form a second channel embedding vector containing multimodal representation contents such as spatial position information, frequency domain response structure and signal-to-noise characteristics. The second channel embedding vector is used as the basis for constructing the representation of each channel dimension in the multidimensional input tensor, so as to enhance the modeling ability of subsequent neural network structures for channel heterogeneity and task-related features.
[0035] In this embodiment, the differences and structural characteristics of each channel in the EEG time series signal are deeply modeled, and a method for constructing an embedded vector that integrates the spatial position, time-frequency characteristics and signal-to-noise information of the channel is proposed to construct a multi-dimensional input tensor with high expressiveness. The method first uses the actual geometric arrangement of each EEG channel on the acquisition cap or electrode array to project the position of each channel into a three-dimensional Cartesian coordinate, for example, by obtaining its three-dimensional coordinate value through a polar coordinate transformation in a standardized scalp grid or an international 10-20 system. Based on the Euclidean distance between all channel coordinates, a preset distance threshold or Gaussian kernel function is used to construct a spatial adjacency matrix between channels to quantitatively describe the physical proximity relationship between each channel and other channels, thereby forming a graph adjacency weight that reflects the channel topological structure.
[0036] Afterwards, the channel space adjacency matrix is used as the structural input of the graph convolution network to control the edge connection relationship in the graph convolution operation. In the graph convolution network, the frequency domain subband features of each channel obtained after the aforementioned wavelet transform are used as the initial graph node feature input, and the node dimension is consistent with the subband feature dimension. The graph convolution operation realizes the feature propagation and fusion between channels through the connection weights defined by the adjacency matrix, capturing the local and global patterns that influence each other in the physical space in the channel spectrum structure, thereby obtaining the first channel embedding vector that fuses the channel time-frequency features and spatial position relationship. This embedding vector retains the response mode of the original channel in a specific frequency band and introduces spatial coupling information, which helps the subsequent network to accurately identify the characteristics of EEG signals under different spatial structures.
[0037] After the graph embedding is generated, in order to further improve the task sensitivity of the embedding representation, the spectral characteristic level information is introduced to encode the quality of each channel. Specifically, during the training phase, the signal-to-noise ratio (SNR) and average power of each channel in the selected sub-band frequency band are recorded and normalized to generate a spectral characteristic vector that characterizes the signal quality of the channel in this frequency band. The spectral characteristic vector can contain multiple dimensions, including but not limited to indicators such as the maximum spectral amplitude, the energy density distribution at the center of the frequency band, and the power spectrum entropy, and it is vectorized and encoded.
[0038] Finally, the first channel embedding vector and its corresponding spectral characteristic vector are concatenated and fused in the feature dimension to form a second channel embedding vector. This vector not only contains the spatial structural relationship and spectral response, but also further introduces channel stability and information credibility factors to form a multi-mode composite embedding representation. The second channel embedding vector is then used to construct the channel dimension representation in the final multi-dimensional input tensor. All time step inputs of each channel will share this vector as the spatial embedding basis to enhance the neural network model's modeling ability for channel heterogeneity features and the basis for channel weight reasoning in the attention mechanism, thereby improving the accuracy of cross-channel comparison judgment and state recognition in subsequent models. This structure not only encodes the input data structurally a priori, but also provides a basis for feature confidence adjustment at the channel level, which helps to improve the model's robustness and generalization ability to local abnormal channels. The following is the reference implementation code: import numpy as np import torch import torch.nn as nn import torch.nn.functional as F # -------- 1. Define graph convolution unit (for channel embedding) -------- class GraphConvolution(nn.Module): def __init__(self, in_features, out_features): super(GraphConvolution, self).__init__() self.linear = nn.Linear(in_features, out_features) def forward(self, x, adj): """ x: initial channel features [num_channels, in_features] adj: channel adjacency matrix [num_channels, num_channels] """ support = self.linear(x) # linear transformation out = torch.matmul(adj, support) # graph convolution propagation return out # -------- 2. Construct channel space adjacency matrix-------- def build_channel_adjacency(channel_coords, sigma=1.0): """ The input is the three-dimensional Cartesian coordinates of each channel, and the adjacency matrix between channels is constructed.
[0039] channel_coords: numpy array, shape [num_channels, 3] """ num_channels = channel_coords.shape[0] adj = np.zeros((num_channels, num_channels)) for i in range(num_channels): for j in range(num_channels): if i != j: dist = np.linalg.norm(channel_coords[i] - channel_coords[j]) adj[i, j] = np.exp(-dist ** 2 / (2 * sigma ** 2)) # Gaussian kernel weighting adj = torch.FloatTensor(adj) return adj # -------- 3. Construct multi-mode embedding vector (fusion of spatial + spectral quality) -------- def build_multimodal_embedding(gcn_output, spectral_descriptors): """ Concatenate the graph convolution output and the spectral quality descriptor to form a multi-modal embedding.
[0040] gcn_output: [num_channels, gcn_dim] spectral_descriptors: [num_channels, spectral_dim] """ return torch.cat([gcn_output, spectral_descriptors], dim=1) # -------- 4. Channel attention module (for subsequent networks) -------- class ChannelAttention(nn.Module): def __init__(self, embedding_dim): super(ChannelAttention, self).__init__() self.score_fn = nn.Linear(embedding_dim, 1) def forward(self, channel_embeddings): #Embed each channel to score and get the normalized attention weight scores = self.score_fn(channel_embeddings) # [num_channels,1] attention_weights = F.softmax(scores, dim=0) # Attention normalization return attention_weights # -------- 5. Inference output module (classification + confidence score) -------- class ClassifierWithConfidence(nn.Module): def __init__(self, input_dim, num_classes): super(ClassifierWithConfidence, self).__init__() self.classifier = nn.Linear(input_dim, num_classes) def forward(self, fused_representation): logits = self.classifier(fused_representation) # [batch_size, num_classes] probs = F.softmax(logits, dim=-1) confidence = torch.max(probs, dim=-1).values # Maximum category probability as confidence score return probs, confidence The above code first receives the three-dimensional coordinates and corresponding spectral features of each channel in the EEG signal as input. In the initial stage, the three-dimensional geometric distance between channels is calculated, and the distance is converted into connection strength using the Gaussian kernel function to construct a channel space adjacency matrix to reflect the physical proximity relationship between electrodes. The adjacency matrix is fed into the graph convolutional network as a graph structure, and together with the frequency domain features of each channel in a specific frequency band, it is used to generate the first layer of channel embedding representation that integrates the spatial structure and spectral response.
[0041] Subsequently, the system further introduces the quality description information such as the spectrum signal-to-noise ratio and power counted by each channel during the training phase, and encodes it into a spectrum characteristic vector. These vectors are concatenated with the embedding vector generated by the aforementioned graph convolution to obtain a multimodal channel embedding vector that contains spatial structure, frequency response and signal quality. Before entering the subsequent network, the model weights these embedding vectors through the channel attention mechanism to identify the channel features that are most relevant to the target classification task. The attention weights are normalized and used to weightedly fuse all channel embeddings, outputting a fused representation tensor representing the integrated features of the entire brain region.
[0042] Finally, the fused representation is fed into a fully connected classifier for inference prediction of the attention state, and the output is the probability distribution of the classification label and its maximum probability value, which serves as the confidence score of the prediction to measure the reliability of the current output result.
[0043] Step S103: Input the multi-dimensional input tensor into a multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit and a residual connection structure, wherein the spatial convolution unit is used to extract spatial correlation features between different channels, the gated recurrent unit is used to learn the evolution law of time series features, and the residual connection structure is used to alleviate the gradient vanishing problem in the deep network training process.
[0044] In step S103, the multi-dimensional input tensor constructed in the previous stage is input into a neural network model with a specific structure. The model is composed of a spatial convolution unit, a gated recurrent unit, and a residual connection structure. The overall architecture has the ability to simultaneously model the spatial dependency and temporal dynamic characteristics of EEG signals, and can alleviate the training degradation problem caused by the increase in network depth.
[0045] First, the input tensor is fed into the spatial convolution unit. The main function of this unit is to identify the spatial correlation pattern between different channels, that is, to learn the synchronous or coordinated change relationship of the EEG channels corresponding to each electrode position in different frequency bands. In implementation, the spatial convolution operation is carried out on the "channel dimension", and a two-dimensional convolution kernel is used to perform sliding convolution on the feature vectors of multiple channels to extract local spatial structural features. The size of the convolution kernel is generally set to 3×1 or 5×1 to ensure that the interaction between adjacent channels is captured without interfering with the temporal structure. To enhance the nonlinear expression capability, the ReLU activation function can be connected after the convolution operation.
[0046] Subsequently, the extracted spatial feature sequence is input into the gated recurrent unit to model the feature evolution law in the time dimension. Specifically, the gated recurrent unit (GRU) or long short-term memory unit (LSTM) structure can be used. These recurrent neural network units can effectively handle the dependencies between the front and back in the time series and avoid the gradient vanishing problem when the sequence is long. In actual operation, the spatial feature vector of each time step is input into the recurrent unit in turn. After recursive propagation of multiple time steps, the model can learn the change pattern of attention state over time. It is recommended to set the number of recurrent layers to 1 to 2 layers and the hidden state dimension to be no less than 128 to balance the modeling ability and training efficiency.
[0047] In order to further improve the training stability of deep networks and accelerate convergence, a residual connection mechanism is introduced into the model structure. Residual connection refers to the introduction of jump connections between multiple network layers, which directly adds the input features of the previous layer to the output of the current layer, thereby constructing a shortcut path for "identity mapping". This structure can effectively prevent the continuous attenuation of gradients in deep networks, so that the model can still maintain good training feasibility when facing high-dimensional input data and complex classification tasks. In this model, residual connections are mainly used between the recurrent network and the output layer to connect the original spatial-temporal features with the high-order extraction results.
[0048] Through the collaborative work of the above structures, the neural network model can perceive and model the internal laws of EEG data at multiple scales, especially the temporal and spatial feature changes corresponding to the attention state. The network structure has been verified through multi-task experiments and has stable, accurate and generalized recognition performance, which can provide a solid feature foundation for subsequent attention state classification and credibility scoring.
[0049] For example, the network model as a whole can be divided into three sequentially connected modules: spatial convolutional unit, gated recurrent unit and residual connection structure. The input data is a three-dimensional tensor collected and processed from the EEG channel, with the dimension of "time step × number of channels × number of features", assuming it is 128 × 32 × 20, indicating that the sample consists of 128 time segments collected from 32 EEG channels, and each channel contains 20-dimensional frequency domain or spatiotemporal features at each time step.
[0050] First, the input of the spatial convolution unit is the entire tensor, and the convolution is mainly performed in the "channel" dimension. A set of two-dimensional convolution kernels can be selected, and the size is set to 1 × 3, that is, for the data of each time step, the feature vectors of the three adjacent channels are taken for sliding window convolution. The convolution output dimension can be set to 64 or 128 channels, indicating the number of spatial features formed after convolution. After the convolution operation, the ReLU function is used to activate the nonlinear response, and Batch Normalization is connected to normalize the output to improve the convergence speed. The convolution output shape is 128 × 30 × 64 (the number of time steps remains at 128, the channel is reduced to 30 after convolution, and the feature dimension is the number of convolution output channels).
[0051] This output is used as a time series input and fed into the gated recurrent unit frame by frame. It is recommended to use a bidirectional GRU structure, and the hidden state dimension of each GRU unit is set to 256. The introduction of the bidirectional structure enables the model to perceive the evolution of time features in the past and future at the same time, and improve the contextual consistency of state judgment. At each time step, the GRU unit receives a 30 × 64-dimensional tensor, expands it into a one-dimensional feature vector and inputs it into the network, and the GRU gradually updates the hidden state. After 128 time steps of iteration, the output is a sequence tensor with a dimension of 128 × 512, which represents the comprehensive representation of each time step after the fusion of the previous and next contextual information.
[0052] To further enhance the expressiveness of the model, a residual connection is added to the end of the GRU output. This is achieved by first converting the dimension of the feature representation before the spatial convolution unit is input into the GRU (for example, the original feature is mapped to the same 512-dimensional GRU output through a fully connected layer), and then adding it to the GRU output element by element in each time step to form a residual enhanced sequence output. This mechanism not only improves the training stability, but also allows the original features to be retained in the deep network and participate in the final classification, which helps to improve the model's recognition ability at the boundaries of complex states.
[0053] Finally, the residual output is input into the attention module or pooling operation, and the subsequent structure determines whether to generate a global context vector, perform multi-label classification, or output a confidence score.
[0054] Furthermore, the step of inputting the multi-dimensional input tensor into a multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit, and a residual connection structure includes: Based on the multidimensional input tensor, a channel correlation matrix is constructed, where the channel correlation matrix is obtained by calculating the cosine similarity between channel embedding vectors corresponding to different channels in the multidimensional input tensor, and is weighted in combination with the distance information of the channels in the scalp space, so as to reflect the time-frequency feature synergy and spatial neighbor relationship between the channels; The channel correlation matrix is used as a parameter to dynamically generate a convolution kernel weight of a spatial convolution unit, wherein the spatial convolution unit performs a two-dimensional convolution operation on the channel dimension of the multidimensional input tensor to extract spatial dependency features between channels and output a spatial correlation feature tensor, wherein the spatial correlation feature tensor enhances cross-channel coupling structure information based on the original multidimensional input tensor; The spatial correlation feature tensor is input into a gated recurrent unit, which adopts a gated recurrent structure to recursively process the spatial correlation feature tensor of each time step, and introduces a channel attention gating mechanism. The channel attention gating mechanism calculates the channel weight coefficient based on the average correlation of each channel in the channel correlation matrix, which is used to adjust the state transfer ratio of each channel in the recursive update of each time step, and outputs the time series evolution feature tensor; The time series evolution feature tensor is input into the residual connection structure. While the residual connection structure transmits the processing result in the main branch network path, batch normalization and nonlinear transformation operations are introduced in the jump path. The batch normalization is used to standardize the distribution of the time series evolution feature tensor. The nonlinear transformation operation adopts the ReLU activation function to improve the signal expression ability. Finally, the output of the jump path is element-by-element added with the output of the main branch path to generate a residual fusion feature tensor, which serves as the input basis for the subsequent attention mechanism and classification reasoning steps.
[0055] In the neural network modeling and reasoning method for attention state recognition based on EEG sequences described in the present invention, in order to effectively model the channel spatial structure and temporal dynamic changes of EEG signals, a multi-channel convolutional recurrent neural network model consisting of a spatial convolution unit, a gated recurrent unit and a residual connection structure is adopted. In order to ensure that the model still has generalization ability and structural adaptability under different individuals and different channel arrangement conditions, the neural network model introduces a set of dynamic calculation mechanisms with a channel correlation matrix as the core, and executes multiple data processing steps in sequence, forming a high-dimensional feature tensor with semantic expression layer by layer.
[0056] First, the received multi-dimensional input tensor consists of feature vectors of multiple channels and multiple time steps. Each channel corresponds to a specific EEG acquisition point and has been given a channel embedding vector through the feature encoding step. The system quantifies the similarity of time-frequency information between channels by calculating the cosine similarity between each pair of channel embedding vectors. At the same time, the system also calls the predefined scalp space position mapping table to obtain the geometric distance between any two channels, and converts the distance information into a distance weight through a Gaussian attenuation function. On this basis, the system weighted averages the cosine similarity to construct a channel correlation matrix. Each element of the matrix is used to characterize the overall synergy between channel pairs. The higher the value, the more consistent the two channels are in spectral structure and physically close to each other.
[0057] Next, the channel correlation matrix is not only used as an evaluation indicator, but is directly involved in the generation process of the convolution kernel in the spatial convolution unit. Specifically, based on the channel correlation matrix, the system combines the variance of the channel features in the current input tensor to adaptively adjust the weight distribution of the convolution kernel, so that the convolution kernel can preferentially capture the information flow path between highly correlated channels during the sliding operation. The convolution operation is expanded in two dimensions on the channel dimension of the input tensor. Each convolution kernel processes vectors of multiple channels to extract feature structures that represent the channel space interaction pattern. The tensor output after convolution is called the spatial correlation feature tensor. Compared with the original input, this tensor emphasizes the collaborative structural information across channels more and is regarded as the original basis for time series modeling in subsequent stages.
[0058] The spatial correlation feature tensor is then input into the gated recurrent unit. The gated recurrent unit adopts a recurrent neural network architecture based on a gating mechanism, such as GRU (gated recurrent unit) or LSTM (long short-term memory network), which can be recursively updated in multiple time steps to capture the feature evolution law in the time dimension. In the present invention, a channel attention gating mechanism is introduced on the basis of the traditional recursive framework. The mechanism uses the previously constructed channel correlation matrix as a reference, calculates the average correlation of each channel, and normalizes it as the channel weight coefficient. These weight coefficients are applied to the input gate and update gate of the state transfer at each time step to dynamically adjust the transmission intensity of the state information of each channel, thereby ensuring that the network pays more attention to the historical evolution trajectory of highly correlated channels during the time series modeling process. The output time series evolution feature tensor contains the fusion information of spatial dependency and temporal dynamics, and has a high feature recognition ability.
[0059] Finally, the time series evolution feature tensor is input into the residual connection structure to enhance network stability. In this structure, the network backbone path continues to perform standard linear transformation or further neural layer processing, while the skip path directly passes the initial input and is processed by batch normalization operation and nonlinear transformation function in the process. Batch normalization is used to standardize the distribution of features in each training batch to alleviate the problem of internal covariate shift, while the nonlinear transformation uses the ReLU activation function to ensure the nonlinear expression ability of the signal. The outputs of the main path and the skip path are fused into a residual fused feature tensor by element-by-element addition at the end of the structure, which not only retains the original information but also introduces deep features. The fused tensor is then used in the subsequent attention mechanism weighting and classification inference steps to provide a well-structured and fully expressed feature input for identifying the attention state. The reference implementation code is as follows: import torch import torch.nn as nn import torch.nn.functional as F class EEGAttentionNetwork(nn.Module): def __init__(self, num_channels, time_steps, embedding_dim, hidden_dim, num_classes, scalp_distance_matrix): super(EEGAttentionNetwork, self).__init__() self.num_channels = num_channels self.embedding_dim = embedding_dim self.hidden_dim = hidden_dim self.time_steps = time_steps # Initialize the embedding vector for each channel self.channel_embeddings = nn.Parameter(torch.randn(num_channels, embedding_dim)) # Channel space position distance matrix: shape [number of channels, number of channels] self.register_buffer('distance_matrix', scalp_distance_matrix) # BatchNorm and ReLU for the residual path self.bn = nn.BatchNorm1d(num_channels) self.relu = nn.ReLU() # GRU unit for temporal modeling self.gru = nn.GRU(input_size=embedding_dim * num_channels, hidden_size=hidden_dim, batch_first=True) # Attention mechanism parameters self.attn_layer = nn.Linear(hidden_dim, 1) # Final classifier self.classifier = nn.Linear(hidden_dim, num_classes) def compute_channel_correlation(self): # Normalize channel embeddings and calculate cosine similarity between channels normed_embeddings = F.normalize(self.channel_embeddings, dim=1) # [C, D] cosine_similarity = torch.matmul(normed_embeddings, normed_embeddings.T) # [C, C] # Distance weighting: using Gaussian decay function to simulate spatial proximity gaussian_weight = torch.exp(-self.distance_matrix ** 2) # Channel correlation matrix = similarity × distance weighting channel_corr = cosine_similarity * gaussian_weight # [C, C] return channel_corr def dynamic_spatial_aggregation(self, input_tensor, correlation_matrix): # Use the correlation matrix as attention weights to weight the input by channel B, C, T = input_tensor.shape aggregated = torch.einsum('bcf,cd->bdf', input_tensor,correlation_matrix) # [B, T, D] return aggregated def forward(self, input_tensor): # Input: input_tensor [B, C, T] B, C, T = input_tensor.shape input_tensor = input_tensor.permute(0, 2, 1) # Convert to [B, T, C] # Build channel correlation matrix channel_corr = self.compute_channel_correlation() # [C, C] # Construct dynamic channel features [C, D] fused_embeddings = torch.matmul(channel_corr, self.channel_embeddings) # [C, D] # Expand the time step input to features with embedding dimensions [B, T, C, D] input_tensor_expanded = input_tensor.unsqueeze(-1) * fused_embeddings.view(1, 1, C, -1) # Convert to [B, T, C, D] -> [B, T, C*D] input_tensor_flat = input_tensor_expanded.view(B, T, C *self.embedding_dim) # Residual path preparation: BN + ReLU residual = input_tensor_flat.clone() normed = self.bn(input_tensor_flat.view(-1, C *self.embedding_dim)).view(B, T, -1) activated = self.relu(normed) fused = activated + residual # Tensor after residual connection # GRU processes time series, the output shape is [B, T, H] rnn_out, _ = self.gru(fused) # The attention mechanism calculates the importance weight of each time step attn_weights = F.softmax(self.attn_layer(rnn_out), dim=1) #[B, T, 1] context_vector = torch.sum(attn_weights * rnn_out, dim=1) # Shape after weighted summation [B, H] # Classification output logits = self.classifier(context_vector) # [B, number of categories] probs = F.softmax(logits, dim=1) # Convert to probability distribution # Confidence score = probability value of the largest category confidence = torch.max(probs, dim=1)[0] return probs, confidence The above code implements a multi-channel convolutional recurrent neural network structure for EEG time series, which aims to identify the attention state in different time periods and output classification results and confidence scores. The model first represents the feature distribution of each EEG channel through a predefined channel embedding vector, and further calculates the cosine similarity between all channels to quantify their correlation in the feature space. In order to introduce physical structure information, the model also introduces the channel distance in the scalp space, and converts it into a weight coefficient through a Gaussian function, which is used to weight the correlation between channels, thereby constructing a channel correlation matrix.
[0060] The correlation matrix is used to dynamically generate the weighted structure of the spatial convolution kernel and perform feature aggregation operations on the channel dimension of the input data. The aggregated results are sent to the gated recurrent neural network as the input sequence for time series modeling. The model can learn the attention change rules in time series through recursive updates of time steps. In the residual structure, the original time series features are jump-connected with the normalized features processed by the activation function, which enhances the expression ability of information and maintains the stability of the network. Next, the model calculates the contribution weight of each time step to the final classification through the attention mechanism, and generates a context vector as the basis for classification. The final output is the classification probability of the attention state and the confidence score based on the maximum category probability, which is used to reflect the stability and reliability of the classification results.
[0061] Step S104: During the model training process, a cross-time-step attention mechanism is introduced to weight the feature contribution of each time segment, and based on the classification confusion of the current training batch, the category weight coefficient in the loss function is dynamically adjusted to enhance the recognition sensitivity of the attention transition state.
[0062] In step S104, two key mechanisms are introduced in the model training process, namely the cross-time-step attention mechanism and the dynamic loss weight adjustment strategy based on classification confusion. These two mechanisms work together to improve the model's ability to recognize fuzzy boundaries or transition states during changes in attention states, and to enhance the robustness and adaptability of the training process.
[0063] The introduction of the attention mechanism is to enable the model to automatically identify and focus on those segments in the time series that are more discriminative for judging the state of attention. In EEG signals, different time segments do not contribute equally to the final state classification, especially in the process of transitioning from concentration to dispersion (or reverse), the signal characteristics may be manifested as low amplitude, low stability or local disturbance. If the model treats all time steps equally, it is easy to mask the recognition effect of these key segments. Therefore, in the training stage, a learnable weight is assigned to each time segment, and the weight value is calculated by the attention module embedded in the network. This module is usually composed of a layer of fully connected network or dot product attention structure, with the input as the feature representation corresponding to the time step and the output as the attention score of the time step. In each round of training, these attention scores are normalized and acted on the feature representation of each time step as weighting coefficients, so that the model can give priority to the part with higher information content in back propagation, effectively improving the pertinence of feature extraction and the separability of classification boundaries.
[0064] At the same time, in order to further solve the problem of imbalance between sample categories or inaccurate recognition of transition states, this step also introduces a dynamic loss function adjustment mechanism based on classification confusion. In the actual training process, some categories may have a high misclassification rate due to a small number of samples or low discrimination. In order to avoid the problem of overfitting the easy-to-classify samples and insufficient learning of difficult-to-classify samples during training, after each batch of training data completes the forward calculation, the system will automatically analyze the prediction accuracy and confusion matrix distribution of each category in the current batch. If it is found that some categories are frequently misjudged by other categories, the system will increase the weight of the category in the loss function, so that the error of the category during back propagation has a greater impact on the update of model parameters, thereby prompting the model to more actively correct its judgment strategy. The adjustment of weights can be dynamically calculated according to the set rules, such as setting the growth rate according to the misjudgment rate of the category or using exponential decay to balance historical information and current performance.
[0065] Through the combined effect of the attention mechanism and the dynamic loss adjustment mechanism, the model can not only accurately capture the discriminative power of temporal features at different stages, but also adaptively guide the learning focus towards those difficult-to-identify transition state samples, so that the final attention state classifier has stronger stability and generalization ability when facing real complex EEG data.
[0066] For example, in the model training phase, first for each time series sample, the structure of the input tensor is: if the time window is divided into time steps, the number of EEG channels is , the feature dimension of each step is , then the input tensor dimension is After processing by spatial convolution and gated recurrent unit, each time step will get a high-level representation that combines spatial and temporal features, denoted as ,in is the hidden state dimension, usually set to 128 or 256.
[0067] Next, we introduce the cross-time step attention mechanism. The specific approach is to add each Mapped to a scalar attention score. It is recommended to use a simple and differentiable attention function, that is, use a shared fully connected layer to achieve linear transformation, and then connect the hyperbolic tangent function for nonlinear transformation. The calculation process can be expressed as: 1. Features of each time step Through the weight vector and the bias term Calculating attention scores ; 2. All time steps Normalized by softmax function to get normalized attention weight ,in ,and ,in, is the total number of time steps; For the Attention scores for time steps; After obtaining the attention weights for each time step, these weights are used to perform weighted summation on the feature representations of all time steps to obtain the context feature vector of the entire sequence , this feature vector will be used as the input of the subsequent classification layer for the prediction of the final state label, where is the total number of time steps.
[0068] The above-mentioned attention mechanism automatically learns the importance of each time period to the overall classification judgment, so that the model can focus more learning resources on key time segments during back propagation, such as the unstable state before and after attention conversion, thereby improving the classification sensitivity and boundary accuracy.
[0069] On this basis, a dynamic loss function weighting mechanism is introduced to enhance the robustness to class imbalance and ambiguous samples. In each mini-batch of each round of training, the system will first complete the forward propagation, record the confusion between the predicted results and the true labels, and form the confusion matrix of the batch. The confusion matrix is a two-dimensional array that records the number of times each actual category is misclassified as another category.
[0070] For categories with high confusion rates, for example, a "transition attention state" has more than If a sample is misclassified as "low attention state", the loss weight of this category is increased. A feasible strategy is: Let the basic category weight be 1.0. If the misjudgment rate of a certain category exceeds the threshold (such as ), then the additional misjudgment rate is multiplied by the adjustment coefficient (recommended setting is 1.5 to 2.0), that is, the final category loss weight is: Class i weight False positive rate in, is an empirically set adjustment factor that controls the sensitivity of the model response. For example, if the misclassification rate of a certain category is , then the error of this category in the final loss function will be magnified to about 1.6 times, guiding the model to more actively optimize the classification boundary of this category.
[0071] The weight adjustment process does not affect the convergence of the training process, because the standard cross entropy loss function is still used as the basic loss structure, and only the sample category weights are dynamically updated. This mechanism calculates and adapts the current model performance in real time at each batch level, without the need for preset category weights or manual intervention.
[0072] The attention mechanism and the dynamic loss weight adjustment mechanism can be executed synchronously in the main training loop. The former enhances the discrimination ability of temporal local features, while the latter improves the learning efficiency of the model in areas with class imbalance and fuzzy boundaries. The two are combined to form a complete optimization strategy.
[0073] All operations in this step can be directly coded and implemented in the deep learning framework. The dependent functions only include basic linear transformation, activation function, softmax normalization, weighted summation and cross-extraction loss calculation. The required parameters can be automatically optimized by gradient descent during the training process.
[0074] Furthermore, in the model training process, a cross-time-step attention mechanism is introduced to weight the feature contribution of each time segment, and based on the classification confusion of the current training batch, the category weight coefficient in the loss function is dynamically adjusted to enhance the recognition sensitivity of the attention transition state, including: By analyzing the attention distribution of each sample at each time step, its attention entropy value is calculated as an indicator to measure the concentration of the sample's attention and the uncertainty of the time segment; Mark samples with higher attention entropy as potential uncertain samples, and count the fluctuation of confidence in the corresponding category distribution in the model output as a measure of the instability of the category discrimination; For the category with frequent uncertain samples, a higher weight is set in the loss function. At the same time, a specific amplitude of input disturbance is introduced to the samples of this category, and the amplitude of change of its output confidence is observed, and the disturbance sensitivity is determined accordingly. According to the size of the perturbation sensitivity, the learning rate update factor is dynamically adjusted for the category to achieve a smoother and more focused optimization path for categories with high discriminant instability, thereby improving the model's recognition ability in the boundary area of the attention state.
[0075] In the neural network modeling and reasoning method for attention state recognition based on EEG sequences described in the present invention, in order to improve the model's ability to recognize the transition area of attention state, a training optimization process with uncertainty recognition as the core, combined with attention mechanism, loss adjustment and learning rate control, is designed in the model training stage.
[0076] First, during the training process, the model obtains the contribution of each time segment to the final classification result through the cross-time step attention mechanism. These attention weights are calculated for the feature representation of each time step in each sample sequence to form a set of normalized weight distributions. On this basis, in order to quantify the concentration of the sample's attention distribution, the system uses the attention weight distribution of each sample as a probability distribution input and obtains its attention entropy value through the standard information entropy calculation method. Attention entropy reflects the degree of attention dispersion of the sample: if the attention weight is concentrated in a few time steps, the entropy value is low, indicating that the model has a clear basis for judging the sample; if the weight is close to an average distribution between multiple time steps, the entropy value is high, indicating that the model cannot locate the key information area and there is a strong uncertainty. This attention entropy can be directly used as the uncertainty score of the current sample.
[0077] Subsequently, the model marks the samples with higher attention entropy in the current training batch and further analyzes the fluctuation of the confidence of the classification output of these samples. This analysis is based on the probability distribution of each category output by the model for each prediction of the sample, from which the maximum category confidence and its variance fluctuation in consecutive training cycles are extracted. If a category frequently appears in the prediction results of high entropy samples and the corresponding confidence of this category fluctuates greatly, it can be considered that the discrimination boundary of this category is not yet stable and the model has not effectively learned its key criteria.
[0078] To deal with such categories with high discriminant instability, the system will temporarily increase its category weight in the loss function in the current training cycle, so that this category occupies a higher gradient ratio in the model parameter update, thereby guiding the model to further learn its discriminant features. At the same time, in order to more accurately identify the robustness of the model to this category, it is necessary to introduce a certain amplitude of input perturbation and perform sensitivity tests on samples of this category. Perturbation can be achieved by adding small random perturbations or perturbation masks to the original input features to ensure that the response changes of the model output confidence are observed without changing the label attributes. By comparing the change amplitude of the model output confidence before and after the perturbation, a perturbation sensitivity index can be obtained, which indicates the response strength of the category to small input changes.
[0079] Finally, the system dynamically sets the learning rate adjustment factor for each category based on the perturbation sensitivity. For categories with high perturbation sensitivity, the system will automatically reduce the amplitude of its learning rate change to reduce the drastic fluctuations in model parameters caused by unstable samples and maintain the smooth progress of the optimization path; for categories with low perturbation sensitivity, the learning rate update speed is appropriately increased to ensure that the model does not stagnate on easy-to-classify samples. Without changing the main training logic, this strategy introduces a multi-layer adaptive adjustment mechanism driven by uncertain samples to the training process, which can significantly improve the model's classification performance for boundary samples and fuzzy states, while avoiding overfitting of stable categories.
[0080] The following is the reference implementation code of the uncertainty training optimization strategy with attention entropy as the core: import torch import torch.nn.functional as F import numpy as np # ========== Input Assumptions ========== # attention_weights: Tensor[batch_size, T] represents the attention weight of each sample across T time steps # predictions: Tensor[batch_size, num_classes] represents the predicted probability distribution of each sample # labels: Tensor[batch_size] represents the true label of each sample # inputs: Tensor[batch_size, channels, T, features] represents the original input tensor # model: defined neural network model (with attention structure) # past_confidences: List[List[float]], the confidence record of each category in the first few epochs, used to evaluate category instability # Step 1: Calculate attention entropy def compute_attention_entropy(attention_weights, epsilon=1e-8): """ The entropy formula is: H = -Σ(p_t * log(p_t)), where p_t is the attention weight at the tth time step.
[0081] The larger the entropy value, the more distracted the attention is, that is, the more uncertain the sample is.
[0082] """ log_weights = torch.log(attention_weights + epsilon) entropy = -torch.sum(attention_weights * log_weights, dim=1) #Calculate once for each sample return entropy # [batch_size] # Step 2: Mark high uncertainty samples def identify_uncertain_samples(entropy, quantile=0.75): #Use the quantile of attention entropy as the threshold and select high entropy samples as uncertain samples threshold = torch.quantile(entropy, quantile) mask = entropy >= threshold return mask # [batch_size], Boolean value # Step 3: Update the category weights in the loss function def update_class_weights(predictions, labels, uncertain_mask, past_confidences, alpha=1.5): #Dynamically increase the weight of the loss function for categories with high frequency and large confidence fluctuations num_classes = predictions.shape[1] class_weights = torch.ones(num_classes) # Initially all are 1.0 # Filter out the prediction results of uncertain samples uncertain_indices = torch.where(uncertain_mask)[0] if uncertain_indices.numel() == 0: return class_weights # If there is no uncertain sample, return the default weight predicted_classes = torch.argmax(predictions[uncertain_indices],dim=1) for c in range(num_classes): count_c = (predicted_classes == c).sum().item() if count_c > 0: # Introduce historical fluctuations in confidence conf_var = np.var(past_confidences[c]) if len(past_confidences[c]) > 1 else 0.0 # Increase in category weight = α × sample proportion × (1 + volatility) class_weights[c] += alpha * (count_c / len(uncertain_indices)) * (1 + conf_var) return class_weights # Tensor[num_classes] # Step 4: Calculate perturbation sensitivity def compute_perturbation_sensitivity(model, inputs, labels, num_classes, epsilon=0.01): #Perturb each category of samples and observe the changes in confidence to measure the sensitivity of the perturbation sensitivity = torch.zeros(num_classes) lr_factors = torch.ones(num_classes) for c in range(num_classes): idx = (labels == c) if idx.sum() == 0: continue original = inputs[idx] perturb = original + epsilon * torch.randn_like(original) #Add perturbation with torch.no_grad(): pred_original = F.softmax(model(original), dim=1) pred_perturb = F.softmax(model(perturb), dim=1) conf_orig = torch.max(pred_original, dim=1)[0] conf_pert = torch.max(pred_perturb, dim=1)[0] delta = torch.abs(conf_orig - conf_pert).mean() # Set the sensitivity index sensitivity[c] = delta # Learning rate scaling: high sensitivity → slow; low sensitivity → fast lr_factors[c] = 1.0 / (1.0 + delta.item()) # Ensure that the scaling factor is between (0,1] return lr_factors # [num_classes] # Comprehensive application (example process during training): # attention_entropy = compute_attention_entropy(attention_weights) # uncertain_mask = identify_uncertain_samples(attention_entropy) # class_weights = update_class_weights(predictions, labels,uncertain_mask, past_confidences) # lr_factors = compute_perturbation_sensitivity(model, inputs,labels, num_classes=predictions.size(1)) # Use class_weights to weight the cross entropy loss function # Use lr_factors to adjust the learning rate of samples related to each category As can be seen from the above code, the operation of introducing the cross-time step attention mechanism, dynamic loss function adjustment and category adaptive learning rate control in the model training process includes the following steps: Based on the attention mechanism, the attention weight distribution of each training sample at each time step is obtained, and the distribution is normalized to a probability distribution form that satisfies the sum of 1. Let The attention distribution of samples is ,in is the time step index, is the total number of time steps, and its attention entropy is calculated according to the following formula 1 : in, Representation sample At time step The attention weights on , output by the network attention layer, Reflects the concentration or dispersion of the sample's attention distribution; According to the preset quantile threshold , select the one that satisfies The samples are taken as uncertainty samples, and the samples predicted for each category in the current training batch are counted. The proportion of uncertainty samples , combined with the confidence history data of the corresponding category in the previous set period, calculate the confidence fluctuation variance of the category , to obtain the adjusted weight of this category in the loss function , calculated by the following formula 2: in, is the preset scaling factor, for example; Obtained through the statistics of uncertainty sample prediction results, Calculation based on the historical confidence sequence of the current model output; For each category , the introduction range is The random disturbance of acts on the input features of the corresponding samples of this category, and the maximum confidence of the output prediction before and after the disturbance is calculated respectively and , to obtain the disturbance sensitivity index , calculated according to the following formula 3: in, Indicates that the label is a category All training samples of and are the maximum predicted probability values before and after the disturbance respectively; according to the disturbance sensitivity index , set category The learning rate adjustment factor , which is calculated as: The category learning rate adjustment factor is used to adjust the class learning rate in the optimizer The relevant gradient update amplitude is proportionally controlled, so that a slower update rate is used for categories with strong perturbation response, so as to improve the robustness and convergence stability of the model for boundary samples of this type.
[0083] Step S105: After the training is completed, the target EEG time series data of any length is received, and the trained neural network model is used for inference to output the corresponding attention state classification label and its confidence score value; wherein the confidence score value is calculated based on the attention-weighted feature representation and the classification boundary distance, and is used to quantify the stability and credibility of the classification result.
[0084] In step S105, after the neural network model is trained, it will be used to perform actual reasoning operations on new EEG time series data, thereby realizing automatic recognition and credibility assessment of attention states. The core of this step is to ensure that the model has the ability to process variable-length input data in practical applications, and can output stable and interpretable classification results and confidence scores for subsequent system decision-making or feedback.
[0085] First, when receiving new EEG time series data, there is no need to limit its time length. This means that the EEG signals generated by the user in different time periods and different tasks can be directly fed into the model as input without prior truncation or fixed-length padding. This capability comes from the loop structure and attention mechanism introduced in the early training process, which makes the model naturally adaptable to variable-length sequences. To be consistent with the training phase, the new data needs to undergo the same wavelet transform and time-frequency feature extraction as the original data, and the channel position embedding and tensor construction are performed in the same way to ensure that the structure and feature space of the input tensor are exactly the same as the training data.
[0086] After the input data is constructed into a compliant multi-dimensional tensor, it is input into the trained neural network model. The model will pass through the spatial convolution layer, gated recurrent unit, residual connection path and attention weighting module in sequence to fully extract and integrate the spatiotemporal joint features in the input sequence. During this process, the attention mechanism will calculate the attention score of each time step again, automatically identify the time segments in the current EEG sequence that are highly correlated with the attention state, and give them a higher influence weight in the inference results. Finally, the high-dimensional features of the entire sequence are compressed into a context vector representing global information and sent to the output layer for classification prediction.
[0087] The final output of the model includes two parts: one is the attention state classification label, usually one of the states such as "highly concentrated", "slightly distracted" or "severely distracted"; the other is the corresponding confidence score value, which is used to reflect the stability and credibility of the model's prediction results. The generation of confidence scores is based on the degree of match between the attention-weighted features inside the model and the discrimination boundaries learned during the training process. The specific implementation method is that the model measures the proximity of the distribution position of the current input in the feature space to the boundaries of each category. If the current feature has significant discrimination near a certain category boundary, a higher confidence score is output, otherwise a lower value is output. This scoring mechanism provides a means of quantifying the reliability of model reasoning without the need for manual labeling, and can serve as an important reference for judging whether the results are credible.
[0088] The entire reasoning process can be automatically implemented in a standard deep learning framework without modifying the network structure or parameters. All that is required is a trained and saved model parameter file and an EEG signal processing and preprocessing module connected at the input. This step has a clear design, a stable process, and clear parameters, ensuring that it has industrial-grade deployment conditions and can be directly applied to multiple scenarios such as real-time monitoring of attention status, fatigue warning systems, and brain-computer interface feedback.
[0089] Furthermore, the inference using the trained neural network model to output the corresponding attention state classification label and its confidence score value includes: Based on the distance between the last layer of hidden feature vectors in the neural network model and each predefined classification hyperplane, the geometric projection distance of the hidden feature vector relative to the classification boundary in the feature space is calculated, and the geometric projection distance is used to construct a continuous confidence score value for each sample at the current moment. The larger the confidence score value, the closer the sample is to the center of the target category in the feature space, and the more reliable the classification result; Record the confidence score value sequence corresponding to each continuous time step, calculate the sliding standard deviation and maximum amplitude change value of the sequence, and use it to represent the confidence fluctuation trend of the current sample in the time interval. The fluctuation trend is used as an indicator of the stability of the evaluation model's judgment on the sample time series; When the confidence fluctuation trend exceeds a preset threshold, the model is automatically triggered to perform a secondary recognition operation on the sample, wherein the secondary recognition operation includes reactivating the attention mechanism to perform feature weighting and perform a second classification prediction, so as to improve the recognition accuracy of unstable samples; In the secondary recognition result, if the confidence score is still lower than the confidence rejection threshold, a rejection flag is output instead of a classification label to enhance the robustness of the system and reduce the risk of misjudgment in critical states or fuzzy samples. The rejection flag is used by an external system to trigger further judgment or manual review process.
[0090] In the neural network modeling and reasoning method for attention state recognition based on EEG sequences described in the present invention, the final reasoning step not only outputs the classification label of the attention state, but also constructs a continuous confidence score value to measure the credibility of the classification result and strengthen the subsequent decision-making mechanism. The reasoning step consists of multiple closely related sub-processes, and the output of each sub-process serves as the basis for subsequent judgment and decision-making, forming a classification decision process with robustness and judgment precision.
[0091] In the inference stage, the hidden feature representation of the input sample in the last layer of the network structure is first extracted through the trained neural network model. The hidden feature vector is located in a high-dimensional feature space and represents the semantic distribution state of the current input sample from the perspective of network discrimination. For each target classification category, the system pre-trains and obtains the corresponding classification hyperplane representation. By calculating the geometric projection distance of the hidden feature vector to each classification hyperplane, the spatial distance of the sample relative to the discrimination boundary of different categories can be obtained. The system selects the distance from the sample to its predicted category hyperplane as the basis for its confidence score. The score is a continuous value that reflects the "confidence" of the sample being judged as the current category. The larger the projection distance, the closer the sample is to the category center area in the feature space, the clearer the discrimination result, and the higher the score.
[0092] This scoring mechanism not only works on a single time point. In the continuous EEG time series analysis, the system records the confidence scores calculated at each time step in order to form a set of scoring value sequences that change over time. In order to further evaluate the judgment stability of the sample throughout the time series, the system calculates the sliding standard deviation and the maximum value change amplitude of the scoring sequence to determine the fluctuation trend of the confidence score. The more drastic the score fluctuation, the less stable the model's judgment of the sample in the time dimension, and there are problems with unclear identification of boundary samples or fuzzy states. This fluctuation trend is used to trigger a more stringent judgment process.
[0093] Once the score fluctuation trend indicator exceeds the stability threshold set by the system, the system will automatically start a secondary recognition process for the sample. This process does not simply repeat the initial judgment, but reactivates the attention mechanism in the model. The attention mechanism will re-weight the importance of different time steps, different channels, or different feature areas according to the feature structure of the current sample, and re-integrate features and classify predictions accordingly. This method can guide the model to perform a more focused and detailed judgment process when facing the initial recognition of unstable samples, thereby improving the reliability of the final classification results.
[0094] If, in the result of the second recognition, the confidence score corresponding to the target category still does not reach the minimum trust threshold defined by the system, the system will not output the classification label of the sample, but will output a rejection sign. The rejection sign is a clear logical signal that tells the system that the current sample is in a cognitive fuzzy zone and lacks sufficient basis for judgment. In this case, the system submits the sample to an external module for further processing, including but not limited to delayed judgment, manual confirmation, or activation of other auxiliary models for collaborative judgment, to avoid systemic risks caused by misjudgment.
[0095] This reasoning and confidence mechanism not only enhances the model's ability to perceive critical state samples, but also realizes the self-steady-state regulation and rejection ability of the classification system through a structured scoring and judgment process, greatly improving the reliability and generalization ability of the neural network model for high-risk identification tasks in practical applications.
[0096] A second embodiment of the present application provides an electronic device, the electronic device comprising: processor; The memory is used to store a program. When the program is read and executed by the processor, it executes a neural network modeling and reasoning method for attention state recognition based on EEG sequences provided in the first embodiment of the present application.
[0097] The third embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon. When the program is executed by a processor, a neural network modeling and reasoning method for attention state recognition based on EEG sequences provided in the first embodiment of the present application is executed.
[0098] Although the present application is disclosed as above in the form of a preferred embodiment, it is not intended to limit the present application. Any technical personnel in this field may make possible changes and modifications without departing from the spirit and scope of the present application. Therefore, the scope of protection of the present application shall be based on the scope defined by the claims of the present application.
Claims
1. A neural network modeling and reasoning method for attention state recognition based on EEG sequences, characterized in that: include: The EEG time series signals obtained by sampling are processed by wavelet transform to extract sub-band signals of different frequency bands and obtain frequency domain information with multi-scale resolution; Extracting time-frequency domain features of the subband signals, and encoding the extracted features into a multi-dimensional input tensor with channel embedding vectors according to the distribution position of each channel in the scalp space; Input the multi-dimensional input tensor into a multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit and a residual connection structure, wherein the spatial convolution unit is used to extract spatial correlation features between different channels, the gated recurrent unit is used to learn the evolution law of time series features, and the residual connection structure is used to alleviate the gradient vanishing problem in the deep network training process; During the model training process, a cross-time-step attention mechanism is introduced to weight the feature contribution of each time segment, and the category weight coefficient in the loss function is dynamically adjusted based on the classification confusion of the current training batch to enhance the recognition sensitivity of the attention transition state. After training is completed, target EEG time series data of arbitrary length is received, and the trained neural network model is used for inference to output the corresponding attention state classification label and its confidence score value; wherein the confidence score value is calculated based on the attention-weighted feature representation and the classification boundary distance, and is used to quantify the stability and credibility of the classification result.
2. The neural network modeling and reasoning method for attention state recognition based on EEG sequences according to claim 1 is characterized in that: The time-frequency domain feature extraction of the sub-band signal and encoding the extracted feature into a multi-dimensional input tensor with a channel embedding vector according to the distribution position of each channel in the scalp space include: The distribution position of each channel in the scalp space is converted into a three-dimensional Cartesian coordinate form, and a channel space adjacency matrix is constructed based on the three-dimensional coordinates to reflect the physical neighbor relationship between the channels; The channel space adjacency matrix is input into a graph convolutional network as an edge connection weight of a graph structure. The graph convolutional network uses the frequency domain features of each channel as an initial node input, performs graph embedding modeling on the features of each channel, and generates a first channel embedding vector that integrates the channel time-frequency response and the spatial structure; Based on the average signal-to-noise ratio and power intensity index of each channel in the target sub-band frequency band recorded in the training phase, the characteristic quality descriptor of the channel in the frequency band is extracted and encoded into a set of spectral characteristic vectors; The first channel embedding vector is connected and fused with the corresponding spectral characteristic vector in dimension to form a second channel embedding vector containing multimodal representation contents such as spatial position information, frequency domain response structure and signal-to-noise characteristics. The second channel embedding vector is used as the basis for constructing the representation of each channel dimension in the multidimensional input tensor, so as to enhance the modeling ability of subsequent neural network structures for channel heterogeneity and task-related features.
3. The neural network modeling and reasoning method for attention state recognition based on EEG sequences according to claim 1 is characterized in that: The step of inputting the multi-dimensional input tensor into a multi-channel convolutional recurrent neural network model composed of a spatial convolution unit, a gated recurrent unit, and a residual connection structure includes: Based on the multidimensional input tensor, a channel correlation matrix is constructed, where the channel correlation matrix is obtained by calculating the cosine similarity between channel embedding vectors corresponding to different channels in the multidimensional input tensor, and is weighted in combination with the distance information of the channels in the scalp space, so as to reflect the time-frequency feature synergy and spatial neighbor relationship between the channels; The channel correlation matrix is used as a parameter to dynamically generate a convolution kernel weight of a spatial convolution unit, wherein the spatial convolution unit performs a two-dimensional convolution operation on the channel dimension of the multidimensional input tensor to extract spatial dependency features between channels and output a spatial correlation feature tensor, wherein the spatial correlation feature tensor enhances cross-channel coupling structure information based on the original multidimensional input tensor; The spatial correlation feature tensor is input into a gated recurrent unit, which adopts a gated recurrent structure to recursively process the spatial correlation feature tensor of each time step, and introduces a channel attention gating mechanism. The channel attention gating mechanism calculates the channel weight coefficient based on the average correlation of each channel in the channel correlation matrix, which is used to adjust the state transfer ratio of each channel in the recursive update of each time step, and outputs the time series evolution feature tensor; The time series evolution feature tensor is input into the residual connection structure. While the residual connection structure transmits the processing result in the main branch network path, batch normalization and nonlinear transformation operations are introduced in the jump path. The batch normalization is used to standardize the distribution of the time series evolution feature tensor. The nonlinear transformation operation adopts the ReLU activation function to improve the signal expression ability. Finally, the output of the jump path is element-by-element added with the output of the main branch path to generate a residual fusion feature tensor, which serves as the input basis for the subsequent attention mechanism and classification reasoning steps.
4. The neural network modeling and reasoning method for attention state recognition based on EEG sequences according to claim 1 is characterized in that: In the model training process, a cross-time-step attention mechanism is introduced to weight the feature contribution of each time segment, and the category weight coefficient in the loss function is dynamically adjusted based on the classification confusion of the current training batch to enhance the recognition sensitivity of the attention transition state, including: By analyzing the attention distribution of each sample at each time step, its attention entropy value is calculated as an indicator to measure the concentration of the sample's attention and the uncertainty of the time segment; Mark samples with higher attention entropy as potential uncertain samples, and count the fluctuation of confidence in the corresponding category distribution in the model output as a measure of the instability of the category discrimination; For the category with frequent uncertain samples, a higher weight is set in the loss function. At the same time, a specific amplitude of input disturbance is introduced to the samples of this category, and the amplitude of change of its output confidence is observed, and the disturbance sensitivity is determined accordingly. According to the size of the perturbation sensitivity, the learning rate update factor is dynamically adjusted for the category to achieve a smoother and more focused optimization path for categories with high discriminant instability, thereby improving the model's recognition ability in the boundary area of the attention state.
5. The neural network modeling and reasoning method for attention state recognition based on EEG sequences according to claim 1 is characterized in that: The method of using the trained neural network model to perform reasoning and outputting corresponding attention state classification labels and confidence score values thereof includes: Based on the distance between the last layer of hidden feature vectors in the neural network model and each predefined classification hyperplane, the geometric projection distance of the hidden feature vector relative to the classification boundary in the feature space is calculated, and the geometric projection distance is used to construct a continuous confidence score value for each sample at the current moment. The larger the confidence score value, the closer the sample is to the center of the target category in the feature space, and the more reliable the classification result; Record the confidence score value sequence corresponding to each continuous time step, calculate the sliding standard deviation and maximum amplitude change value of the sequence, and use it to represent the confidence fluctuation trend of the current sample in the time interval. The fluctuation trend is used as an indicator of the stability of the evaluation model's judgment on the sample time series; When the confidence fluctuation trend exceeds a preset threshold, the model is automatically triggered to perform a secondary recognition operation on the sample, wherein the secondary recognition operation includes reactivating the attention mechanism to perform feature weighting and perform a second classification prediction, so as to improve the recognition accuracy of unstable samples; In the secondary recognition result, if the confidence score is still lower than the confidence rejection threshold, a rejection flag is output instead of a classification label to enhance the robustness of the system and reduce the risk of misjudgment in critical states or fuzzy samples. The rejection flag is used by an external system to trigger further judgment or manual review process.
6. The neural network modeling and reasoning method for attention state recognition based on EEG sequences according to claim 1 is characterized in that: The wavelet transform processing is performed on the EEG time series signal obtained by sampling to extract sub-band signals of different frequency bands and obtain frequency domain information with multi-scale resolution, including: After receiving the EEG time series signal, based on a predefined frequency sensitivity mapping function, a target frequency band group associated with the current task is determined, and according to the difference in response entropy of each frequency band group to the change of attention state in the training sample, the wavelet basis function with the strongest adaptability is selected as the wavelet transform mother wavelet of the current sample, wherein the frequency sensitivity mapping function is obtained by analyzing the discriminant information gain of the training set attention state labeled samples in each sub-frequency band, and is used to guide the wavelet function to match the feature expression structure; The wavelet basis function obtained by the selection is used to perform multi-scale decomposition on the EEG time series signal to generate Wave, Wave, Wave, wave and Wave, and introduce a bandpass energy gating operation for each sub-band signal to filter out low-amplitude non-significant frequency bands, output an enhanced sub-band signal set, and use the enhanced sub-band signal set as frequency domain information with multi-scale resolution.
Citation Information
Patent Citations
Auxiliary diagnosis system for patients with disturbance of consciousness based on automatic sleep staging
CN116687422A
Method and system for optimizing few-view-angle neural radiation field based on object pose probe
CN118552615A
Driving control circuit with fault detection function
CN119148608A
RSVP type unbalanced electroencephalogram signal classification method based on adaptive channel mixed attention mechanism and decoupling learning
CN119385578A
Cited By
Harmful gas concentration intelligent identification and detection method
CN120741584A
Intelligent identification and detection method for harmful gas concentration
CN120741584B
Continuous monitoring method and system for multi-modal physiological parameter fusion
CN120753668A
Neural electrophysiological signal analysis method based on music perception
CN120837091A
Traction stretching motion state monitoring method based on signal processing
CN120873496A