A loss function calculation method and device for a sample unbalanced data set
By employing interval loss calculation and a loss function method that dynamically adjusts sample weights, the problem of sample imbalance in extreme precipitation prediction is solved, improving the model's generalization ability and prediction accuracy, and achieving efficient prediction of extreme precipitation events.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHENGDU UNIV OF INFORMATION TECH
- Filing Date
- 2026-02-11
- Publication Date
- 2026-04-24
AI Technical Summary
Existing loss functions cannot effectively handle the problem of imbalanced samples in extreme precipitation prediction, resulting in insufficient model generalization ability and prediction accuracy.
By constructing a loss function calculation method for imbalanced datasets, this method employs interval loss calculation and dynamic adjustment of sample weights. It combines statistical interval samples and interval sample loss gradients to automatically adjust various parameters, optimize the model, fix the dominant gradient during backpropagation, and avoid overfitting.
It improves the model's generalization ability and prediction accuracy in extreme precipitation prediction, reduces the workload of parameter adjustment, prevents the risk of insufficient attention to a few samples and overfitting, and enhances the prediction accuracy of extreme precipitation events.
Smart Images

Figure CN121682506B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of loss function calculation, and particularly relates to a method and apparatus for calculating loss function for imbalanced datasets. Background Technology
[0002] Accurate prediction of extreme precipitation has a crucial impact on various fields, especially agriculture, transportation and public safety. Predicting extreme precipitation is a very challenging task, mainly due to the following factors: (1) Precipitation patterns are affected by many factors such as season, latitude and longitude, ocean currents, and monsoons. Moreover, with the passage of time and human intervention, the climate around the world has changed to varying degrees compared to decades ago, making precipitation patterns complex and difficult to grasp; (2) The rarity of extreme precipitation events makes it difficult for neural networks to learn their patterns from sufficient samples.
[0003] Extreme precipitation prediction is essentially a binary classification task, specifically a classification task with an imbalanced number of classes in the training set, which is highly similar to the task of distinguishing foreground and background in image segmentation. Traditional loss functions aim to continuously reduce the total loss; however, reducing the loss value does not necessarily lead to improved evaluation metrics such as accuracy. For imbalanced datasets, neural network models using traditional loss functions are prone to overfitting the simpler samples on the training set, resulting in poor generalization ability and poor performance on the test set. Traditional loss functions often use pre-set weighting strategies for different classes, failing to dynamically adjust them based on the current training situation. Furthermore, traditional loss functions only focus on calculating the loss value, neglecting the loss gradient resulting from weight updates during backpropagation in the neural network, thus affecting the overall effect across all samples. Summary of the Invention
[0004] To address the aforementioned shortcomings in the existing technology, this invention provides a method and apparatus for calculating the loss function of imbalanced datasets, which solves the problems of insufficient generalization ability and prediction accuracy of neural network models on imbalanced datasets for extreme precipitation prediction.
[0005] To achieve the above-mentioned objectives, the technical solution adopted by this invention is as follows:
[0006] On the one hand, the present invention provides a method for calculating the loss function for imbalanced datasets, comprising the following steps:
[0007] S1. Process the acquired ERA5 precipitation data file and construct a weather dataset. In the weather dataset, the fewer extreme precipitation samples are used as positive samples, and the more numerous non-extreme precipitation samples are used as negative samples.
[0008] S2. Input the samples in the weather dataset into the extreme precipitation prediction model, and convert the output of the extreme precipitation prediction model into a probability value through the sigmoid function, which is used as the probability of the extreme precipitation event corresponding to the sample.
[0009] S3. Define the probability that a sample is correctly predicted;
[0010] S4. Based on the probability that the sample is correctly predicted, calculate the interval loss and construct the loss function for extreme precipitation prediction.
[0011] Further, step S1 includes the following steps:
[0012] S11. Obtain ERA5 precipitation data files according to the preset time span, time resolution, spatial coverage and spatial resolution;
[0013] S12. Divide the acquired ERA5 precipitation data file into... The three-dimensional mesh data, in which, The size of the spatial grid is used for dividing the space; 24 refers to a continuous 24 hours.
[0014] S13. Calculate the mean and standard deviation of precipitation for each divided 3D grid data within a preset time span. Take precipitation events exceeding the precipitation threshold in the 3D grid data as extreme precipitation samples and the remaining events as non-extreme precipitation samples to form a weather dataset. The precipitation threshold is equal to the sum of the mean precipitation and N times the standard deviation of precipitation. The fewer extreme precipitation samples are taken as positive samples and the more numerous non-extreme precipitation samples are taken as negative samples.
[0015] Furthermore, the expression for calculating the probability that a sample is correctly predicted in S3 is as follows:
[0016] , ,
[0017] in, This represents the probability that the sample is correctly predicted. The sample is identified as The probability that a sample is correctly predicted. This represents the probability interval of a sample being correctly predicted, where, when This indicates that the sample is a positive sample. This indicates that the sample is a negative sample.
[0018] Further, step S4 includes the following steps:
[0019] S41. Set decision boundary values and divide the interval of the correct prediction of the sample into four intervals based on the decision boundary values. The four intervals include the 0th interval, the 1st interval, the 2nd interval and the 3rd interval, respectively. The 1st interval and the 2nd interval are the intervals adjacent to the decision boundary values.
[0020] S42. Set the proportions of the first interval and the second interval in the total gradient as the first interval gradient proportion and the second interval gradient proportion, respectively, and calculate the interval weight coefficients of the first interval and the second interval.
[0021] S43. Calculate the average gradient of positive and negative samples, and then calculate the weight coefficients of positive and negative samples respectively.
[0022] S44. Based on the interval weight coefficients of the first and second intervals, and the weight coefficients of the positive and negative samples, construct the loss function for extreme precipitation prediction.
[0023] Furthermore, the calculation expressions for the interval weight coefficients of the first interval and the second interval in S42 are as follows:
[0024] ,
[0025] in, This represents the sum of the absolute values of the gradients of the samples within the 0th interval. This represents the interval weight coefficient for the first interval. This represents the sum of the absolute values of the gradients of the samples within the first interval. This represents the interval weight coefficient for the second interval. This represents the sum of the absolute values of the gradients of the samples within the second interval. This represents the sum of the absolute values of the gradients of the samples within the third interval. This indicates the gradient percentage in the first interval. This represents the gradient percentage in the second interval, where, and Both are greater than 0 and less than 1. and The sum is less than 1.
[0026] Furthermore, the calculation expressions for the weight coefficients of positive and negative samples in S43 are as follows:
[0027] ,
[0028] ,
[0029] ,
[0030] ,
[0031] in, This represents the weighting coefficient of the positive samples. This represents the weighting coefficient of the negative samples. This represents the ratio of the average gradient of positive samples to the average gradient of negative samples. This represents the average gradient of the positive samples. This represents the average gradient of the negative samples. This represents the total number of positive samples. Indicates the first One positive sample, This represents the gradient obtained from the loss value of the sample. Indicates the sample loss value. This represents the output of the extreme precipitation prediction model corresponding to the sample. This represents the total number of negative samples. Indicates the first There are 10 negative samples, of which... Greater than 0 and less than 1.
[0032] Furthermore, the calculation expression for the loss function of extreme precipitation prediction in S44 is as follows:
[0033] ,
[0034] ,
[0035] in, This represents the loss function for extreme precipitation prediction. This represents the total number of samples in interval 0. Represents the 0th interval. One sample, This represents the loss of a single sample within the interval. This represents the total number of samples in the first interval. Indicates the first interval. One sample, This represents the total number of samples in the second interval. Indicates the first term in the second interval. One sample, This represents the total number of samples in the third interval. Indicates the third interval. One sample, The sample is identified as The focus loss function for the samples.
[0036] Furthermore, the method also includes the following steps:
[0037] S5. Calculate the gradient value of the loss function for extreme precipitation prediction;
[0038] S6. Update the network parameters of the extreme precipitation prediction model using the gradient value of the loss function based on extreme precipitation prediction.
[0039] S7. Repeatedly calculate the loss function for extreme precipitation prediction using the updated network parameters of the extreme precipitation prediction model until the preset training time threshold is reached.
[0040] The beneficial effects of this invention are as follows: This invention provides a loss function calculation method for imbalanced datasets. By changing the traditional total loss calculation method, the loss calculation process is interval-based, and combined with statistical interval samples and interval sample loss gradients, automatic adjustment of various parameters is achieved. During backpropagation, the interval of the dominant gradient is fixed, avoiding artificially high confidence levels and improving the model's generalization ability. It can effectively improve model performance compared to other loss functions on multiple class-imbalanced datasets. The loss function calculation method provided by this invention uses an interval-based loss calculation method combined with a fixed dominant interval strategy, making the extreme precipitation prediction model pay more attention to nearby decision boundary values, i.e., samples near the classification boundary, thereby improving the generalization ability of the extreme precipitation prediction model. The loss function calculation method provided by this invention… The loss function calculation method adds statistical analysis of the sample loss gradient and adaptively sets the class weight parameters based on the sample loss gradient. This reduces the work of adjusting parameters and prevents the risk of insufficient attention to a few samples and overfitting caused by fixed parameters. The loss function calculation method provided by this invention involves all calculations based on the most direct factor in the model's backpropagation, namely the loss gradient, which has strong interpretability. Compared with traditional loss functions, it can effectively improve the prediction accuracy of extreme precipitation prediction models for extreme precipitation events. For other binary classification datasets with imbalanced class numbers, it can also achieve adaptive dynamic weighting of the two classes, saving the cost required by traditional fixed-weight loss function calculation methods to repeatedly adjust parameters to pursue the optimal result.
[0041] On the other hand, the present invention also provides a loss function calculation device based on the above-described loss function calculation method for imbalanced datasets, the device comprising:
[0042] The first module is used to process the acquired ERA5 precipitation data files and construct a weather dataset, in which the samples in the weather dataset include extreme precipitation samples and non-extreme precipitation samples.
[0043] The second module is used to input samples from the weather dataset into the extreme precipitation prediction model, and to convert the output of the extreme precipitation prediction model into probability values through the sigmoid function, which are used as the probability of the extreme precipitation event corresponding to the sample.
[0044] The third module is used to define the probability that a sample is correctly predicted.
[0045] The fourth module is used to calculate interval loss based on the probability that a sample is correctly predicted, and to construct a loss function for extreme precipitation prediction.
[0046] Furthermore, the aforementioned loss function calculation device also includes:
[0047] The fifth module is used to calculate the gradient value of the loss function for extreme precipitation prediction;
[0048] The sixth module is used to update the network parameters of the extreme precipitation prediction model based on the gradient value of the loss function for extreme precipitation prediction.
[0049] The seventh module is used to repeatedly calculate the loss function for extreme precipitation prediction using the network parameters of the updated extreme precipitation prediction model until a preset training threshold is reached.
[0050] The beneficial effects of the present invention are as follows: The loss function calculation device provided by the present invention is a device corresponding to the above-mentioned loss function calculation method, which is used to implement the above method, thereby effectively improving the generalization ability of extreme precipitation prediction models and the accuracy of extreme precipitation prediction for binary classification datasets such as weather datasets with imbalanced class numbers.
[0051] Other advantages of the present invention will be analyzed in more detail in the following embodiments. Attached Figure Description
[0052] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0053] Figure 1 This is a flowchart illustrating the steps of a loss function calculation method for an imbalanced dataset in an embodiment of the present invention.
[0054] Figure 2 This is a block diagram of a loss function calculation device based on a loss function calculation method for an imbalanced dataset, according to an embodiment of the present invention.
[0055] Figure 3 This is a schematic diagram comparing the CSI index of this solution with the other five methods on the training and test sets of the weather dataset in this embodiment of the invention.
[0056] Figure 4 This is a schematic diagram comparing the CSI index difference results of this scheme and the other 5 methods on the training and test sets of the weather dataset in this embodiment of the invention.
[0057] Figure 5 This is a schematic diagram comparing the CSI index difference results of this scheme and the other 5 methods on the training and test sets of the yeast1 dataset in this embodiment of the invention.
[0058] Figure 6 This is a schematic diagram comparing the CSI index difference results of this scheme and the other 5 methods on the training and test sets of the page-blocks0 dataset in this embodiment of the invention.
[0059] Figure 7 This diagram illustrates the comparison of the CSI metric differences between this method and the other five methods on the training and test sets of the abalone-17_vs_7-8-9-10 dataset in this embodiment of the invention. Detailed Implementation
[0060] 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, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without inventive effort are within the scope of protection of the present invention.
[0061] The fundamental difficulty in predicting extreme precipitation lies in the rarity of such events. Whether precipitation is extreme is usually determined by its deviation from local historical climate conditions, rather than by an absolute threshold. This deviation is typically represented using percentile methods. For example, extreme precipitation is defined as: 24-hour precipitation ≥ the 95th percentile of the local historical rainfall.
[0062] Heavy rain is defined as 24-hour precipitation ≥ 50 mm, torrential rain as 24-hour precipitation ≥ 100 mm, and extremely heavy rain as 24-hour precipitation ≥ 250 mm. According to meteorological bureau yearbook data, even in coastal areas with abundant rainfall, the average number of days with daily precipitation ≥ 100 mm accounts for only about 1%-2% of the year, and it is even rarer in other regions.
[0063] The rarity of extreme precipitation events exacerbates the contradiction between the minority and majority classes of samples. During learning, the model naturally favors the majority class, making it difficult to fully explore the patterns and characteristics of the minority class. At the same time, the patterns inherent in extreme precipitation events are complex, further highlighting the contradiction between simple and difficult samples.
[0064] like Figure 1 As shown, in one embodiment of the present invention, the present invention provides a method for calculating a loss function for an imbalanced dataset, comprising the following steps:
[0065] S1. Process the acquired ERA5 precipitation data file and construct a weather dataset. In the weather dataset, the fewer extreme precipitation samples are used as positive samples, and the more numerous non-extreme precipitation samples are used as negative samples.
[0066] S1 includes the following steps:
[0067] S11. Obtain ERA5 precipitation data files according to the preset time span, time resolution, spatial coverage and spatial resolution;
[0068] In this embodiment, the extreme weather dataset is constructed based on ERA5 precipitation data, with a preset time span of 2013 to 2022, a time resolution of 1 hour, a spatial coverage of 80°E-135°E, 3°N-58°N, and a spatial resolution of 0.25°.
[0069] S12. Divide the acquired ERA5 precipitation data file into... The three-dimensional mesh data, in which, The size of the spatial grid is 24, which refers to 24 consecutive hours. In this embodiment, the three-dimensional data is in .npy format, and the three-dimensional data is used to represent the precipitation information at 24 consecutive moments on a 224×224 spatial grid.
[0070] S13. Calculate the mean and standard deviation of precipitation for each divided 3D grid data within a preset time span. Take precipitation events exceeding the precipitation threshold in the 3D grid data as extreme precipitation samples and the remaining events as non-extreme precipitation samples to form a weather dataset. The precipitation threshold is equal to the sum of the mean precipitation and N times the standard deviation of precipitation. The fewer extreme precipitation samples are taken as positive samples and the more numerous non-extreme precipitation samples are taken as negative samples.
[0071] In this embodiment, since the geographical span of the area corresponding to the spatial coverage is large, the judgment of extreme precipitation needs to take into account local climate characteristics. Therefore, the value of N is 2 or 3.
[0072] S2. Input the samples in the weather dataset into the extreme precipitation prediction model, and convert the output of the extreme precipitation prediction model into a probability value through the sigmoid function, which is used as the probability of the extreme precipitation event corresponding to the sample.
[0073] In this scheme, all samples are normalized using a maximum-minimum method before being input into the extreme precipitation prediction model. In this embodiment, the extreme precipitation prediction model uses the past 24 hours and a spatial size of 224 within the region. Using 224 sample data as input, the extreme precipitation occurrence of each spatial grid in the same area is predicted in the next 24 hours;
[0074] The extreme precipitation prediction model uses U-Net as its basic architecture, with both its input and output channels set to 24, and the first-layer baseline channels set to 64. During training, the extreme precipitation prediction model uses the Adam optimizer with an initial learning rate of 0.01, and a step size scheduling strategy that decays to 0.1 times every 30 rounds. In this embodiment, all models are trained uniformly for 100 rounds with a batch size of 24, while a fixed random seed is used, and deterministic computation is enabled to ensure reproducibility.
[0075] S3. Define the probability that a sample is correctly predicted;
[0076] The expression for calculating the probability that a sample is correctly predicted in S3 is as follows:
[0077] , ,
[0078] in, This represents the probability that the sample is correctly predicted. The sample is identified as The probability that a sample is correctly predicted. This represents the probability interval of a sample being correctly predicted, where, when This indicates that the sample is a positive sample. This indicates that the sample is a negative sample.
[0079] S4. Based on the probability that the sample is correctly predicted, calculate the interval loss and construct the loss function for extreme precipitation prediction.
[0080] S4 includes the following steps:
[0081] S41. Set decision boundary values and divide the interval of the correct prediction of the sample into four intervals based on the decision boundary values. The four intervals include the 0th interval, the 1st interval, the 2nd interval and the 3rd interval, respectively. The 1st interval and the 2nd interval are the intervals adjacent to the decision boundary values.
[0082] In this scheme, the probability interval corresponding to interval 0 is [0, 0.5-margin), the probability interval corresponding to interval 1 is [0.5-margin, 0.5), the probability interval corresponding to interval 2 is [0.5, 0.5+margin), and the probability interval corresponding to interval 3 is [0.5+margin, 1]. Here, margin represents the decision boundary value; in this embodiment, the value of margin is 0.1.
[0083] S42. Set the proportions of the first interval and the second interval in the total gradient as the first interval gradient proportion and the second interval gradient proportion, respectively, and calculate the interval weight coefficients of the first interval and the second interval.
[0084] In this scheme, by introducing dynamically changing interval weight coefficients for the first and second intervals, the change in the magnitude of the total loss value loses its indicative function. This ensures that the samples in the first and second intervals dominate the gradient in each backpropagation, making the result of each weight update more biased towards the first and second intervals. Although in this embodiment, the first and second intervals are intervals adjacent to the decision boundary values, the intervals affected by the interval weight coefficients are not limited to the intervals adjacent to the boundary.
[0085] The calculation expressions for the interval weight coefficients of the first and second intervals in S42 are as follows:
[0086] ,
[0087] in, This represents the sum of the absolute values of the gradients of the samples within the 0th interval. This represents the interval weight coefficient for the first interval. This represents the sum of the absolute values of the gradients of the samples within the first interval. This represents the interval weight coefficient for the second interval. This represents the sum of the absolute values of the gradients of the samples within the second interval. This represents the sum of the absolute values of the gradients of the samples within the third interval. This indicates the gradient percentage in the first interval. This represents the gradient percentage in the second interval, where, and Both are greater than 0 and less than 1. and The sum is less than 1.
[0088] In this plan, due to , , and It is different in each complete round of training, therefore and It changes dynamically to ensure that the proportions of the first and second intervals in the total gradient are respectively... and In this embodiment, the gradient percentage of the first interval is set to 0.35, and the gradient percentage of the second interval is set to 0.53.
[0089] S43. Calculate the average gradient of positive and negative samples, and then calculate the weight coefficients of positive and negative samples respectively.
[0090] In this scheme, the weight coefficient of the negative sample is equal to 1 minus the weight coefficient of the positive sample.
[0091] The formula for calculating the weight coefficient of the positive sample in S43 is as follows:
[0092] , ,
[0093] ,
[0094] ,
[0095] ,
[0096] in, This represents the weighting coefficient of the positive samples. This represents the weighting coefficient of the negative samples. This represents the ratio of the average gradient of positive samples to the average gradient of negative samples. This represents the average gradient of the positive samples. This represents the average gradient of the negative samples. This represents the total number of positive samples. Indicates the first One positive sample, This represents the gradient obtained from the loss value of the sample. Indicates the sample loss value. This represents the output of the extreme precipitation prediction model corresponding to the sample. This represents the total number of negative samples. Indicates the first There are 10 negative samples, of which 10 are negative samples. Greater than 0 and less than 1.
[0097] In this embodiment, the method of calculating the loss function for a single sample is not limited. Binary cross-entropy loss function, focus loss function, etc. can be used. However, as verified by this scheme, the focus loss function performs better when there is a lack of hyperparameters for iteration in the initial training.
[0098] As an example, this invention provides a method for calculating the loss value and gradient of a single sample using a focus loss function; the calculation expressions for the loss value of a sample and the gradient obtained from the loss value of the sample are as follows:
[0099] ,
[0100] ,
[0101] in, Indicates the difficulty level. Let represent the absolute value of the gradient obtained from the loss value of the sample, where The value of the difficulty coefficient is typically greater than 1 and less than 10. In this scheme, the higher the value of the difficulty coefficient, the less attention the model pays to simple samples and the more attention it pays to samples that are more difficult to identify. Furthermore, the gradient difference between simple and complex samples is also greater. This scheme assumes that difficult or simple samples may contain more noise, and the most valuable samples to learn are those near the classification boundary. Fully learning from these samples can improve the model's generalization ability.
[0102] S44. Based on the interval weight coefficients of the first and second intervals, and the weight coefficients of the positive and negative samples, construct the loss function for extreme precipitation prediction.
[0103] The calculation expression for the loss function of extreme precipitation prediction in S44 is as follows:
[0104] ,
[0105] ,
[0106] in, This represents the loss function for extreme precipitation prediction. This represents the total number of samples in interval 0. Represents the 0th interval. One sample, This represents the loss of a single sample within the interval. This represents the total number of samples in the first interval. Indicates the first interval. One sample, This represents the total number of samples in the second interval. Indicates the first term in the second interval. One sample, This represents the total number of samples in the third interval. Indicates the third interval. One sample, Indicates that the sample is identified as The focus loss function for the samples.
[0107] In one embodiment, the above method further includes the following steps:
[0108] S5. Calculate the gradient value of the loss function for extreme precipitation prediction;
[0109] S6. Update the network parameters of the extreme precipitation prediction model using the gradient value of the loss function based on extreme precipitation prediction.
[0110] S7. Repeatedly calculate the loss function for extreme precipitation prediction using the updated network parameters of the extreme precipitation prediction model until a preset training threshold is reached. In this embodiment, the preset training threshold is 100. By repeatedly calculating and optimizing the loss function, the prediction results for extreme precipitation can be made infinitely closer to the actual results, thereby improving the accuracy of extreme precipitation prediction.
[0111] like Figure 2 As shown, in another embodiment of the present invention, the present invention also provides a loss function calculation device based on the above-described loss function calculation method for imbalanced datasets, comprising:
[0112] The first module is used to process the acquired ERA5 precipitation data files and construct a weather dataset, in which the samples in the weather dataset include extreme precipitation samples and non-extreme precipitation samples.
[0113] The first module includes:
[0114] The first submodule is used to obtain ERA5 precipitation data files based on preset time span, time resolution, spatial coverage and spatial resolution;
[0115] The second submodule is used to divide the acquired ERA5 precipitation data files into... The three-dimensional mesh data, in which, The size of the spatial grid is used for dividing the space; 24 refers to a continuous 24 hours.
[0116] The third submodule is used in S13 to calculate the mean and standard deviation of precipitation for each divided 3D grid data within a preset time span. Precipitation events exceeding the precipitation threshold in the 3D grid data are taken as extreme precipitation samples, and the remaining events are taken as non-extreme precipitation samples, forming a weather dataset. The precipitation threshold is equal to the sum of the mean precipitation and N times the standard deviation of precipitation. The fewer extreme precipitation samples are taken as positive samples, and the more numerous non-extreme precipitation samples are taken as negative samples.
[0117] The second module is used to input samples from the weather dataset into the extreme precipitation prediction model, and to convert the output of the extreme precipitation prediction model into probability values through the sigmoid function, which are used as the probability of the extreme precipitation event corresponding to the sample.
[0118] The fourth submodule is used to treat a sample as a positive sample when the probability of the extreme precipitation event corresponding to the sample is greater than or equal to a preset binary classification threshold.
[0119] The fifth submodule is used to treat a sample as a negative sample if the probability of the extreme precipitation event corresponding to the sample is less than a preset binary classification threshold.
[0120] The third module is used to define the probability that a sample is correctly predicted.
[0121] The expression for calculating the probability that the sample is correctly predicted is as follows:
[0122] , ,
[0123] in, This represents the probability that the sample is correctly predicted. This represents the probability of an extreme precipitation event corresponding to a positive sample. Indicates that the sample is identified as The probability that a sample is correctly predicted. This represents the probability interval of a sample being correctly predicted, where, when This indicates that the sample is a positive sample. This indicates that the sample is a negative sample.
[0124] The fourth module is used to calculate interval loss based on the probability that a sample is correctly predicted, and to construct a loss function for extreme precipitation prediction.
[0125] The sixth submodule is used to set the decision boundary value and divide the interval of the correct prediction of the sample into four intervals based on the decision boundary value. The four intervals include the 0th interval, the 1st interval, the 2nd interval and the 3rd interval, respectively. The 1st interval and the 2nd interval are the intervals adjacent to the decision boundary value.
[0126] The seventh submodule is used to set the proportions of the first interval and the second interval in the total gradient as the first interval gradient proportion and the second interval gradient proportion, respectively, and to calculate the interval weight coefficients of the first interval and the second interval.
[0127] The calculation expressions for the interval weight coefficients of the first interval and the second interval are as follows:
[0128] ,
[0129] in, This represents the sum of the absolute values of the gradients of the samples within the 0th interval. This represents the interval weight coefficient for the first interval. This represents the sum of the absolute values of the gradients of the samples within the first interval. This represents the interval weight coefficient for the second interval. This represents the sum of the absolute values of the gradients of the samples within the second interval. This represents the sum of the absolute values of the gradients of the samples within the third interval. This indicates the gradient percentage in the first interval. This represents the gradient percentage in the second interval, where, and Both are greater than 0 and less than 1. and The sum is less than 1.
[0130] The eighth submodule is used to calculate the average gradient of positive and negative samples, and to calculate the weight coefficients of positive and negative samples respectively.
[0131] The formula for calculating the weight coefficient of the positive sample is as follows:
[0132] , ,
[0133] ,
[0134] ,
[0135] ,
[0136] in, This represents the weighting coefficient of the positive samples. This represents the weighting coefficient of the negative samples. This represents the ratio of the average gradient of positive samples to the average gradient of negative samples. This represents the average gradient of the positive samples. This represents the average gradient of the negative samples. This represents the total number of positive samples. Indicates the first One positive sample, This represents the gradient obtained from the loss value of the sample. Indicates the sample loss value. This represents the output of the extreme precipitation prediction model corresponding to the sample. This represents the total number of negative samples. Indicates the first There are 10 negative samples, of which 10 are negative samples. Greater than 0 and less than 1.
[0137] The ninth submodule is used to construct a loss function for extreme precipitation prediction based on the interval weight coefficients of the first and second intervals, as well as the weight coefficients of positive and negative samples.
[0138] The calculation expression for the loss function of the extreme precipitation prediction is as follows:
[0139] ,
[0140] ,
[0141] in, This represents the loss function for extreme precipitation prediction. This represents the total number of samples in interval 0. Represents the 0th interval. One sample, This represents the loss of a single sample within the interval. This represents the total number of samples in the first interval. Indicates the first interval. One sample, This represents the total number of samples in the second interval. Indicates the first term in the second interval. One sample, This represents the total number of samples in the third interval. Indicates the third interval. One sample, Indicates that the sample is identified as The focus loss function for the samples.
[0142] In one embodiment, the loss function calculation device further includes:
[0143] The fifth module is used to calculate the gradient value of the loss function for extreme precipitation prediction;
[0144] The sixth module is used to update the network parameters of the extreme precipitation prediction model based on the gradient value of the loss function for extreme precipitation prediction.
[0145] The seventh module is used to repeatedly calculate the loss function for extreme precipitation prediction using the network parameters of the updated extreme precipitation prediction model until the loss function is less than or equal to a preset loss threshold.
[0146] In one embodiment of the present invention, the loss function calculation method provided by this solution was compared with five other loss function calculation methods. To ensure a fair comparison, all experimental conditions, including the weather dataset, extreme precipitation prediction model, training strategy, and evaluation metrics, were identical except for the loss function used. The other five loss function calculation methods are: Method 1, Binary Cross-Entropy Loss (BCE Loss); Method 2, Focal Loss; Method 3, Label-Distribution-Aware Margin Loss (LDAM Loss); Method 4, Label-Distribution-Aware Margin Loss with Extended Weighted Training Strategy (LDAM_DRW Loss); and Method 5, Parameter-Free Loss (PF Loss).
[0147] The comparison results of the loss function proposed in this scheme with the other five loss function calculation methods on the weather dataset are shown in Table 1. The evaluation indicators used in this comparative experiment include True Positive Rate (TPR), True Negative Rate (TNR), Accuracy (ACC), F1 score, and Critical Success Index (CSI). TPR (Total Recall) represents the model's recall ability for positive samples, i.e., the proportion of true positive samples correctly predicted as positive by the model, also known as recall rate. TNR (Total Rejection Rate) represents the model's ability to identify negative samples, i.e., the proportion of true negative samples correctly predicted as negative by the model. ACC (Accuracy Accuracy) represents the proportion of correctly predicted samples out of the total number of samples. F1 Score is a metric that comprehensively considers precision and recall, and its main advantage is that it can better evaluate model performance when the class distribution is imbalanced. CSI (Corrective Judgment Index) measures the correctness of model predictions, reflecting the model's accuracy in real-world applications by simultaneously considering false positive and false negative rates. The values of TPR, TNR, F1 score, and CSI are all between 0 and 1, with higher values indicating better model performance.
[0148] Table 1. Comparison of evaluation indicators on the weather dataset
[0149]
[0150] As can be seen from Table 1, considering the results of the F1 score and CSI indicators that comprehensively consider the model's capabilities, the actual scores of the loss function calculation method provided by this scheme are higher than those of the other five methods, which fully demonstrates the effectiveness of the present invention in improving the model's generalization ability and prediction accuracy.
[0151] like Figure 3 As shown, this experiment also plotted the CSI index of this scheme (CBE(ours)) and the other 5 methods on the training and test sets of the weather dataset over 100 training epochs. Train represents the CSI index on the training set, and Test represents the CSI index on the test set.
[0152] like Figure 4 As shown, to more intuitively illustrate the effectiveness of this scheme in improving the model's generalization ability, this experiment also... Figure 1 Based on this, the critical success index difference (Train CSI-Test CSI) results of this scheme (CBE(ours)) and the other 5 methods were plotted for 100 training epochs on the training set and test set during the training process.
[0153] Depend on Figure 3 and Figure 4 It can be observed that, within the same training period, the highest CSI index achieved by this scheme on the test set is higher than that of the other 5 methods, and the difference between the CSI index and the CSI index on the training set during the same period is small. This indicates that this scheme has a significant effect on improving the generalization ability of the model.
[0154] In addition, to comprehensively evaluate the generalization ability of the loss function calculation method proposed in this scheme on the general class imbalance problem, three classic imbalanced classification datasets from the KEEL dataset repository were selected: the yeast1 dataset, the page-blocks0 dataset, and the abalone-17_vs_7-8-9-10 dataset. These three datasets are all derived from different real-world scenarios and have different imbalance ratios, providing a reliable benchmark for verifying the performance of the loss function on structured data.
[0155] The yeast1 dataset originates from the yeast database in the UCI Machine Learning Repository and aims to predict the subcellular localization of proteins based on their sequence attributes. The dataset contains eight real-valued features to describe different spectral properties of protein sequences. Its classification task exhibits class imbalance; in this binary classification version, the target class "NUC" is defined as the positive class (minority class), accounting for approximately 28.9% of all 1484 samples; the remaining classes are merged into the negative class (majority class), accounting for 71.1%. The dataset has an imbalance ratio (IR) of 2.46, making it suitable for evaluating model performance in mildly imbalanced scenarios.
[0156] The page-blocks0 dataset is a dataset built for classifying the physical layout blocks of document pages. It contains 5472 instances, each described by 10 features (4 real numbers and 6 integers), covering the geometric attributes of page blocks, such as height, length, area, and eccentricity. In this binary classification benchmark, the minority class "text blocks" are labeled as positive examples, accounting for 10.21% of the total samples; the remaining non-text blocks (such as graphics, horizontal lines, etc.) are merged into negative examples, accounting for 89.79%. Its imbalance ratio (IR) is 8.79, and it is often used to validate the model's ability to handle moderately imbalanced real-world data.
[0157] The abalone-17_vs_7-8-9-10 dataset is derived from the classic abalone age prediction dataset (Abalone) and is a highly imbalanced binary classification problem. The dataset contains eight features (seven real numbers and one nominal attribute "Sex"), all physical measurements of abalone. Under this setting, individuals aged 17 are defined as the target minority class (positive samples), while individuals aged 7, 8, 9, and 10 are merged into the majority class (negative samples). Out of a total of 2338 samples, positive samples account for only about 2.48%, while negative samples account for 97.52%, resulting in an imbalance ratio (IR) as high as 39.31, making it suitable for testing the robustness of algorithms under extreme class imbalance conditions.
[0158] In this embodiment, the three datasets were randomly divided into training and test sets in an 8:2 ratio, ensuring consistency in class distribution before and after the division. To maintain the focus and comparability of the experiment, a simple multilayer perceptron was used as the basic classifier. This network contains two hidden layers with dimensions of 64 and 32, interspersed with batch normalization layers, ReLU activation functions, and Dropout layers to alleviate overfitting. All experiments used the Adam optimizer with an initial learning rate of 1e-3, coupled with a step decay strategy. The training batch size was uniformly set to 64, the maximum number of training epochs was 100, and a fixed random seed was used to ensure the reproducibility of the results.
[0159] As shown in Table 2, the quantitative comparison results of the loss function calculation method provided in this scheme with the other 5 methods on the yellow1 dataset in terms of TPR, TNR, ACC, F1 score and CSI evaluation indicators.
[0160] Table 2. Quantitative comparison results of evaluation indicators on the yeast1 dataset.
[0161]
[0162] As shown in Table 3, the quantitative comparison results of the loss function calculation method provided in this scheme with the other 5 methods on the page-blocks0 dataset in terms of TPR, TNR, ACC, F1 score and CSI evaluation indicators.
[0163] Table 3. Quantitative Comparison Results of Evaluation Metrics on the Page-Blocks0 Dataset
[0164]
[0165] As shown in Table 4, the quantitative comparison results of the loss function calculation method provided in this scheme with the other 5 methods on the abalone-17_vs_7-8-9-10 dataset in terms of TPR, TNR, ACC, F1 score and CSI evaluation indicators are presented.
[0166] Table 4. Quantitative comparison results of evaluation metrics on the abalone-17_vs_7-8-9-10 dataset.
[0167]
[0168] Based on the CSI test metrics shown in Tables 2, 3, and 4 on three randomly selected datasets, the loss function calculation method provided in this approach outperforms the other five methods. Due to the relatively small size of the class-imbalanced dataset provided on the KEEL website, the test metrics fluctuate significantly, but based on… Figure 5 , Figure 6 and Figure 7 It can still be observed that the critical success index difference (Train CSI-Test CSI) of the loss function provided by this scheme is lower than that of the other five methods over 100 training epochs on the training and test sets of the three datasets. This further confirms the effectiveness of the loss function calculation method provided by this scheme in improving the model's generalization ability while improving the model's predictive classification ability.
[0169] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention.
Claims
1. A loss function calculation method for imbalanced datasets, used for extreme precipitation prediction, characterized in that, Includes the following steps: S1. Process the acquired ERA5 precipitation data file and construct a weather dataset. In the weather dataset, the fewer extreme precipitation samples are used as positive samples, and the more numerous non-extreme precipitation samples are used as negative samples. S2. Input the samples in the weather dataset into the extreme precipitation prediction model, and convert the output of the extreme precipitation prediction model into a probability value through the sigmoid function, which is used as the probability of the extreme precipitation event corresponding to the sample. S3. Define the probability that a sample is correctly predicted; The expression for calculating the probability that a sample is correctly predicted in S3 is as follows: , , in, This represents the probability that the sample is correctly predicted. The sample is identified as The probability that a sample is correctly predicted. This represents the probability interval of a sample being correctly predicted, where, when This indicates that the sample is a positive sample. This indicates that the sample is a negative sample; S4. Based on the probability that the sample is correctly predicted, calculate the interval loss and construct the loss function for extreme precipitation prediction. S4 includes the following steps: S41. Set decision boundary values and divide the interval of the correct prediction of the sample into four intervals based on the decision boundary values. The four intervals include the 0th interval, the 1st interval, the 2nd interval and the 3rd interval, respectively. The 1st interval and the 2nd interval are the intervals adjacent to the decision boundary values. S42. Set the proportions of the first interval and the second interval in the total gradient as the first interval gradient proportion and the second interval gradient proportion, respectively, and calculate the interval weight coefficients of the first interval and the second interval. The calculation expressions for the interval weight coefficients of the first and second intervals in S42 are as follows: , in, This represents the sum of the absolute values of the gradients of the samples within the 0th interval. This represents the interval weight coefficient for the first interval. This represents the sum of the absolute values of the gradients of the samples within the first interval. This represents the interval weight coefficient for the second interval. This represents the sum of the absolute values of the gradients of the samples within the second interval. This represents the sum of the absolute values of the gradients of the samples within the third interval. This indicates the gradient percentage in the first interval. This represents the gradient percentage in the second interval, where, and Both are greater than 0 and less than 1. and The sum is less than 1; S43. Calculate the average gradient of positive and negative samples, and then calculate the weight coefficients of positive and negative samples respectively. S44. Based on the interval weight coefficients of the first and second intervals, and the weight coefficients of the positive and negative samples, construct the loss function for extreme precipitation prediction. S5. Calculate the gradient value of the loss function for extreme precipitation prediction; S6. Update the network parameters of the extreme precipitation prediction model using the gradient value of the loss function based on extreme precipitation prediction. S7. Repeatedly calculate the loss function for extreme precipitation prediction using the updated network parameters of the extreme precipitation prediction model until the preset training time threshold is reached.
2. The method for calculating the loss function for imbalanced datasets according to claim 1, characterized in that, S1 includes the following steps: S11. Obtain ERA5 precipitation data files according to the preset time span, time resolution, spatial coverage and spatial resolution; S12. Divide the acquired ERA5 precipitation data file into... The three-dimensional mesh data, in which, The size of the spatial grid is used for dividing the space; 24 refers to a continuous 24 hours. S13. Calculate the mean and standard deviation of precipitation for each divided 3D grid data within a preset time span. Take precipitation events exceeding the precipitation threshold in the 3D grid data as extreme precipitation samples and the remaining events as non-extreme precipitation samples to form a weather dataset. The precipitation threshold is equal to the sum of the mean precipitation and N times the standard deviation of precipitation. The fewer extreme precipitation samples are taken as positive samples and the more numerous non-extreme precipitation samples are taken as negative samples.
3. The method for calculating the loss function for imbalanced datasets according to claim 1, characterized in that, The calculation expressions for the weight coefficients of positive and negative samples in S43 are as follows: , , , , , in, This represents the weighting coefficient of the positive samples. This represents the weighting coefficient of the negative samples. This represents the ratio of the average gradient of positive samples to the average gradient of negative samples. This represents the average gradient of the positive samples. This represents the average gradient of the negative samples. This represents the total number of positive samples. Indicates the first One positive sample, This represents the gradient obtained from the loss value of the sample. Indicates the sample loss value. This represents the output of the extreme precipitation prediction model corresponding to the sample. This represents the total number of negative samples. Indicates the first There are 10 negative samples, of which... Greater than 0 and less than 1.
4. The method for calculating the loss function for imbalanced datasets according to claim 3, characterized in that, The calculation expression for the loss function of extreme precipitation prediction in S44 is as follows: , , in, This represents the loss function for extreme precipitation prediction. This represents the total number of samples in interval 0. Represents the 0th interval. One sample, This represents the loss of a single sample within the interval. This represents the total number of samples in the first interval. Indicates the first interval. One sample, This represents the total number of samples in the second interval. Indicates the first term in the second interval. One sample, This represents the total number of samples in the third interval. Indicates the third interval. One sample, The sample is identified as The focus loss function for the samples.
5. A loss function calculation device based on the loss function calculation method for imbalanced datasets according to any one of claims 1-4, used for extreme precipitation prediction, characterized in that, The device includes: The first module is used to process the acquired ERA5 precipitation data files and construct a weather dataset, in which the samples in the weather dataset include extreme precipitation samples and non-extreme precipitation samples. The second module is used to input samples from the weather dataset into the extreme precipitation prediction model, and to convert the output of the extreme precipitation prediction model into probability values through the sigmoid function, which are used as the probability of the extreme precipitation event corresponding to the sample. The third module is used to define the probability that a sample is correctly predicted. The fourth module is used to calculate interval loss based on the probability that a sample is correctly predicted, and to construct a loss function for extreme precipitation prediction.
6. The loss function calculation device according to claim 5, characterized in that, The device further includes: The fifth module is used to calculate the gradient value of the loss function for extreme precipitation prediction; The sixth module is used to update the network parameters of the extreme precipitation prediction model based on the gradient value of the loss function for extreme precipitation prediction. The seventh module is used to repeatedly calculate the loss function for extreme precipitation prediction using the network parameters of the updated extreme precipitation prediction model until a preset training threshold is reached.
Citation Information
Patent Citations
Short temporary rainfall forecasting method and system based on dynamic neural network architecture
CN121189386A
Loss function optimization method and apparatus, computer device, and storage medium
WO2020143304A1