Water quality prediction method
By combining long-term time-dependent models, short-term time-dynamic models, feature fusion networks, and capsule networks, this water quality prediction method solves the problems of computational complexity and low accuracy of traditional methods, achieving efficient and accurate water quality prediction and supporting water environment management and protection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HOHAI UNIV
- Filing Date
- 2025-05-30
- Publication Date
- 2026-04-17
AI Technical Summary
Traditional water quality prediction methods are complex to calculate and have low accuracy, making it difficult to effectively handle nonlinear and non-stationary characteristics.
A combination of long-term time-dependent models, short-term time-dynamic models, feature fusion networks, and capsule networks is used to generate target feature vectors for water quality prediction through feature extraction and fusion.
It improves the accuracy and reliability of water quality forecasting, enabling timely reflection of long-term trends and short-term dynamic changes in water quality, providing scientific basis for relevant departments, and helping to formulate effective governance and protection measures.
Smart Images

Figure CN120745892B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of water quality prediction technology, and more specifically to water quality prediction methods. Background Technology
[0002] Water is an essential resource for human economic and social development. my country faces particularly prominent water resource problems: its per capita water resources are only one-quarter of the world average, and their spatial and temporal distribution is uneven. Furthermore, major river systems across the country generally face varying degrees of pollution. Water quality monitoring and forecasting, as key technologies for water environment management, are of great significance for improving the efficiency of water resource governance and ensuring water environment safety. Accurate water quality forecasting not only provides a scientific basis for water environment management decisions but also enables early warning of water quality emergencies, thereby improving the ability to prevent and control water environment risks.
[0003] Traditional water quality prediction methods are typically based on mechanistic models of hydrodynamics and water quality reaction kinetics, such as WASP and QUAL2K. However, while mechanistic models have clear physical meaning, they require extensive parameter calibration and complex boundary conditions, and are difficult to handle nonlinear and nonstationary characteristics. Therefore, the computational process is complex, and the prediction accuracy is relatively low.
[0004] Therefore, providing a water quality prediction method that is simple to calculate and has high accuracy has become an urgent problem to be solved. Summary of the Invention
[0005] In view of this, the present invention provides a water quality prediction method to solve the problem of providing a water quality prediction method with a simple calculation process and high prediction accuracy.
[0006] In a first aspect, the present invention provides a water quality prediction method, the method comprising:
[0007] Obtain historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area;
[0008] Input historical water quality monitoring data into a preset water quality prediction model, and output future water quality prediction data for the target area at a preset time.
[0009] Based on future water quality forecast data, determine the current pollution level of the target area's water quality and output corresponding treatment measures.
[0010] In one optional implementation, the preset water quality prediction model includes a long-term time-dependent model, a short-term time-dynamic model, a feature fusion network, and a capsule network. Historical water quality monitoring data is input into the preset water quality prediction model, which outputs future water quality prediction data for a preset duration corresponding to the target area's water quality, including:
[0011] Historical water quality monitoring data is input into a long-term time-dependent model, and long-term time-dependent features are output.
[0012] Historical water quality monitoring data is input into a short-term dynamic model, and short-term dynamic characteristics are output.
[0013] The feature fusion network is used to fuse long-term time-dependent features and short-term time-dynamic features to generate target fused features.
[0014] The target fusion features are input into the capsule network, and feature extraction is performed on the target fusion features to output the target feature vector;
[0015] Based on the target feature vector, the system outputs the predicted future water quality data for the target area.
[0016] In one optional implementation, a feature fusion network is used to fuse long-term temporal dependency features and short-term temporal dynamic features to generate target fused features, including:
[0017] Both long-term time-dependent features and short-term time-dynamic features are input into multiple feature extraction branches; the size of the convolutional kernels in each feature extraction branch is different.
[0018] For each feature extraction branch, convolution operations are performed on long-term time-dependent features and short-term time-dynamic features, and self-attention calculation is performed to output the initial branch features;
[0019] Feature enhancement processing is performed on each initial branch feature to generate the target branch feature;
[0020] The features of each target branch are fused to generate initial fused features;
[0021] Feature filtering is performed on the initial fusion features to obtain the target fusion features.
[0022] In one optional implementation, the initial fusion features include multiple sub-initial fusion features. Feature filtering is performed on the initial fusion features to obtain target fusion features, including:
[0023] Based on historical water quality monitoring data, calculate proxy water quality prediction data;
[0024] Calculate the correlation coefficient between each sub-initial fusion feature in the initial fusion feature and the proxy water quality prediction data;
[0025] Calculate the information gain of each initial sub-fusion feature on the proxy water quality prediction data; information gain is used to characterize the information contribution of each initial sub-fusion feature to the proxy water quality prediction data.
[0026] The correlation coefficients and information gains of each initial sub-fusion feature are weighted and fused to generate the importance scores of each initial sub-fusion feature.
[0027] Based on the importance scores corresponding to each initial sub-fusion feature, select the sub-target fusion features whose importance scores are greater than a preset score threshold to generate the target fusion feature.
[0028] In one optional implementation, the target fusion features are input into a capsule network, feature extraction is performed on the target fusion features, and a target feature vector is output, including:
[0029] Based on the deformable convolution kernel in the capsule network, deformable convolution operation is performed on the target fusion features to generate multiple feature maps;
[0030] Based on each feature map, generate the capsule vector corresponding to each feature map;
[0031] Based on the vectors of each capsule, construct the primary capsule;
[0032] Based on a preset nonlinear function, a nonlinear transformation is performed on each primary capsule to generate a nonlinear vector;
[0033] Based on a dynamic routing mechanism, each nonlinear vector is converted into a target input vector;
[0034] Pass the target input vector to the higher-level capsules in the capsule network;
[0035] The advanced capsule performs a nonlinear transformation on the target input vector and outputs the target feature vector.
[0036] In one alternative implementation, based on a dynamic routing mechanism, each nonlinear vector is transformed into a target input vector, including:
[0037] Initialize the initial coupling coefficients based on the prior relationships between different water quality characteristics in historical water quality monitoring data;
[0038] An attention-enhancing mechanism is used to weight each nonlinear vector to generate a weighted vector.
[0039] Based on each weighted vector, calculate the prediction vector corresponding to the weighted vector;
[0040] Based on the prediction vector, the initial coupling coefficients are updated to obtain the updated coupling coefficients.
[0041] Calculate attention weights based on the updated coupling coefficients;
[0042] The target input vector is obtained by multiplying the prediction vector by the attention weight.
[0043] In one optional implementation, the long-term time-dependent model includes a multi-layered long-term time-dependent structure. Historical water quality monitoring data is input into the long-term time-dependent model, and long-term time-dependent features are output, including:
[0044] For each time step in each long-term time-dependent structure, obtain the first current input data, the first current hidden state, and the first current cell state corresponding to the current time; the first current hidden state and the first current cell state are calculated based on the historical input data corresponding to the previous time step.
[0045] Based on the first current input data, the first current hidden state, and the first current cell state, calculate the weight information corresponding to the forget gate, input gate, and output gate respectively;
[0046] Based on the weight information corresponding to the forget gate, input gate, and output gate, the forget gate calculation, input gate calculation, and output gate calculation are performed.
[0047] The output of the last long-term time-dependent structure is determined as the long-term time-dependent feature.
[0048] In one optional implementation, the short-term dynamic model includes a standard structure layer and an attention mechanism structure layer; historical water quality monitoring data is input into the short-term dynamic model, and short-term dynamic features are output, including:
[0049] For each time step in the standard structure layer, obtain the second current input data corresponding to the current time and the previous hidden state corresponding to the previous time; the previous hidden state is calculated based on the historical input data corresponding to the previous time.
[0050] Based on the second current input data and the hidden state of the previous time step, calculate the current hidden state corresponding to the current time step;
[0051] Output the hidden state corresponding to each time step in the standard structure layer to generate standard time dynamic features;
[0052] For each time step in the attention mechanism structure layer, obtain the second current input data corresponding to the current time step and the previous hidden state corresponding to the previous time step; the previous hidden state is calculated based on the historical input data corresponding to the previous time step.
[0053] Based on the second current input data and the hidden state of the previous time step, calculate the current hidden state corresponding to the current time step;
[0054] Map the hidden state corresponding to each time step in the attention mechanism structure layer to a new dimension to generate the hidden features corresponding to each time step;
[0055] Each hidden feature is processed to generate an attention gating signal;
[0056] Each attention-gated signal is multiplied element-wise with its corresponding hidden state to obtain the weighted time dynamic features.
[0057] The standard time dynamic features and the weighted time dynamic features are fused to generate short-term time dynamic features.
[0058] In one optional implementation, the preset water quality prediction model is trained based on a target loss function, which includes a prediction sub-loss function, a time series sub-loss function, a trend sub-loss function, an uncertainty sub-loss function, and a physical constraint sub-loss function.
[0059] In one alternative implementation, the target loss function is as follows:
[0060] L_total=α·L_prediction+β·L_temporal+γ·L_trend+δ·L_uncertainty+ε·L_physical;
[0061] L_prediction={0.5(y_t-P_t) 2 ,|y_t-P_t|≤δ|y_t-P_t|-0.5δ 2 ,other};
[0062]
[0063] L_uncertainty=-logp(y_t|P_t,σ_t 2 );
[0064] L_physical=∑λ_i·max(0,g_i(P_t));
[0065] Where L_total is the target loss function, L_prediction is the prediction sub-loss function, L_temporal is the temporal sub-loss function, L_trend is the trend sub-loss function, L_uncertainty is the uncertainty sub-loss function, and L_physical is the physical constraint sub-loss function; α, β, γ, δ, and ε are all weight coefficients; y_t is the true value of the sample during training, P_t is the predicted value of the sample output during training, and δ is the threshold parameter of the prediction sub-loss function; σ_t represents the time gradient operator; 2 To predict the variance; g_i(P_t) represents the physicochemical constraints.
[0066] The water quality prediction method provided in this application obtains historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area: The historical water quality monitoring data is input into a long-term time-dependent model, outputting long-term time-dependent features. The long-term time-dependent model can capture the dependence and change patterns of water quality data over a longer time scale. The historical water quality monitoring data is input into a short-term time-dynamic model, outputting short-term time-dynamic features. The short-term time-dynamic model focuses on analyzing the dynamic changes of water quality data within a short period. It can promptly reflect sudden changes, short-term fluctuations, and the impact of recent human activities or environmental factors on water quality. Based on a feature fusion network, the long-term time-dependent features and short-term time-dynamic features are fused to generate target fused features. This fully utilizes the advantages of both, considering both the long-term evolution trend of water quality and short-term dynamic changes, enabling the generated target fused features to more comprehensively and accurately describe the overall characteristics of water quality, providing richer and more accurate information for subsequent analysis and prediction. The target fused features are input into a capsule network for feature extraction, outputting a target feature vector. Capsule networks can further extract and refine the fused features, highlighting the most critical features for water quality prediction and removing redundant information. The generated target feature vector has higher representativeness and discriminative power, and can more effectively reflect the intrinsic relationship between water quality and various influencing factors, laying a solid foundation for accurate future water quality prediction. Based on the target feature vector, it outputs the future water quality prediction data corresponding to the target area. It can comprehensively consider the long-term trend and short-term dynamic changes of water quality, improving the accuracy and reliability of prediction. Accurate future water quality prediction data can help relevant departments take countermeasures in advance, such as rationally planning water resource utilization and preparing sewage treatment equipment in advance to cope with possible water quality changes. Based on the future water quality prediction data, it determines the current pollution level of the target area and outputs corresponding treatment measures. It can clearly identify the current pollution status and development trend of water quality, thereby outputting targeted treatment measures. This helps relevant departments take timely and effective governance and protection measures, such as strengthening the supervision of pollution sources, implementing water quality improvement projects, and adjusting water resource management strategies, to protect water resources and the environment, ensure water safety, and reduce the adverse effects of water pollution on ecosystems and human health. The above method provides a water quality prediction method with a simple calculation process and high prediction accuracy. Attached Figure Description
[0067] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0068] Figure 1 This is a schematic flowchart of a water quality prediction method according to an embodiment of the present invention;
[0069] Figure 2 This is a schematic flowchart of another water quality prediction method according to an embodiment of the present invention;
[0070] Figure 3 This is a flowchart illustrating the training process of a preset water quality prediction model according to an embodiment of the present invention.
[0071] Figure 4 This is a structural block diagram of a water quality prediction device according to an embodiment of the present invention;
[0072] Figure 5 This is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. Detailed Implementation
[0073] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0074] It should be noted that the water quality prediction method provided in this application embodiment can be executed by a water quality prediction device. This device can be implemented as part or all of a computer device through software, hardware, or a combination of both. The computer device can be a server or a terminal. In this application embodiment, the server can be a single server or a server cluster composed of multiple servers. The terminal in this application embodiment can be a smartphone, personal computer, tablet computer, wearable device, or other intelligent hardware device such as an intelligent robot. The following method embodiments will use an electronic device as an example for explanation.
[0075] According to an embodiment of the present invention, a water quality prediction method embodiment is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0076] This embodiment provides a water quality prediction method that can be used in the aforementioned electronic equipment. Figure 1 This is a flowchart of a water quality prediction method according to an embodiment of the present invention, such as... Figure 1As shown, the process includes the following steps:
[0077] Step S101: Obtain historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area.
[0078] Specifically, electronic devices can acquire historical water quality monitoring data from water quality monitoring stations within and around the target area. This historical data may include pH, dissolved oxygen, chemical oxygen demand (COD), ammonia nitrogen content, turbidity, etc. Different monitoring stations may monitor different water quality parameters, therefore, it is necessary to integrate data from multiple stations to obtain more comprehensive water quality information.
[0079] Step S102: Input historical water quality monitoring data into the preset water quality prediction model and output the future water quality prediction data for the target area for a preset duration.
[0080] Specifically, electronic devices can input historical water quality monitoring data into a preset water quality prediction model. The preset water quality prediction model extracts features from the historical water quality monitoring data and outputs the predicted number of future water quality for a preset time period corresponding to the water quality of the target area based on the extracted features.
[0081] This step will be explained in detail below.
[0082] Step S103: Based on future water quality prediction data, determine the current pollution level of the target area's water quality and output corresponding treatment measures.
[0083] Specifically, electronic equipment can be classified into pollution levels based on national or local water quality standards and relevant pollution indicators. Common indicators include chemical oxygen demand (COD), biochemical oxygen demand (BOD), ammonia nitrogen, total phosphorus, and heavy metal content. Pollution levels are generally classified into several grades, such as light pollution, moderate pollution, heavy pollution, and severe pollution.
[0084] Slight pollution: When future water quality forecasts show that most indicators are close to but do not exceed the corresponding water quality standards, with only a few indicators slightly exceeding the standards, and do not pose a significant threat to the aquatic ecosystem and human health, it can be classified as slightly polluted. For example, COD content exceeds the Class III standard for surface water but is lower than the Class IV standard, while other major indicators are basically within the standards.
[0085] Moderate pollution: If multiple conventional pollution indicators exceed water quality standards by a certain range, and have a certain impact on the aquatic ecosystem, such as signs of mild eutrophication or a decrease in aquatic species, it can be classified as moderate pollution. For example, ammonia nitrogen and total phosphorus levels both exceed the Class IV surface water standard, and COD also shows a significant exceedance.
[0086] Severe pollution: When most pollution indicators are seriously exceeded, the aquatic ecosystem is severely damaged, the water body becomes black and smelly, and a large number of aquatic organisms die, it can be judged as severely polluted. For example, indicators such as BOD, COD, and ammonia nitrogen far exceed the Class V water standard, and the water body has basically lost its usability.
[0087] Severe pollution: If water quality forecast data shows high concentrations of toxic and harmful substances, such as severely excessive heavy metals or the detection of persistent organic pollutants, posing a significant risk to the ecological environment and human health, it is considered severe pollution. In this case, the water body may have already caused irreversible damage to the surrounding ecological environment.
[0088] Electronic devices can output corresponding treatment measures based on the current pollution level. For example, for a lightly polluted level, the corresponding treatment measures could include: Strengthening monitoring and management: increasing the frequency of water quality monitoring and closely monitoring water quality trends. Strengthening supervision of surrounding pollution sources to ensure stable and compliant emissions from industrial enterprises and preventing illegal discharge of wastewater. Ecological restoration measures: appropriately planting aquatic plants such as reeds and cattails around the water body to absorb nutrients and enhance the water's self-purification capacity. Simultaneously, introducing filter-feeding fish such as silver carp and bighead carp can control algae growth. Public education: conducting environmental awareness campaigns to raise the environmental awareness of surrounding residents and reduce pollution of water bodies by domestic sewage and garbage. For example, organizing community outreach activities, distributing environmental protection manuals, and advocating for water conservation and the rational use of fertilizers and pesticides.
[0089] For the current pollution level of moderate pollution, corresponding treatment measures may include: Pollution source control: Imposing time-limited rectification orders on industrial enterprises exceeding emission standards, requiring them to upgrade their pollution control facilities to ensure pollutant emissions meet standards. For domestic sewage, accelerating the construction and upgrading of sewage treatment plants to improve sewage treatment capacity and efficiency. Simultaneously, strengthening the control of non-point source pollution in rural areas, promoting ecological agriculture, and reducing the use of pesticides and fertilizers. Strengthening ecological restoration: Expanding the planting area of aquatic plants to build a diversified aquatic ecosystem. Constructing artificial wetlands can further purify water quality through the synergistic effects of physical, chemical, and biological methods. In addition, regularly aerating water bodies to improve dissolved oxygen levels and promote the growth and metabolism of aerobic microorganisms. Emergency measures: Developing emergency response plans to prevent further deterioration of water quality due to sudden pollution incidents. Stockpiling necessary emergency treatment materials, such as activated carbon and flocculants, to quickly take measures to adsorb and precipitate pollutants when needed.
[0090] In response to the current level of severe pollution, corresponding treatment measures may include: Comprehensive pollution source control: Conduct a comprehensive investigation and remediation of all pollution sources, implement strict environmental law enforcement, and shut down polluting enterprises that show no signs of improvement. Strengthen supervision of sewage treatment plants to ensure they operate at full capacity, and consider adding temporary sewage treatment facilities for emergency treatment of sewage. Ecological restoration projects: Implement large-scale ecological restoration projects, such as sediment dredging, to remove pollutants accumulated at the bottom of the water body and reduce endogenous pollution. Simultaneously, introduce clean water sources for ecological replenishment to improve hydrodynamic conditions and accelerate water body renewal. In addition, release large amounts of beneficial microbial agents to promote the decomposition and transformation of pollutants. Public participation and supervision: Establish a public participation mechanism to encourage citizens to participate in water quality supervision and report pollution activities. Regularly publish water quality information to enhance public confidence and participation in water quality improvement. For example, establish a hotline and online platform for reporting issues to promptly handle and respond to public reports.
[0091] For the current pollution level of severe pollution, corresponding treatment measures may include: Emergency Response: Immediately activate the emergency response plan and take all necessary measures to prevent the spread of pollution. This includes setting up oil booms and interception nets around the polluted area to prevent further spread of pollutants to other waters. Simultaneously, organize professional teams to quickly collect and treat pollutants, such as using activated carbon adsorption and chemical precipitation to treat heavy metals and toxic organic compounds. Deep Treatment and Remediation: Develop a long-term deep treatment plan, which may require advanced treatment processes such as advanced oxidation technology and membrane separation technology to deeply purify the water. For contaminated soil and sediment, ex-situ treatment may be necessary, transporting them to specialized treatment sites for remediation. Ecological Reconstruction: After initial improvement in water quality, carry out ecological reconstruction work. Reintroduce suitable aquatic species to gradually restore the structure and function of the aquatic ecosystem. Simultaneously, strengthen the monitoring and assessment of the ecosystem, adjust ecological reconstruction measures in a timely manner, and ensure the stable recovery of the ecosystem.
[0092] The water quality prediction method provided in this application acquires historical water quality monitoring data for a preset historical period corresponding to the water quality of a target area. This provides a foundation for predicting future water quality data, allowing for an understanding of water quality change trends and patterns, and providing rich information for subsequent prediction models. Furthermore, setting a preset historical period ensures that sufficiently rich data is obtained to comprehensively reflect the long-term change characteristics of water quality in the target area. This avoids incomplete information due to insufficient data, making subsequent prediction results more reliable and accurate. Then, the historical water quality monitoring data is input into the preset water quality prediction model, which outputs future water quality prediction data for the target area for the preset future period. The preset water quality prediction model uses the patterns and trends in the historical water quality monitoring data to predict the water quality for the preset future period. This allows relevant departments or personnel to understand in advance the possible changes in water quality in the target area, providing forward-looking information for water resource management and protection. Accurate future water quality prediction data helps relevant departments formulate reasonable water resource management strategies and decisions. By predicting future water quality data, the utilization and allocation of water resources can be rationally arranged. For areas where water quality issues may arise, water use plans can be adjusted in advance, prioritizing residential and essential industrial water supply to avoid water waste and irrational use, thereby improving water resource utilization efficiency. Finally, determining the current pollution level based on future water quality forecasts allows for timely and accurate assessment of the current water quality status and future trends in the target area, and their potential impact on the environment and human health. This helps relevant departments to promptly grasp the water quality situation and take appropriate measures to protect water resources and the ecological environment. Corresponding treatment measures are assigned based on different pollution levels, making the remediation work more targeted and effective. The above method provides a water quality prediction method with a simple calculation process and high predictive accuracy.
[0093] This embodiment provides a water quality prediction method that can be used in the aforementioned electronic equipment. Figure 2 This is a flowchart of a water quality prediction method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps:
[0094] Step S201: Obtain historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area.
[0095] Please refer to the above description of step S101 for details on this step, which will not be repeated here.
[0096] Step S202: Input historical water quality monitoring data into the preset water quality prediction model and output the future water quality prediction data for the target area for a preset duration.
[0097] Specifically, the preset water quality prediction model includes a long-term time-dependent model, a short-term time-dynamic model, a feature fusion network, and a capsule network. Step S202 above may include the following steps:
[0098] Step S2021: Input historical water quality monitoring data into the long-term time-dependent model and output long-term time-dependent features.
[0099] Specifically, the long-term time dependency model includes a multi-layered long-term time dependency structure, and step S2021 above may include the following steps:
[0100] Step a1: For each time step in each long-term time-dependent structure, obtain the first current input data, the first current hidden state, and the first current cell state corresponding to the current moment.
[0101] The first current hidden state and the first current cell state are calculated based on the historical input data corresponding to the previous moment.
[0102] Specifically, for each time step in each long-term time-dependent structure, the first current input data, the first current hidden state, and the first current cell state corresponding to the current moment are obtained.
[0103] Step a2: Based on the first current input data, the first current hidden state, and the first current cell state, calculate the weight information corresponding to the forget gate, input gate, and output gate, respectively.
[0104] Specifically, the first current input data xt and the first current hidden state h t-1 The first current cell state ct-1 is concatenated with it to form a vector [x] t h t-1 c t-1 The input is fed into a multilayer perceptron. After undergoing several layers of linear transformation and activation functions (such as ReLU), the multilayer perceptron outputs three parameter vectors α. f αi and α o These are used to adjust the weights of the forget gate, input gate, and output gate, respectively. The specific calculation formulas are as follows:
[0105] α f =MLP f ([x t h t-1 c t-1 ])
[0106] α i =MLP i ([x t b t-1 c t-1 ])
[0107] α o =MLP o ([x t h t-1 c t-1 ])
[0108] Among them, MLP f MLP i and MLP o These are multilayer perceptrons used to calculate the adjustment parameters of the forget gate, input gate, and output gate.
[0109] Step a3: Based on the weight information corresponding to the forget gate, input gate, and output gate, perform forget gate calculation, input gate calculation, and output gate calculation.
[0110] Specifically, based on the weight information corresponding to the forget gate, input gate, and output gate, the forget gate calculation, input gate calculation, and output gate calculation are performed, and the specific formulas are as follows:
[0111] f t =σ((1+α) f )·(W f ·[h t-1 x t ]+b f ));
[0112] i t =σ((1+α) i )·(W i ·[h t-1 x t ]+b i ));
[0113] o t =σ((1+α) o )·(W o ·[h t-1 x t ]+b o ));
[0114] Among them, f t Forget Gate, α f For the weight information corresponding to the forget gate, i t For input gate, o t This is the output gate.
[0115] Among them, when abnormal fluctuations are detected, α fThis increases the weight of the forget gate, reducing the retention of historical steady-state data and allowing more past information to be forgotten, enabling the model to adapt to new anomalies more quickly. In anomalies, αi increases the weight of the input gate, enhancing the model's ability to capture current anomalies and allowing more new anomaly information to enter the cell state.
[0116] Step a4: The output of the output gate of the last long-term time-dependent structure is determined as the long-term time-dependent feature.
[0117] Specifically, the output of the output gate of the last long-term time-dependent structure is determined as the long-term time-dependent feature.
[0118] Step S2022: Input historical water quality monitoring data into the short-term time dynamic model and output short-term time dynamic characteristics.
[0119] Specifically, the short-term dynamic model includes a standard structure layer and an attention mechanism structure layer. Step S2022 above may include the following steps:
[0120] Step b1: For each time step in the standard structure layer, obtain the second current input data corresponding to the current time step and the previous hidden state corresponding to the previous time step.
[0121] The hidden state of the previous time step is calculated based on the historical input data corresponding to the previous time step.
[0122] Step b2: Based on the second current input data and the hidden state at the previous time step, calculate the current hidden state corresponding to the current time step.
[0123] Step b3: Output the hidden state corresponding to each time step in the standard structure layer to generate standard time dynamic features.
[0124] Specifically, historical water quality monitoring data is input as a time series into a standard structure layer, which can be a standard GRU cell. This data contains various water quality parameters at different time points, such as pH, dissolved oxygen content, and pollutant concentration. The input data is typically in the form of a three-dimensional tensor, with dimensions of [batch size, number of time steps, number of features].
[0125] At time step t=0, a hidden state h0 needs to be initialized for the standard structure layer. Typically, this initial hidden state can be initialized entirely as a zero vector, with the same dimension as the hidden layer dimension of the standard GRU unit.
[0126] For each time step t (starting from t=1), the standard GRU cell performs the following calculations:
[0127] Compute update gate zt The update gate determines the hidden state at the previous moment. t-1 How much information needs to be retained up to the current time? The formula is z. t =σ(W z ·[h t-1 ,x t ]), where σ is the Sigmoid function, W z It is the weight matrix of the updated gate, [h t-1 ,x t This indicates that the hidden state of the previous time step is concatenated with the second current input data of the current time step.
[0128] Calculate the reset gate r t Resetting the door determines the hidden state at the previous moment. t-1 How much information needs to be reset? The calculation formula is r. t =σ(W r ·[h t-1 ,x t ]), where W r It is the weight matrix of the reset gate.
[0129] Calculate the candidate hidden states h~t: The candidate hidden states are calculated based on the second current input data and the hidden state at the previous time step after the reset gate processing. The calculation formula is as follows: Where W is the weight matrix for calculating the candidate hidden state, and tanh is the hyperbolic tangent function.
[0130] The current hidden state ht is calculated by weighting the historical hidden state and candidate hidden states using the update gate. The calculation formula is as follows:
[0131] After processing all time steps, the standard structure layer outputs the hidden state sequence for each time step, i.e., the standard temporal dynamic features. These hidden state sequences contain the common short-term temporal dynamic features in water quality data, such as the daily periodic changes in water quality parameters.
[0132] Step b4: For each time step in the attention mechanism structure layer, obtain the second current input data corresponding to the current time step and the previous hidden state corresponding to the previous time step.
[0133] The hidden state of the previous time step is calculated based on the historical input data corresponding to the previous time step.
[0134] Step b5: Based on the second current input data and the hidden state of the previous time step, calculate the current hidden state corresponding to the current time step.
[0135] Step b6: Map the hidden state corresponding to each time step in the attention mechanism structure layer to a new dimension to generate the hidden feature corresponding to each time step.
[0136] Step b7: Process each hidden feature to generate an attention gating signal.
[0137] Step b8: Multiply each attention gating signal element-wise with the corresponding hidden state to obtain the weighted time dynamic features.
[0138] Specifically, historical water quality monitoring data is also input into the attention mechanism structure layer. This layer can be a GRU branch incorporating a gated attention mechanism. The input data format is the same as the standard structure layer.
[0139] Similar to the standard structure layer, at time step t=0, a hidden state h0 is initialized for the attention mechanism structure layer, which is usually initialized as a zero vector.
[0140] Following the standard GRU unit calculation process, the update gate zt, reset gate rt, and candidate hidden state are calculated sequentially for each time step. The hidden state ht at the current moment is calculated using the same formula as the standard GRU branch.
[0141] After the GRU unit outputs the current hidden state at each time step, these current hidden states are input into the gated attention module. The gated attention module processes them according to the following steps:
[0142] The attention mechanism structure layer maps the current hidden state of the standard GRU output to a new dimension through a linear layer, enabling the model to learn higher-level feature representations. Then, the output of the linear layer is processed using a sigmoid function to generate an attention gating signal. The attention gating signal represents the importance of the data at each time step, with values ranging from [0,1].
[0143] The attention-gated signal is multiplied element-wise with the hidden state of each time step output by the GRU to obtain a weighted feature representation, i.e., weighted temporal dynamic features. In this way, the features of important time steps are enhanced, while the features of unimportant time steps are weakened.
[0144] Step b9: The standard time dynamic features and the weighted time dynamic features are fused to generate short-term time dynamic features.
[0145] Specifically, the standard time dynamic features and the weighted time dynamic features are concatenated to generate short-term time dynamic features.
[0146] Step S2023: Based on the feature fusion network, long-term time-dependent features and short-term time-dynamic features are fused to generate target fused features.
[0147] Specifically, step S2023 above may include the following steps:
[0148] Step c1 involves inputting both long-term time-dependent features and short-term time-dynamic features into multiple feature extraction branches.
[0149] The size of the convolution kernel varies in each feature extraction branch.
[0150] Specifically, electronic devices can input both long-term temporal dependencies and short-term dynamic features into multiple feature extraction branches. Each feature extraction branch uses convolutional kernels of different sizes to extract feature information at different scales. For example, three feature extraction branches can be set up, using convolutional kernels of sizes 3, 5, and 7, respectively. When performing self-attention calculations, each feature extraction branch focuses on the dependencies between features at different scales.
[0151] For example, a small-scale branch (kernel size 3): Small-scale convolutional kernels can capture local details in water quality features. For instance, a small-scale convolutional kernel can more sensitively detect sudden changes in the concentration of certain trace substances in the water at a given moment. During self-attention computation, it primarily focuses on the close dependencies between features in adjacent time steps, uncovering short-term, local feature patterns.
[0152] Mesoscale branch (kernel size 5): The mesoscale convolution kernel can take into account both certain local features and broader feature correlations. In water quality prediction, it may capture some trends in water quality parameters over several consecutive time steps, such as the gradual increase or decrease of dissolved oxygen content over several days. This branch focuses on a relatively wider range of feature dependencies during self-attention calculations, which helps to discover some short- to medium-term water quality change patterns.
[0153] Large-scale branch (kernel size 7): Large-scale convolutional kernels focus on extracting more macroscopic and long-term water quality features. They can capture the overall trend of water quality changes over a longer period, such as seasonal fluctuations. During self-attention computation, the large-scale branch focuses on feature dependencies across a wider range of time steps, providing the model with long-term water quality trend information.
[0154] Step c2: For each feature extraction branch, perform convolution operations on long-term time-dependent features and short-term time-dynamic features, and perform self-attention calculation to output the initial branch features.
[0155] Specifically, for each feature extraction branch, the feature extraction branch uses the corresponding convolution kernel to perform convolution operations on long-term time-dependent features and short-term time-dynamic features to obtain convolution feature vectors.
[0156] After the convolution operation is completed in each feature extraction branch, a series of convolutional feature vectors are obtained. Let the convolutional feature vectors output by the convolution operation be X = [x1, x2, ..., xT], where T is the number of time steps and xt is the feature vector at the t-th time step.
[0157] To perform self-attention calculation, the convolutional feature vector X needs to be mapped to the query, key, and value spaces respectively through linear transformations, resulting in the corresponding matrices Q, K, and V. The specific calculation is as follows:
[0158] Query matrix Q: Q = XW Q W Q It is a learnable weight matrix with dimension d. in ×d k d in dk is the dimension of the input feature vector, and dk is the dimension of the query vector.
[0159] Bond matrix K: K = XW K W K It is a learnable weight matrix, also with dimension d. in ×d k .
[0160] Value matrix V: V = XW V W V It is a learnable weight matrix with dimension d. in ×d v d v It is the dimension of the value vector.
[0161] Next, the attention score is obtained by calculating the dot product of the query matrix Q and the key matrix K. The specific formula is as follows:
[0162]
[0163] Among them, K T It is the transpose of the key matrix K. This scaling operation is performed to prevent the dot product result from becoming too large. The attention score represents the similarity between each query vector and all key vectors; a higher score indicates a stronger relevance.
[0164] To transform the attention scores into a probability distribution, the attention scores are processed using the softmax function: AttentionWeights = softmax(AttentionScores).
[0165] The softmax function maps attention scores to the interval [0,1], with all elements summing to 1. This results in attention weights that represent the importance of features at each time step in calculating the final output.
[0166] Finally, the attention weights are multiplied by the value matrix V to obtain the weighted value matrix:
[0167] Output = AttentionWeights · V
[0168] This weighted matrix is the output of the self-attention mechanism, i.e., the initial branch features, which integrates the correlation information between features from different time steps. In the output of each time step, the model performs a weighted summation of the value vectors from different time steps according to the attention weights, thereby highlighting the feature information relevant to the current time step.
[0169] Step c3: Perform feature enhancement processing on each initial branch feature to generate the target branch feature.
[0170] Specifically, electronic devices can perform normalization operations on the initial branch features. For example, batch normalization or layer normalization can accelerate the model training process, improve the model's stability, and make the features have a similar distribution across different training batches and feature dimensions.
[0171] Then, activation functions (such as ReLU, LeakyReLU, etc.) are used to perform nonlinear transformations on the normalized features, introducing nonlinear factors to enhance the model's expressive power and enable the model to learn more complex feature patterns.
[0172] Finally, the features after nonlinear transformation are scaled to map the feature values to an appropriate range, generating the target branch features. This avoids the adverse effects of some features being too large or too small on model training.
[0173] Step c4: Perform fusion processing on the features of each target branch to generate initial fused features.
[0174] Specifically, electronic devices can fuse the target branch features after feature enhancement processing from each feature extraction branch to generate an initial fused feature. The aim is to integrate feature information from different scales and aspects to obtain a more comprehensive and richer feature representation. Common fusion methods include the following:
[0175] Concatenation: This method concatenates the features of different target branches along their feature dimensions. For example, if the feature dimension of branch 1 is D1 and the feature dimension of branch 2 is D2, the concatenated feature dimension will be D1 + D2. This method is simple and direct, and it preserves the original feature information of each branch.
[0176] Weighted summation: Assign a weight to each target branch feature, and then sum them according to the weights. The weights can be learned by the model or set empirically. This method allows for flexible combinations based on the importance of different branch features.
[0177] After fusion processing, the initial fusion feature is obtained, which integrates information from multiple feature extraction branches and has a richer expressive power.
[0178] Step c5: Perform feature filtering on the initial fusion features to obtain the target fusion features.
[0179] Specifically, the initial fusion feature includes multiple sub-initial fusion features, and step c5 above may include the following steps:
[0180] Step c51: Calculate proxy water quality prediction data based on historical water quality monitoring data.
[0181] Specifically, for each water quality indicator in historical water quality monitoring data, the electronic device can calculate its trend over a past period (e.g., one week, one month) based on the historical water quality monitoring data. The trend can be represented by a slope. For example, for a certain water quality indicator x, its trend s over the time interval [t1, t2] can be obtained by calculating the slope using linear regression, with the following formula:
[0182]
[0183] in, It is the average value over a time interval. It is the average value of the water quality index over the time interval.
[0184] Then, the electronic device calculates the proxy water quality prediction data based on the changing trends of each water quality indicator.
[0185] Step c52: Calculate the correlation coefficient between each sub-initial fusion feature in the initial fusion features and the proxy water quality prediction data.
[0186] Specifically, for each sub-initial fusion feature in the initial fusion feature, the Pearson correlation coefficient r between each sub-initial fusion feature and the proxy water quality prediction data is calculated using the following formula:
[0187]
[0188] Where n is the number of samples, This represents the average value of the initial fusion features of the sub-subs. This represents the average value corresponding to the proxy water quality prediction data. The closer the absolute value of the correlation coefficient is to 1, the stronger the correlation.
[0189] Step c53: Calculate the information gain of each sub-initial fusion feature on the proxy water quality prediction data.
[0190] Information gain is used to characterize the degree of information contribution of each initial fusion feature to the proxy water quality prediction data.
[0191] Specifically, for the initial fusion features, the formula for calculating the information gain IG is:
[0192] IG(s|f)=H(s)-H(s|f)
[0193] Where H(s) is the entropy of the proxy water quality prediction data s, and H(s|f) is the conditional entropy of the proxy water quality prediction data s under the initial fusion feature f. The formula for calculating entropy is:
[0194]
[0195] Where p(xi) is the probability that X takes the value xi, and k is the number of categories that X can take.
[0196] Step c54: The correlation coefficients and information gains corresponding to each initial sub-fusion feature are weighted and fused to generate the importance scores corresponding to each initial sub-fusion feature.
[0197] Specifically, the electronic device can weight and combine the calculated correlation coefficients and information gains to obtain the initial fusion feature f. i Importance Score i :
[0198] S i =α|ρ i |+(1-α)IG i
[0199] Here, α∈[0,1] is a hyperparameter used to control the relative importance of the correlation coefficient and information gain. When α=1, only linear correlation is considered; when α=0, only information gain is considered.
[0200] Step c55: Based on the importance scores corresponding to each initial sub-fusion feature, select the sub-target fusion features whose importance scores are greater than the preset score threshold, and generate the target fusion features.
[0201] Specifically, the electronic device can compare the importance score corresponding to each initial sub-fusion feature with a preset score threshold, and then select each sub-target fusion feature whose importance score is greater than the preset score threshold to generate the target fusion feature.
[0202] Step S2024: Input the target fusion features into the capsule network, extract features from the target fusion features, and output the target feature vector.
[0203] Specifically, step S2024 above may include the following steps:
[0204] Step d1: Perform deformable convolution operation on the target fusion features based on the deformable convolution kernel in the capsule network to generate multiple feature maps.
[0205] Deformable convolution can adaptively adjust the sampling position of the convolution kernel, thereby better capturing the geometric deformation of features. In historical water quality monitoring data, water quality characteristics may exhibit certain nonlinear changes at different time steps, and deformable convolution can more flexibly adapt to these changes.
[0206] Specifically, the deformable convolutional kernel in the capsule network has a size of k and a stride of s. The deformable convolution learns the offset of each sampling point, building upon the traditional convolution. In this way, the convolutional kernel adaptively adjusts the sampling position according to different input features, thereby extracting features more accurately.
[0207] Deformable convolution operations are performed on the target fusion features based on deformable convolution kernels to generate multiple feature maps.
[0208] Step d2: Construct a primary capsule based on each feature map.
[0209] Specifically, based on the location neurons on each feature map, capsule vectors are generated to form primary capsules. This process transforms the feature maps obtained from convolution into a more structured representation. Each initial capsule represents a specific water quality feature, which can more effectively capture and represent the complex relationships between features, laying the foundation for subsequent dynamic routing and feature representation enhancement. For example, if N feature maps are obtained after convolution, and each feature map is of size M, then N×M primary capsules will be obtained.
[0210] Step d3: Based on a preset nonlinear function, perform a nonlinear transformation on each primary capsule to generate a nonlinear vector.
[0211] Specifically, electronic devices can use the squash function to perform nonlinear transformations, and the squash function can be dynamically adjusted according to different input characteristics.
[0212] The squash function is:
[0213]
[0214] Where, α j is the adaptive parameter of the j-th primary capsule, which can be obtained by a small neural network based on the input vector s of the primary capsule. j The squash function learns from this. In this way, it can dynamically adjust its compression level based on different input features, better representing the specific water quality characteristics represented by the capsule.
[0215] Step d4: Based on the dynamic routing mechanism, each nonlinear vector is converted into the target input vector.
[0216] Specifically, step d4 above may include the following steps:
[0217] Step d41: Initialize the initial coupling coefficient based on the prior relationships between different water quality characteristics in historical water quality monitoring data.
[0218] Specifically, the electronic device can initialize the initial coupling coefficient based on the prior relationships between different water quality characteristics in historical water quality monitoring data.
[0219] For example, for some water quality characteristics that are clearly correlated (such as dissolved oxygen and biochemical oxygen demand), the corresponding initial coupling coefficient b can be used. ij Initializing the initial coupling coefficient to a large positive value indicates a potentially strong correlation between the two features. For features with weaker correlations, the corresponding initial coupling coefficient b can be adjusted. ij Initialize it to a small value. This allows the dynamic routing mechanism to have a certain bias in the initial stage, speeding up the convergence process.
[0220] Step d42: Based on the attention enhancement mechanism, attention weights are applied to each nonlinear vector to generate a weighted vector.
[0221] Specifically, the electronic device can calculate the attention weight vector ai corresponding to the nonlinear vector vi based on the weighted neural network in the capsule network. The dimension of the weight vector ai is the same as that of the nonlinear vector vi. Each element in the attention weight vector ai represents the importance of the corresponding element in the nonlinear vector vi.
[0222] The weighted neural network can be a simple fully connected layer that maps a nonlinear vector vi to a vector space with the same dimension as vi, and then converts the output into a probability distribution through an activation function (such as the softmax function) to obtain the attention weight vector ai.
[0223] Then, the attention weight vector ai is multiplied element-wise with the nonlinear vector vi to obtain the weighted input vector. This is a weighted vector. In this way, important feature elements in the input vector are assigned larger weights, while unimportant feature elements are assigned smaller weights.
[0224] Step d43: Calculate the prediction vector corresponding to each weighted vector.
[0225] Specifically, based on each weighted vector, the prediction vector corresponding to the weighted vector is calculated according to the following formula:
[0226]
[0227] Among them, W ij This is the weight matrix. For the weighted vector, u j|i Let be the prediction vector, where the weight matrix is obtained by training the capsule network.
[0228] Step d44: Based on the prediction vector, update the initial coupling coefficient to obtain the updated coupling coefficient.
[0229] Specifically, electronic devices can introduce a forgetting factor and, based on the prediction vector, update the initial coupling coefficient according to the following formula to obtain the updated coupling coefficient:
[0230]
[0231] Here, β is the forgetting factor, ranging from (0,1). The forgetting factor β controls the degree to which early information is retained. When β is small, it means the model gives less weight to the early coupling coefficients bij, focusing more on new information in the current iteration; when β is large, it means the model gives more weight to the early coupling coefficients bij, tending to retain earlier information. By introducing the forgetting factor, the dynamic routing mechanism can be made more flexible and stable. The model can better balance early and new information during iteration, avoiding the accumulation of early erroneous information, thereby improving the model's convergence speed and performance.
[0232] Step d45: Calculate the attention weights based on the updated coupling coefficients.
[0233] Specifically, after calculating the updated coupling coefficient, the electronic device can use the updated coupling coefficient b as a basis. ij Calculate attention weight c ij Attention weight c ij The formula used to measure the strength of information transmission from primary capsule i to advanced capsule j is as follows:
[0234]
[0235] Here, k iterates through all the primary capsules. Using the softmax function, c... ij Normalized to the interval [0,1], and all c pointing to the same advanced capsule j ij The sum is 1.
[0236] Step d46: Multiply the prediction vector by the attention weights to obtain the target input vector.
[0237] Specifically, electronic devices can utilize the calculated attention weights c ij Combined with the prediction vector u j∣i Calculate the target input vector sj of the high-level capsule j. The calculation formula is:
[0238] s j =∑ i c ij u j|i
[0239] This formula indicates that the target input vector of advanced capsule j is the prediction vector of all primary capsules u. j∣i The weighted sum, where the weight is the attention weight c. ij Thus, the contribution of each primary capsule to the advanced capsule is determined by the degree of its association with the advanced capsule (by c). ij The indication has been adjusted.
[0240] Step d5: Pass the target input vector to the higher-level capsules in the capsule network.
[0241] Specifically, the electronic device passes the target input vector to the higher-level capsules in the capsule network.
[0242] Step d6: The advanced capsule performs a nonlinear transformation on the target input vector and outputs the target feature vector.
[0243] Specifically, the target input vector s of the advanced capsule is obtained. j Afterwards, a nonlinear transformation is needed, typically using the squash function. The squash function compresses the length of a vector to the interval [0,1] while preserving its direction information. The formula for the squash function is:
[0244]
[0245] Among them, v j It is the target feature vector output after the higher capsule j is transformed by the squash function. It represents the more abstract and higher-level feature information represented by the higher capsule.
[0246] Dynamic routing mechanisms typically require multiple iterations to optimize the coupling coefficient b. ijThe iteration converges to a stable value. After each iteration, it is necessary to determine whether the iteration stopping condition is met. Common stopping conditions include reaching a preset maximum number of iterations, or the change in the coupling coefficient bij being less than a certain threshold.
[0247] If the stopping condition is not met, the process of updating the coupling coefficients, calculating the attention weights, calculating the high-level capsule input vector, and performing nonlinear transformations is repeated until the stopping condition is met.
[0248] If the stopping condition is met, the dynamic routing process ends, and the final high-level capsule output vector vj is obtained.
[0249] Step S2025: Based on the target feature vector, output the future water quality prediction data corresponding to the water quality of the target area.
[0250] Specifically, after obtaining the target feature vector vj from the final high-level capsule output, it can be used for subsequent tasks, such as classification or regression:
[0251] Classification tasks: For water quality classification tasks (such as determining whether the water is clean, lightly polluted, or heavily polluted), the magnitude of the output vector from the higher-level capsule can be used as a reference. j || Make a classification decision. Typically, the category represented by the highest-order capsule with the largest modulus is chosen as the final prediction.
[0252] Regression Task: For regression prediction of water quality parameters (such as dissolved oxygen content, pH value, etc.), the target feature vector output by the advanced capsule can be input into a fully connected layer, and the future water quality prediction data can be obtained through linear transformation. For example, let the weight matrix of the fully connected layer be W, and the bias be b, then the future water quality prediction data... It can be represented as Where v is a vector formed by concatenating the target feature vectors output by all advanced capsules.
[0253] Step S203: Based on future water quality prediction data, determine the current pollution level of the target area's water quality and output corresponding treatment measures.
[0254] Please refer to the above description of step S103 for details on this step, which will not be repeated here.
[0255] The water prediction method provided in this application acquires the first current input data, the first current hidden state, and the first current cell state at each time step in a long-term time-dependent structure, thereby accurately capturing information changes at each moment and providing a foundation for subsequent analysis. Based on the first current input data, the first current hidden state, and the first current cell state, the weight information corresponding to the forget gate, input gate, and output gate is calculated. This allows for adaptive determination of which past information should be forgotten, which should be retained, and how to update the cell state at each time step, thus better handling long-term dependencies and effectively modeling long-term trends in water quality data. Then, based on the weight information corresponding to the forget gate, input gate, and output gate, forget gate calculation, input gate calculation, and output gate calculation are performed. These gate calculations can flexibly control the flow of information, enabling the model to dynamically adjust its focus on information at different time steps according to the characteristics of the data, helping to more accurately capture long-term dependency features in water quality data. The output result of the output gate of the last layer of the long-term time-dependent structure is determined as the long-term time-dependent feature. This allows for the extraction of representative long-term features from complex time-series data, providing crucial information for subsequent analysis and prediction, and helping the model understand the patterns and trends of water quality changes over longer timescales.
[0256] For each time step in the standard structure layer, the second current input data corresponding to the current moment and the hidden state corresponding to the previous moment are obtained. The current hidden state corresponding to the current moment is calculated based on the second current input data and the hidden state of the previous moment. This allows the model to capture the dynamic changes of data on short-term time scales, reflecting the real-time changes in water quality data by continuously updating the hidden state. The hidden state corresponding to each time step in the standard structure layer is output, generating standard time dynamic features. This helps the model understand the changing patterns of water quality data from a short-term perspective, providing short-term dynamic information for subsequent fusion with long-term features, enabling the model to comprehensively consider features at different time scales. For each time step in the attention mechanism structure layer, the second current input data corresponding to the current moment and the hidden state corresponding to the previous moment are also obtained. The current hidden state corresponding to the current moment is calculated based on this, and then the hidden state corresponding to each time step is mapped to a new dimension, generating hidden features corresponding to each time step. This mapping operation transforms the hidden state into a space more suitable for expressing data features, helping the model better capture the intrinsic features of the data. Each hidden feature is processed to generate an attention gating signal. Attention mechanisms enable models to automatically focus on more important parts of the data. By generating attention-gated signals, hidden states can be weighted according to the importance of different time steps, highlighting key information and suppressing irrelevant information. Each attention-gated signal is element-wise multiplied with its corresponding hidden state to obtain weighted temporal dynamic features. These standard temporal dynamic features and weighted temporal dynamic features are then fused to generate short-term temporal dynamic features. This fusion method integrates the general short-term dynamic information captured by the standard structural layer with the key short-term information highlighted by the attention mechanism structural layer, making the short-term temporal dynamic features more comprehensive and accurate, and better reflecting the complex changes in water quality data over short periods.
[0257] Next, both long-term time-dependent features and short-term time-dynamic features are input into multiple feature extraction branches. The convolution kernel sizes in each branch differ. For each branch, convolution operations are performed on the long-term time-dependent features and short-term time-dynamic features, followed by self-attention calculation to output initial branch features. Different sized convolution kernels can capture feature information at different scales, while self-attention calculation can further uncover the intrinsic relationships between features, enabling the model to extract features more comprehensively and deeply, thus improving its ability to represent water quality data features. Feature enhancement processing is applied to each initial branch feature to generate target branch features. Then, the target branch features are fused to generate initial fused features. Feature enhancement further highlights useful features, suppresses noise, and improves feature quality and robustness, while fusion processing integrates features extracted from different branches, fully utilizing multi-scale and multi-dimensional feature information to provide richer and more comprehensive feature representations for subsequent analysis. Based on historical water quality monitoring data, proxy water quality prediction data is calculated. This provides a benchmark for evaluating the relationship between features and prediction results, helping to determine which features are more important for water quality prediction. The correlation coefficients between each sub-initial fusion feature and the surrogate water quality prediction data, as well as the information gain of each sub-initial fusion feature on the surrogate water quality prediction data, are calculated. The correlation coefficient measures the strength of the linear relationship between the feature and the prediction result, while the information gain characterizes the information contribution of each sub-initial fusion feature to the surrogate water quality prediction data. These two indicators comprehensively assess the importance of each sub-feature to water quality prediction. The correlation coefficients and information gains corresponding to each sub-initial fusion feature are weighted and fused to generate an importance score for each sub-initial fusion feature. Based on the importance scores of each sub-initial fusion feature, sub-target fusion features with importance scores greater than a preset threshold are selected to generate target fusion features. This feature selection method based on importance scores can remove features that contribute little to water quality prediction and retain the most valuable features, thereby improving the efficiency and accuracy of the model and enabling the model to focus more on key features for water quality prediction.
[0258] The target fusion features are subjected to deformable convolution operations using deformable convolution kernels in a capsule network, generating multiple feature maps. Primary capsules are then constructed based on these feature maps. Deformable convolution adaptively adjusts the shape and position of the convolution kernel, better adapting to local changes in the data and thus extracting features more accurately. Constructing primary capsules transforms the feature maps into capsule form, providing a more structured representation for subsequent processing. Based on a preset nonlinear function, each primary capsule undergoes a nonlinear transformation to generate a nonlinear vector. This nonlinear transformation increases the model's expressive power, enabling it to better fit complex water quality data distributions. Initial coupling coefficients are initialized based on prior relationships between different water quality features in historical water quality monitoring data. Attention-enhanced weights are applied to each nonlinear vector using an attention enhancement mechanism, generating weighted vectors. Predicted vectors are then calculated based on these weighted vectors, and the initial coupling coefficients are updated using these updated vectors. In this way, the model can automatically adjust the coupling relationships between different features according to the characteristics of the data. Attention weighting further highlights important features, allowing the model to more accurately capture the interactions between water quality features. Based on the updated coupling coefficients, attention weights are calculated, and the target input vector is obtained by multiplying the predicted vector by the attention weights. The calculation of attention weights weights the predicted vector according to the importance of features, making the target input vector more representative and better reflecting the characteristics of water quality data, thus providing high-quality input for subsequent advanced capsule processing. The target input vector is then passed to the advanced capsules in the capsule network. These capsules perform a nonlinear transformation on the target input vector, outputting a target feature vector. The nonlinear transformation of the advanced capsules further abstracts and refines the features, generating more discriminative target feature vectors, providing strong feature support for the final water quality prediction, and helping to improve the accuracy and reliability of water quality prediction.
[0259] In one optional implementation, the preset water quality prediction model is trained based on a target loss function, which includes a prediction sub-loss function, a time series sub-loss function, a trend sub-loss function, an uncertainty sub-loss function, and a physical constraint sub-loss function.
[0260] Specifically, the target loss function is as follows:
[0261] L_total=α·L_prediction+β·L_temporal+γ·L_trend+δ·L_uncertainty+ε·L_physical;
[0262] L_prediction={0.5(y_t-P_t) 2 ,|y_t-P_t|≤δ|y_t-P_t|-0.5δ2, others};
[0263]
[0264] L_uncertainty=-logp(y_t|P_t,σ_t 2 );
[0265] L_physical=∑λ_i·max(0,g_i(P_t));
[0266] Where L_total is the target loss function, L_prediction is the prediction sub-loss function, L_temporal is the temporal sub-loss function, L_trend is the trend sub-loss function, L_uncertainty is the uncertainty sub-loss function, and L_physical is the physical constraint sub-loss function; α, β, γ, δ, and ε are all weight coefficients; y_t is the true value of the sample during training, P_t is the predicted value of the sample output during training, and δ is the threshold parameter of the prediction sub-loss function; σ_t represents the time gradient operator; 2 To predict the variance; g_i(P_t) represents the physicochemical constraints.
[0267] Specifically, such as Figure 3 As shown, the training process of the preset water quality prediction model may include the following steps:
[0268] S1. Data Acquisition and Preprocessing: Collect water quality monitoring data such as pH, dissolved oxygen, temperature, and turbidity. Clean the raw data, remove outliers and missing values, and standardize the data to construct a time series dataset. This unifies indicators of different dimensions to the same scale, preparing for subsequent analysis.
[0269] S2. Dataset Partitioning and Validation: The dataset is divided into training, validation, and test sets in a 7:2:1 ratio. First, the data is sorted chronologically to maintain temporal continuity. Statistical characteristics (mean, variance, skewness, kurtosis) of each subset are calculated, and the consistency of sample distribution is verified using the KS test to ensure similar data distribution across subsets, providing reliable data support for model training and evaluation.
[0270] S3. Initialize the Bayesian optimization framework: Define the optimization space for hyperparameters such as the number of layers and neurons in the long-term time-dependent model, short-term time-dynamic model, feature fusion network, and capsule network. Use the validation set prediction error (composed of MSE, MAE, and RMSE combined with certain weights) as the objective function. Initialize the Gaussian process regression model (using the Matérn5 / 2 kernel function) and the expected improvement (EI) acquisition function. Set the maximum number of iterations and the initial number of sampling points, and configure an early stopping strategy (stop optimization when there is no improvement after K consecutive iterations). Provide a framework and rules for optimizing hyperparameters.
[0271] S4. Construct a hybrid model architecture with multiple layers, including a long-term time dependency model, a short-term time dynamic model, a feature fusion network, and a capsule network: The long-term time dependency model uses a multi-layered long-term time dependency structure to extract long-term time dependency features, involving forgetting gates, input gates, cell state gates, and output gates. The short-term time dynamic model uses standard structural layers and attention mechanism structural layers to capture short-term dynamic changes, calculating hidden states through update and reset gates. The feature fusion network fuses long-term time dependency features and short-term time dynamic features to generate target fused features. These target fused features are then input into the capsule network for feature extraction, outputting a target feature vector. A dropout layer is added to prevent overfitting, and an activation function is used for non-linear transformation, enabling the model to comprehensively process feature information at different time scales.
[0272] S5. Construct a multi-objective loss function: The loss function L_total is composed of a prediction sub-loss function (using the Huber loss function to enhance robustness to outliers), a time series sub-loss function (measuring time series correlation through the time gradient operator), a trend sub-loss function (ensuring that the predicted trend is consistent with the physical change law), an uncertainty sub-loss function (introducing a Bayesian framework to estimate prediction uncertainty), and a physical constraint sub-loss function (ensuring that the prediction results meet physical and chemical constraints) combined with certain weights, comprehensively considering factors such as prediction error, time series relationship, trend consistency, uncertainty, and physical constraints.
[0273] S6. Implement Bayesian optimization process: Evaluate hyperparameter performance using a Gaussian process regression model and dynamically adjust the weight coefficients (α, β, γ, δ, ε) of the loss function. Use the acquisition function EI to select the parameter combination that maximizes EI, continuously update the Gaussian process model, and repeat the optimization process until convergence to find the optimal hyperparameter combination and improve model performance.
[0274] S7. Perform model training: Forward propagation (calculating the outputs of the long-term time-dependent model, short-term time-dynamic model, feature fusion network, and capsule network) and backpropagation (calculating the gradient of the objective loss function and updating the model parameters, with the update rule being...) are performed based on the composite multi-objective loss function. Where η is the learning rate and θ is the model parameter), the model parameters are gradually optimized through continuous iterative training until the model converges.
[0275] S8. Model Integration and Feature Fusion: Long-term time-dependent features are extracted based on the long-term time-dependent model, and short-term time-dynamic features are extracted based on the short-term time-dynamic model. A feature fusion network is used to fuse the long-term time-dependent features and the short-term time-dynamic features to generate target fused features. These target fused features are then input into a capsule network for feature extraction, outputting a target feature vector. Based on the target feature vector, future water quality prediction data for the target area is output. The contribution of important time steps is highlighted, enabling the model to comprehensively utilize different types of feature information.
[0276] S9. Uncertainty Quantification and Physical Constraints: Using a Bayesian framework to model the distribution of prediction results (p(y_t|x_t)=N(μ_t,σ_t)). 2 )) Calculate the confidence interval (CI_t=[μ_t-k·σ_t,μ_t+k·σ_t]), and define physical constraints (such as pH∈[0,14], DO≥0, (etc.) to ensure that the prediction results have both uncertainty estimates and conform to the physicochemical laws of water quality parameters.
[0277] S10. Prediction Result Generation and Evaluation: Predict the new input data and output the predicted point values. Trend Analysis and abnormal warning The comprehensive results are presented. Model performance is evaluated using multi-dimensional metrics, including prediction accuracy assessment (RMSE, MAE, R...). 2 The model employs three assessment methods: uncertainty assessment (PICP) and physical plausibility assessment (Physics_Score), to comprehensively measure the model's predictive power, uncertainty, and physical plausibility.
[0278] The water quality prediction method provided in this application includes a prediction sub-loss function, a time-series sub-loss function, a trend sub-loss function, an uncertainty sub-loss function, and a physical constraint sub-loss function as its target loss function. The prediction sub-loss function adopts a smooth piecewise function form. For cases where the error between the predicted and actual values is small, squared loss is used to more accurately measure small errors, allowing the model to be trained better for accurate predictions. For cases where the error is large, linear loss is used to avoid the excessive penalty of squared loss for large errors, making the model more robust to outliers and thus improving the overall accuracy of prediction. The time-series sub-loss function, by calculating the sum of the absolute values of the time gradient differences between the actual and predicted values, constrains the model to learn the correct dynamic pattern of water quality changes over time, ensuring that the predicted results are consistent with the changing trend of the actual data in the time series, thus improving the time-series accuracy of the prediction. The trend sub-loss function focuses on the product of the time gradients of the actual and predicted values. When the product of the gradients is negative, i.e., when the predicted trend is opposite to the actual trend, a loss is generated, which prompts the model to learn the correct water quality change trend, further improving the accuracy and reliability of the prediction. The uncertainty sub-loss function measures the uncertainty of the prediction using negative log-likelihood, enabling the model to learn about the uncertainty of the prediction results. It not only provides the predicted value but also reflects the reliability of the prediction, offering more comprehensive information for subsequent decision-making. The physical constraint sub-loss function incorporates physicochemical constraints into the model training, ensuring that the prediction results conform to the basic physicochemical principles of water quality changes, making the predictions physically reasonable and improving the model's practicality and reliability. Finally, by setting different weight coefficients α, β, γ, δ, and ε, the influence of each sub-loss function on model training can be flexibly adjusted according to specific application scenarios and needs, allowing the model to better adapt to different water quality prediction tasks and data characteristics.
[0279] This embodiment also provides a water quality prediction device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0280] This embodiment provides a water quality prediction device, such as... Figure 4 As shown, it includes:
[0281] The acquisition module 301 is used to acquire historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area;
[0282] Output module 302 is used to input the historical water quality monitoring data into a preset water quality prediction model and output the future water quality prediction data of the target area for a preset time period.
[0283] The determination module 303 is used to determine the current pollution level of the target area's water quality based on the future water quality prediction data, and output corresponding treatment measures.
[0284] Further functional descriptions of the above modules and units are the same as those in the corresponding embodiments described above, and will not be repeated here.
[0285] In this embodiment, the water quality prediction device is presented in the form of a functional unit. Here, a unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0286] This invention also provides a computer device having the above-described features. Figure 4 The water quality prediction device shown.
[0287] Please see Figure 5 , Figure 5 This is a schematic diagram of the structure of a computer device provided in an optional embodiment of the present invention.
[0288] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and all such modifications and variations fall within the scope defined by the appended claims.
Claims
1. A water quality prediction method, characterized in that, The method includes: Obtain historical water quality monitoring data for a preset historical period corresponding to the water quality of the target area; The historical water quality monitoring data is input into a preset water quality prediction model, and the future water quality prediction data for the target area corresponding to the future preset time period is output. Based on the predicted future water quality data, the current pollution level of the target area is determined, and corresponding treatment measures are output. The preset water quality prediction model includes a long-term time-dependent model, a short-term time-dynamic model, a feature fusion network, and a capsule network. The step of inputting the historical water quality monitoring data into the preset water quality prediction model and outputting future water quality prediction data for a preset time period corresponding to the target area includes: The historical water quality monitoring data is input into the long-term time-dependent model, and the long-term time-dependent features are output. The historical water quality monitoring data is input into the short-term time dynamic model, and the short-term time dynamic characteristics are output. The long-term time-dependent features and the short-term time-dynamic features are fused based on the feature fusion network to generate target fused features; The target fusion features are input into the capsule network, and feature extraction is performed on the target fusion features to output the target feature vector; Based on the target feature vector, output the future water quality prediction data corresponding to the water quality of the target area; The step of fusing the long-term time-dependent features and the short-term time-dynamic features based on the feature fusion network to generate the target fused features includes: Both the long-term time-dependent features and the short-term time-dynamic features are input into multiple feature extraction branches; the size of the convolutional kernels in each feature extraction branch is different. For each of the feature extraction branches, convolution operations are performed on the long-term time-dependent features and the short-term time-dynamic features, and self-attention calculation is performed to output the initial branch features; The initial branch features are subjected to feature enhancement processing to generate target branch features; The features of each target branch are fused to generate initial fused features; The initial fusion features are subjected to feature filtering to obtain the target fusion features; The step of inputting the target fusion features into the capsule network, extracting features from the target fusion features, and outputting a target feature vector includes: Based on the deformable convolution kernels in the capsule network, deformable convolution operations are performed on the target fusion features to generate multiple feature maps. Based on the aforementioned feature maps, a primary capsule is constructed; Based on a preset nonlinear function, each of the primary capsules is subjected to a nonlinear transformation to generate a nonlinear vector; Based on the dynamic routing mechanism, each of the aforementioned nonlinear vectors is converted into a target input vector; The target input vector is passed to the higher-level capsules in the capsule network; The advanced capsule performs a nonlinear transformation on the target input vector and outputs the target feature vector.
2. The method according to claim 1, characterized in that, The initial fusion feature includes multiple sub-initial fusion features. The step of feature filtering the initial fusion feature to obtain the target fusion feature includes: Based on the historical water quality monitoring data, calculate the proxy water quality prediction data; Calculate the correlation coefficient between each of the sub-initial fusion features in the initial fusion features and the proxy water quality prediction data; Calculate the information gain of each of the aforementioned sub-initial fusion features on the proxy water quality prediction data; the information gain is used to characterize the degree of information contribution of each of the aforementioned sub-initial fusion features to the proxy water quality prediction data. The correlation coefficients corresponding to each of the sub-initial fusion features are weighted and fused with the information gain to generate an importance score corresponding to each of the sub-initial fusion features; Based on the importance scores corresponding to each of the initial sub-fusion features, select the sub-target fusion features whose importance scores are greater than a preset score threshold, and generate the target fusion feature.
3. The method according to claim 1, characterized in that, The process of converting each nonlinear vector into a target input vector based on a dynamic routing mechanism includes: Initialize the initial coupling coefficients based on the prior relationships between different water quality characteristics in the historical water quality monitoring data; Based on the attention enhancement mechanism, attention weighting is applied to each of the aforementioned nonlinear vectors to generate a weighted vector; Based on each of the weighted vectors, calculate the prediction vector corresponding to the weighted vector; Based on the prediction vector, the initial coupling coefficient is updated to obtain the updated coupling coefficient; Based on the updated coupling coefficient, the attention weights are calculated; The target input vector is obtained by multiplying the prediction vector by the attention weight.
4. The method according to claim 1, characterized in that, The long-term time dependency model includes a multi-layered long-term time dependency structure. The step of inputting the historical water quality monitoring data into the long-term time dependency model and outputting long-term time dependency features includes: For each time step in each of the long-term time-dependent structures, the first current input data, the first current hidden state, and the first current cell state corresponding to the current time are obtained; the first current hidden state and the first current cell state are calculated based on the historical input data corresponding to the previous time step. Based on the first current input data, the first current hidden state, and the first current cell state, calculate the weight information corresponding to the forget gate, input gate, and output gate respectively; Based on the weight information corresponding to the forget gate, the input gate, and the output gate, the forget gate calculation, input gate calculation, and output gate calculation are performed respectively. The output result of the output gate of the last layer of the long-term time-dependent structure is determined as the long-term time-dependent feature.
5. The method according to claim 1, characterized in that, The short-term time dynamic model includes a standard structure layer and an attention mechanism structure layer; The step of inputting the historical water quality monitoring data into the short-term dynamic model and outputting short-term dynamic features includes: For each time step in the standard structure layer, the second current input data corresponding to the current time and the previous hidden state corresponding to the previous time are obtained; the previous hidden state is calculated based on the historical input data corresponding to the previous time. Based on the second current input data and the previous hidden state, calculate the current hidden state corresponding to the current time. Output the hidden state corresponding to each time step in the standard structure layer to generate standard time dynamic features; For each time step in the attention mechanism structure layer, the second current input data corresponding to the current time and the previous hidden state corresponding to the previous time are obtained; the previous hidden state is calculated based on the historical input data corresponding to the previous time. Based on the second current input data and the previous hidden state, calculate the current hidden state corresponding to the current time. The hidden state corresponding to each time step in the attention mechanism structure layer is mapped to a new dimension to generate the hidden feature corresponding to each time step; The hidden features are processed to generate attention gating signals; Each attention-gated signal is multiplied element-wise with the corresponding hidden state to obtain the weighted time dynamic features. The standard time dynamic features and the weighted time dynamic features are fused to generate the short-term time dynamic features.
6. The method according to any one of claims 1-5, characterized in that, The preset water quality prediction model is trained based on a target loss function, which includes a prediction sub-loss function, a time series sub-loss function, a trend sub-loss function, an uncertainty sub-loss function, and a physical constraint sub-loss function.
7. The method according to claim 6, characterized in that, The target loss function is as follows: L total =a•L prediction +β•L temporal +γ•L trend +d ’ •L uncertainty +e•L physical ; in, L prediction ={0.5(y t -P t )²,|y t -P t |≤δ|y t -P t |-0.5δ², others}; L temporal = ∑|▽ t y t -▽ t P t |; L trend = ∑max(0,-(▽ t y t )(▽ t P t )); L uncertainty =-logp(y t |P t ,σ t 2); L physical =∑λ i ·max(0,g i (P t )); Among them, L total Let L be the target loss function. prediction For the prediction sub-loss function, L temporal For the time series sub-loss function, L trend For the trend sub-loss function, L uncertainty For the uncertainty sub-loss function, L physical The physical constraint sub-loss function; α, β, γ, δ ’ ε and y are both weighting coefficients; t P represents the true values of the samples during training. t δ represents the predicted sample value output during training; δ is the threshold parameter of the prediction sub-loss function; ▽ t σ represents the time gradient operator; t ² represents the prediction variance; g i (P t ) represents physical and chemical constraints.
Citation Information
Patent Citations
Traffic speed prediction method based on time sequence diagram neural network
CN113159414A
Urban water network area river and lake water quality improvement and water safety guarantee method and system
CN117236199A