Method for predicting voltage, specific energy and specific capacity of battery based on Bayesian optimization-GRU
Patent Information
- Application Number
- CN202510661625.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-22
- Publication Date
- 2025-10-17
AI Technical Summary
Traditional battery performance parameter prediction methods are limited by model complexity and data noise, resulting in insufficient prediction accuracy and robustness.
A GRU network model based on Bayesian optimization is adopted. Through data preprocessing and feature extraction, the hyperparameters of the GRU network are tuned in combination with the Bayesian optimization algorithm to improve the prediction accuracy and generalization ability of the model.
The prediction accuracy of battery voltage, specific energy and specific capacity and the generalization ability of the model are significantly improved, making it suitable for performance prediction of different types of batteries.
Smart Images

Figure CN120805641A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of batteries, in particular to a method for predicting battery performance parameters, and specifically to a method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU, which is used to accurately predict the voltage, specific energy and specific capacity of the battery. BACKGROUND
[0002] As a key component of energy storage and conversion, the accurate prediction of battery performance parameters such as voltage, specific energy and specific capacity is crucial for battery health management, performance optimization and safety evaluation. However, traditional prediction methods are often limited by model complexity, data noise and parameter tuning difficulties, resulting in insufficient prediction accuracy and robustness. SUMMARY
[0003] The application proposes a method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU. The method first preprocesses and extracts features from the data to construct a dataset containing battery cycle number, voltage, specific energy and specific capacity. Then, a GRU neural network model is built to model battery performance parameters using the advantages of GRU in processing time series data. The key innovation is to introduce a Bayesian optimization algorithm to intelligently optimize the hyperparameters of the GRU network (such as learning rate, batch size, iteration number, etc.), thereby significantly improving the prediction accuracy and generalization ability of the model.
[0004] To achieve the above purpose, the application adopts the following technical solutions:
[0005] A method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU, comprising the following steps:
[0006] Defining and training a GRU network model, the input of which is the attribute information of the battery, and the output of which is the battery voltage, specific energy and specific capacity;
[0007] Using Bayesian optimization to find the best parameter combination of the GRU network model;
[0008] Re-training the GRU network model based on the best parameter combination;
[0009] Using the trained GRU network model to predict the battery voltage, specific energy and specific capacity.
[0010] Preferably, the GRU network model includes one GRU layer and three fully connected layers.
[0011] Preferably, the specific steps of using Bayesian optimization to find the best parameter combination of the GRU network model include:
[0012] The average loss is obtained by adding the mean square error losses of the voltage, specific energy and specific capacity prediction on the verification set and averaging them, and the Bayesian optimization finds the optimal parameter combination according to the average loss, and the average loss corresponding to the optimal parameter combination is the smallest.
[0013] Preferably, the parameters in the parameter combination include: the full connection layer size, the number of layers of the full connection layer and the learning rate.
[0014] Preferably, the attribute information of the battery includes: material composition, structure parameters.
[0015] Compared with the prior art, the method has the following advantages:
[0016] The hyperparameters of the GRU network are optimized by the Bayesian optimization algorithm, which can significantly improve the prediction accuracy and generalization ability of the model.
[0017] By utilizing the advantage of the GRU network that can capture long-term dependencies in time series data, the performance parameters of the battery, such as voltage, specific energy and specific capacity, can be more accurately predicted.
[0018] The method has universality and can be applied to different types of battery performance prediction problems, providing strong support for the research and optimization of batteries. BRIEF DESCRIPTION OF DRAWINGS
[0019] Fig. 1 The principle diagram of the method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU.
[0020] Figs. 2(a)-2(b) The flowchart of the method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU.
[0021] Fig. 3 The schematic diagram of the training process of the GRU network model. DETAILED DESCRIPTION
[0022] As Figs. 1-3 A method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU, comprising the following steps:
[0023] Defining and training a GRU network model, the input of which is the attribute information of the battery, and the output of which is the battery voltage, specific energy and specific capacity;
[0024] Using Bayesian optimization to find the optimal parameter combination of the GRU network model;
[0025] Re-training the GRU network model based on the optimal parameter combination;
[0026] Using the trained GRU network model to predict the battery voltage, specific energy and specific capacity.Figs. 2(a)-2(b) , specifically including the following steps:
[0027] Step 1: Data reading
[0028] Use the pd.read_csv() statement to read the CSV format dataset file in the specified path and store the read data in the data variable.
[0029] At this point, data is a Pandas DataFrame object, whose rows represent different sample data and columns correspond to various attribute information of the battery (such as feature columns and target columns), which facilitates subsequent data operations and processing.
[0030] Step 2: Convert the target column to the target column:
[0031] Create an encoding object: Create a LabelEncoder object label_encoder, which will be used to convert the categorical data in the working_ion column (usually string type, representing different types of working ions) into numerical data.
[0032] Perform encoding operation: Use label_encoder.fit_transform(data['working_ion']) statement to perform encoding conversion.
[0033] Specifically, the fit_transform method is performed in two steps:
[0034] The first step is the fit phase. This method automatically counts the frequency of occurrence of different categories in the working_ion column and uses this information to build a coding mapping rule. For example, if the column contains different categories such as "Li+", "Na+", and "K+", it will determine the corresponding coding value for each category (for example, "Li+" corresponds to 0, "Na+" corresponds to 1, "K+" corresponds to 2, etc.)
[0035] The second step is the "transform phase." Following the encoding mapping rules established earlier, each string value in the working_ion column is replaced with its corresponding numeric encoding. The converted result is added to the data frame as a new column named working_ion_numeric.
[0036] This completes the encoding of the working_ion column, converting it into a numerical form that meets the input data requirements of subsequent models.
[0037] Extract feature variable X: Use data.drop(['working_ion','working_ion_numeric',
[0038] The feature variables are separated from the original data data by the statement X = data.drop(columns=['working_ion', 'working_ion_numeric', 'average_voltage', 'energy_grav', 'capacity_grav'], axis=1).
[0039] The columns working_ion (original string type working ion column), working_ion_numeric (just generated encoded working ion column), average_voltage (average voltage, one of the subsequent target variables), energy_grav (specific energy, one of the target variables), and capacity_grav (specific capacity, one of the target variables) are removed from the separated feature variable part.
[0040] The remaining columns constitute the feature variables X.
[0041] These feature variables contain various relevant attribute information of the battery, such as possible material composition, structural parameters, etc., which will be used as input data for subsequent prediction modeling of target variables.
[0042] Extract target variables: assign the data in the average_voltage column to y_voltage, the data in the energy_grav column to y_energy, and the data in the capacity_grav column to y_capacity, respectively. These three target variables represent key indicators of battery performance, i.e., the average voltage, specific energy, and specific capacity of the battery, which will be predicted by the feature variables X and the constructed model. They will serve as target variables for subsequent model training and evaluation.
[0043] Create and use encoding objects: for each string type column, such as material_type (material type, values such as Carbon, Graphite) and structure (structure, values such as Layered, Porous), create a new LabelEncoder object (le = LabelEncoder()), and then use the le.fit_transform(X[col]) statement to encode and convert the column data.
[0044] Similarly, the fit_transform method first constructs an encoding mapping rule according to the occurrence of different string values in the column, and then replaces the actual string values with the corresponding numerical encoding. After encoding the column, the encoded data is directly replaced with the corresponding column data in the original X, achieving encoding processing of all string type columns in the entire feature data, ensuring that all feature data are in numerical form.
[0045] The encoding principle is similar to that of the working_ion column. A mapping relationship from string category to numerical value is established to ensure that each different string category has a unique numerical encoding.
[0046] Step 3, Data Normalization
[0047] Step 3A, Create Normalization Object.
[0048] Four MinMaxScaler objects are created respectively, namely scaler_X for normalizing data in the feature variable X, scaler_y_voltage for normalizing the target variable y_voltage, scaler_y_energy for normalizing the target variable y_energy, and scaler_y_capacity for normalizing the target variable y_capacity.
[0049] MinMaxScaler is used to standardize the feature variable X, mapping each feature value to the [0, 1] interval.
[0050] The mathematical formula is:
[0051] where x is the original feature value, x min and x max are the minimum and maximum values of the feature in the entire data set, and x scaled is the standardized feature value.
[0052] Standardization helps improve the stability and convergence speed of model training, avoiding adverse effects of different features due to dimensional differences and other factors on model training.
[0053] Step 3B, Feature Data Normalization.
[0054] For feature data in the feature variable X, use the scaler_X.fit_transform(X) statement for normalization.
[0055] First, the fit part of the fit_transform method calculates the minimum value and maximum value (j represents the jth feature dimension, assuming there are p feature dimensions, then j = 1, 2,..., p) parameters according to all data points in X.
[0056] Then, the transform part will follow this rule to transform each data point x ij(i represents the i-th sample, i = 1, 2, …, n, n is the number of samples) is converted so that the characteristic value of the converted data falls in the interval [0, 1], and the calculation formula is The normalized feature data X_scaled is obtained.
[0057] Step 3C, target variable normalization.
[0058] Take the target variable y_voltage as an example. Since MinMaxScaler requires the input data format to be a two-dimensional array (shape (n_samples, 1), where n_samples is the number of samples)
[0059] First, y_voltage (originally in one-dimensional array form) is converted to two-dimensional array form by y_voltage.values.reshape(-1, 1),
[0060] Then use scaler_y_voltage.fit_transform(y_voltage.values.reshape(-1, 1)) to perform normalization operation, and get the normalized y_voltage_scaled.
[0061] The same operation is also applied to the target variables y_energy and y_capacity, respectively, to obtain y_energy_scaled and y_capacity_scaled, ensuring that all data participating in model training and prediction are in the appropriate numerical range and scale uniform.
[0062] Step 4, divide the training set, validation set and test set
[0063] Step 4A, use the train_test_split function.
[0064] Pass in the normalized feature data X_scaled, target variables y_voltage_scaled, y_energy_scaled, y_capacity_scaled, and specify test_size = 0.2 and random_state = 42.
[0065] test_size = 0.2 means that 20% of the overall data (all data after normalization) is divided into a test set, which is used to evaluate the performance of the model on unseen data;
[0066] random_state = 42 is a random seed parameter, setting this parameter can ensure that each time the code is run to divide the dataset, the division result is consistent and repeatable, which is convenient for comparing the results of different experiments.
[0067] Step 4B, use the train_test_split function.
[0068] This time, the features of the training set X_train, the target variables of the training set y_voltage_train, y_energy_train, and y_capacity_train are passed in.
[0069] Similarly, set test_size = 0.2 (i.e. take 20% of the training set as the validation set) and random_state = 42.
[0070] The purpose of this is to further divide a part of the training set as a validation set, which is used to periodically evaluate the performance of the model on the unseen training data subset during the model training process, so as to adjust the hyperparameters of the model in time, prevent the model from overfitting the training set data, etc.
[0071] Step 5, adjust the input data format
[0072] Step 5A, for training set feature data:
[0073] For training set feature data X_train, use the X_train.reshape(, 1,) statement to adjust it to the three-dimensional format required by the GRU model, that is, [batch_size, sequence_length, input_size].
[0074] Where batch_size is determined by X_train.shape[0], representing the number of training set samples;
[0075] sequence_length is set to 1 here, indicating that the time series length of the input data is 1 (if it is processing time series data with multiple time steps, the value will change accordingly);
[0076] input_size is determined by X_train.shape[1], representing the number of dimensions of input features at each time step, that is, the number of features of the feature data after the previous series of processing.
[0077] Step 5B, for test set feature data:
[0078] The same operation is applied to the test set feature data X_test, that is, it is converted to the [batch_size, sequence_length, input_size] format using the X_test.reshape(, 1,) statement, to ensure that the format of the test set data is consistent with the format of the training set data and meets the input requirements of the GRU model, so that it can be correctly input into the GRU model for subsequent training and prediction operations.
[0079] Step 6, define the GRU network
[0080] The GRU model as a whole contains a GRU layer (nn.GRU) and three fully connected layers (nn.Linear), which aims to receive preprocessed battery data features as input and then output the predicted values of the battery voltage, specific energy and specific capacity.
[0081] In the __init__ method of the GRU class, the initialization operation of each layer of the model and the related parameters is performed, as follows:
[0082] Step 6A, nn.GRU layer initialization
[0083] The self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True) statement is used to create the GRU layer.
[0084] The input_size parameter represents the number of feature dimensions of the input data, that is, the number of input features at each time step after the previous data preprocessing, for example, input_size in the [batch_size, sequence_length, input_size] format after the previous data is arranged.
[0085] The hidden_size parameter determines the dimension size of the hidden state vector in the GRU layer, which has an important influence on the complexity of the data patterns that the model can capture. From a mathematical point of view, the dimension of the hidden state vector at each time step is hidden_size.
[0086] The num_layers parameter represents the number of stacked layers of the GRU layer. Increasing the number of layers can make the model learn more complex nonlinear relationships, but it may also cause overfitting and other problems.
[0087] batch_first=True is a setting parameter that specifies the dimension order of the input data, considering the first dimension of the input data as the batch size, which is in line with common data organization habits, so the dimension order of the input data is [batch_size, sequence_length, input_size].
[0088] Step 6B, Fully Connected Layer Initialization
[0089] self.fc_voltage = nn.Linear(hidden_size, 1), self.fc_energy = nn.Linear(hidden_size, 1), and self.fc_capacity = nn.Linear(hidden_size, 1) are three statements that create fully connected layers for predicting voltage, specific energy, and specific capacity, respectively.
[0090]
[0091] Step 6C, Learning Rate Recording
[0092] self.lr = lr statement records the incoming learning rate parameter, which controls the step size of parameter updates during model training, and plays a key role in whether the model can converge and the convergence speed. A suitable learning rate can effectively update the model parameters in the direction of minimizing the loss function during training.
[0093] Step 6D, Forward Propagation
[0094] The forward method defines the forward propagation path of data in the model, that is, how data is calculated through each layer in turn and finally gets the output result.
[0095] The out, _ = self.gru(x) statement passes the input data x (whose dimension is [batch_size, sequence_length, input_size]) into the GRU layer for calculation, and gets two outputs.
[0096] The first output out is the hidden state sequence after processing by the GRU layer, with a dimension of [batch_size, sequence_length, hidden_size];
[0097] The second output (here ignored by _, which is usually the last layer's hidden state at each time step, and may be used in some specific scenarios).
[0098] In this process, according to the calculation principle of the GRU layer mentioned earlier, the hidden state is updated one by one at each time step, completing feature extraction and information transmission on the input sequence.
[0099] The output of the last time step is taken:
[0100] The out = out[:, -1, :] statement extracts the hidden state vector of the last time step from the hidden state sequence output by the GRU layer, and the dimension of out becomes [batch_size, hidden_size]. The reason for this is that it is generally believed that the hidden state of the last time step has integrated the information of the entire input sequence, and this is used as the input to the subsequent fully connected layer for final prediction.
[0101] Fully connected layer prediction output:
[0102] The three lines of code voltage_pred = self.fc_voltage(out), energy_pred = self.fc_energy(out), and capacity_pred = self.fc_capacity(out) pass the extracted hidden state vector out of the last time step into the corresponding fully connected layer.
[0103] After linear transformation by the fully connected layer, three prediction values are obtained: voltage prediction value voltage_pred, specific energy prediction value energy_pred, and specific capacity prediction value capacity_pred, all with dimensions [batch_size, 1], i.e., each sample corresponds to a predicted value. Finally, these three prediction values are returned as the output of the model, which are used for subsequent comparison with the true values, calculation of loss, and evaluation of model performance, etc.
[0104] Step 7, train the model.
[0105] Step 7A, train_model function - model training and validation evaluation
[0106] The train_model function is defined to train the model and return the average loss on the validation set. This function will be called multiple times during the Bayesian optimization process to evaluate the performance of the model under different parameter combinations.
[0107] Step 7B, define the loss function and optimizer
[0108] The criterion = nn.MSELoss() statement defines the loss function as Mean Squared Error Loss (MSE)
[0109] The mathematical formula is
[0110] where n is the number of samples, y i is the true value, is the predicted value. The root mean square error is the square root of the mean squared error, that is, It measures the average deviation between the predicted value and the true value, and the smaller the value, the more accurate the model prediction.
[0111] optimizer = torch.optim.Adam(model.parameters(), lr = lr) creates an Adam optimizer, which is a commonly used gradient-based optimization algorithm that combines adaptive learning rate adjustment and other advantages, which can more effectively update model parameters. model.parameters() represents obtaining all learnable parameters of the model (such as weight matrices, bias vectors, etc. in GRU layers and fully connected layers), and lr is the learning rate parameter passed in earlier, which controls the step size of each parameter update.
[0112] Step 7C, training loop and forward propagation, loss calculation
[0113] Set num_epochs = 1000, indicating that the model will perform 1000 complete training iteration processes, and one iteration through the entire training set is a training cycle (epoch).
[0114] In each training cycle, first set the model to training mode by model.train().
[0115] Then perform forward propagation, pass the tensor X_train_tensor of training set feature data into the model model, and get three predicted values, namely the voltage predicted value outputs_voltage, the specific energy predicted value outputs_energy and the specific capacity predicted value outputs_capacity, their dimensions are all [batch_size, 1] (assuming the batch size is batch_size).
[0116] Next, calculate the loss of each target variable according to the mean squared error loss function defined earlier, that is, loss_voltage = criterion(outputs_voltage, y_voltage_train_tensor) calculates the loss of voltage prediction, and loss_energy and loss_capacity are the same, which calculate the loss of specific energy and specific capacity prediction, respectively.
[0117] Finally, we add these three losses together to get the total loss loss = loss_voltage + loss_energy + loss_capacity, which is the performance measure of the model on the training set at the current training cycle, and the goal is to make it decrease during the training process.
[0118]
[0119] Step 7D, Backpropagation and Parameter Optimization
[0120] After calculating the loss, we need to perform backpropagation to calculate the gradient of the model parameters, so that we can update the parameters according to the gradient information to reduce the loss.
[0121] First, the optimizer.zero_grad() statement will clear the previously accumulated gradient information in the optimizer, because by default, PyTorch will accumulate gradients, and we need to clear them before each backpropagation to avoid repeated calculations and errors.
[0122] Then, loss.backward() performs the backpropagation operation, automatically calculating the gradient of the loss with respect to each parameter of the model according to the chain rule, and these gradient information will be stored in the corresponding parameters.
[0123] Finally, optimizer.step() updates the model parameters according to the calculated gradient and the update rule of the optimizer (here it is the Adam optimizer), adjusting the parameters to move in the direction that reduces the loss function, completing a parameter optimization process.
[0124] Step 7E, Model Evaluation on Validation Set
[0125] After completing a complete training cycle (1000 iterations), we need to evaluate the model's performance on the validation set to determine whether the model has overfitting, etc., and the generalization ability of the model under the current parameter setting.
[0126] First, set the model to evaluation mode by model.eval(), so that the model is in a stable evaluation state.
[0127] Similarly, we need to convert the feature data X_val of the validation set to a tensor and transfer it to the corresponding device (X_val_tensor = torch.Tensor(X_val).to(device)).
[0128] Then pass it into the model for forward propagation to get the predicted values outputs_voltage, outputs_energy, and outputs_capacity on the validation set.
[0129] Next, evaluate for each target variable.
[0130] Take voltage prediction as an example, since the target variable has been normalized before, we need to first inverse the normalization of the predicted value and the true value of the validation set through the inverse_transform method of the corresponding MinMaxScaler object, restore it to the original data range, and get y_voltage_val_pred (predicted value) and y_voltage_val_original (true value).
[0131] Then use the mean_squared_error function (the mathematical formula of which has been introduced before to calculate the mean squared error) to calculate the mean squared error loss of voltage prediction on the validation set loss_voltage_val, and the same operation process is used for the evaluation of specific energy and specific capacity prediction, respectively loss_energy_val and loss_capacity_val.
[0132] Step 7F, return the average loss:
[0133] Finally, add the mean squared error loss of voltage, specific energy and specific capacity prediction on the validation set and take the average as the return value of the function, which represents the comprehensive performance of the model on the validation set.
[0134] In the Bayesian optimization process, the average loss will be used to judge the pros and cons of the model under different parameter combinations, and then find the optimal parameter combination.
[0135] Step 8: Bayesian optimization parameter search.
[0136] Bayesian optimization is a method for finding the optimal value of a function, which is used in this code to find the optimal hyperparameter combination (hidden layer size, number of layers and learning rate) of the GRU model to minimize the average loss on the validation set.
[0137] Step 8A, define the parameter space
[0138] The parameter space of Bayesian optimization is defined through the pbounds dictionary, where the search range of the hidden_size parameter is set to (32, 256), meaning that Bayesian optimization will try different hidden layer size values within this interval; The search range of num_layers is (1, 3), that is, try different layer combinations of 1 to 3 layers; The search range of lr (learning rate) is (0.0001, 0.01), which will explore the appropriate learning rate value within this interval
[0139] Step 8B, create a Bayesian optimization object:
[0140] A BayesianOptimization object optimizer is created, passing in several key parameters.
[0141] The f parameter specifies the objective function to be optimized, which here uses an anonymous function (lambda function) that actually calls the previously defined train_model function and passes in the corresponding parameters, including the training and validation set data and the parameters to be optimized (hidden layer size, number of layers, learning rate, etc.), so that the Bayesian optimization process will evaluate the performance of the model on the validation set (measured by the returned average loss) under different parameter combinations by calling this objective function.
[0142] pbounds is the previously defined parameter space, which tells the Bayesian optimization algorithm to search for parameters within which range.
[0143] random_state = 42 sets the random seed, ensuring that each time the Bayesian optimization process is run, the initial random exploration and other operations have reproducibility, making it easier to compare the results of different experiments.
[0144] Step 8C, perform Bayesian optimization iterative search
[0145] The iterative search process of Bayesian optimization is performed through the optimizer.maximize method.
[0146] init_points = 5 means that before starting the formal iterative optimization, 5 random explorations are performed, that is, 5 different parameter combinations (hidden layer (fully connected layer) size, number of layers, learning rate) are randomly selected in the parameter space, and the train_model function is called to evaluate the average loss of the model on the validation set under these random parameters, to preliminarily understand the situation of the parameter space and provide a reference for subsequent optimization.
[0147] n_iter = 10 specifies that the number of formal iterative optimization is 10 times, after the initial 5 random explorations, the Bayesian optimization algorithm will use the existing evaluation results (different parameter combinations corresponding to the average loss of the validation set) to infer the next parameter combination that may make the average loss smaller, then use the Bayesian theorem to evaluate, and repeatedly this process, after 10 iterations, find a relatively optimal parameter combination, so that the average loss of the model on the validation set is as small as possible.
[0148] Step 8C, get the optimized best parameters
[0149] After the Bayesian optimization is completed, the best parameter combination found needs to be obtained in order to use these best parameters to retrain the final model later.
[0150] The best parameter combination found in the Bayesian optimization process is obtained in dictionary form through best_params = optimizer.max['params'], and then the best values corresponding to the hidden layer size, number of layers, and learning rate are extracted from it respectively, and converted to appropriate data types (such as converting the hidden layer size and number of layers to integer types), and stored in the best_hidden_size, best_num_layers, and best_lr variables. These best parameters will be used to initialize the final model for subsequent complete training and testing operations, etc.
[0151] Step 8C, initialize the model with the best parameters
[0152] According to the obtained best parameters, the GRU model is reinitialized to ensure that the model structure is built according to the optimized best parameters.
[0153] That is, using the previously defined GRU class, the best input_size (usually determined by the feature dimension of the data itself and already clear in the early stage), best_hidden_size, best_num_layers, and best_lr are passed in to instantiate the model.
[0154] Step 9, redefine the loss function and optimizer.
[0155] Because a new round of complete model training is to be performed, the loss function and optimizer need to be redefined.
[0156] Loss function definition:
[0157] The criterion = nn.MSELoss() statement defines the loss function as the Mean Squared Error Loss (MSE).
[0158] The criterion object is created by instantiating the nn.MSELoss() class, which will be used later when calculating the loss of each training batch.
[0159] Optimizer definition:
[0160] optimizer = torch.optim.Adam(final_model.parameters(), lr = best_lr) creates an Adam optimizer.
[0161] The Adam optimizer combines the advantages of adaptive learning rate adjustment, which dynamically adjusts the learning rate of each parameter according to the gradient of each parameter and historical gradient information, and then updates the parameters according to the appropriate step size, so that the parameters move in the direction that reduces the loss function.
[0162] The final_model.parameters() indicates that all learnable parameters in the final_model GRU model are obtained (such as weight matrices, bias vectors in the GRU layer, and corresponding parameters in the fully connected layer, etc.), and the optimizer will perform gradient calculation and update operations on these parameters.
[0163] lr = best_lr specifies the learning rate as the best learning rate value obtained by the previous Bayesian optimization, so that the optimizer can update the model parameters with the relatively optimal step size, improving the efficiency and effectiveness of model training.
[0164] In order to be able to perform subsequent model training-related calculations in PyTorch, the feature data X_train of the complete training set and the corresponding target variables (voltage, specific energy, specific capacity)
[0165] y_voltage_train, y_energy_train, and y_capacity_train are converted to torch.Tensor type.
[0166] Step 10, model training loop based on complete training set.
[0167] Set the training round to 600 times, defined by num_epochs = 600, and then enter the training loop. In each training cycle (epoch):
[0168] First, set the model to training mode by final_model.train(), enable specific operations such as Dropout required during training, and inform the model that it is in the training stage, so that gradient calculation and parameter update operations can be performed correctly.
[0169] Second, perform forward propagation by passing the tensor X_train_tensor of the training set feature data into final_model to obtain the predicted values outputs_voltage, outputs_energy, and outputs_capacity of the three target variables, with dimensions [batch_size, 1], representing the prediction results for each sample. During this process, data is extracted and information is transmitted in the GRU layer, and the predicted values are output after linear transformation by the fully connected layer.
[0170] Third step, calculate the loss according to the predicted value and the true value.
[0171] The loss of voltage, specific energy and specific capacity prediction is calculated using mean square error loss function criterion, i.e. loss_voltage = criterion(outputs_voltage, y_voltage_train_tensor), loss_energy = criterion(outputs_energy, y_energy_train_tensor) and loss_capacity = criterion(outputs_capacity, y_capacity_train_tensor),
[0172]
[0173] The three losses are added to get the total loss loss = loss_voltage + loss_energy + loss_capacity.
[0174]
[0175] Fourth step, perform back propagation and parameter update operation.
[0176] Clear the previously accumulated gradient information in the optimizer through optimizer.zero_grad() to avoid the influence of error accumulation of gradient on parameter update.
[0177] Perform loss.backward() to perform back propagation, automatically calculate the gradient of the loss with respect to each learnable parameter in the model according to the chain rule, and store the gradient information in the corresponding parameter attribute.
[0178] Through optimizer.step(), update the model parameters according to the update rule of Adam optimizer, combine the calculated gradient and the set best learning rate best_lr, and move the parameters to the direction that makes the loss function decrease, gradually optimize the model performance.
[0179] Step 11, test set data conversion and model performance evaluation.
[0180] First step, convert the feature data X_test of the test set into torch.Tensor type and transfer it to the corresponding device (according to torch.cuda.is_available()), through X_test_tensor = torch.tensor(X_test, dtype=torch.float32).to(device) to prepare for model prediction on the test set.
[0181]
[0182] Second step, set the model to evaluation mode by final_model.eval(), turn off specific operations enabled during training such as Dropout, to ensure the stability and reliability of the model output results.
[0183] Third step, in the with torch.no_grad(): context environment (no gradient calculation, because the test phase does not need to update the parameters), the tensor X_test_tensor of the test set feature data is passed into the model for forward propagation, to get the prediction values outputs_voltage, outputs_energy and outputs_capacity on the test set.
[0184] Evaluate the prediction results for each target variable:
[0185] Voltage prediction evaluation: Since the target variable data has been normalized before, first perform inverse normalization operation on the predicted value and the true value of the test set,
[0186] y_voltage_pred = scaler_y_voltage.inverse_transform(outputs_voltage.cpu().numpy()) and y_voltage_test_original = scaler_y_voltage.inverse_transform(y_voltage_test) to restore the voltage prediction value and the true value to the original data range.
[0187]
[0188] Use the mean_absolute_error function to calculate the mean absolute error (MAE), and use the r2_score function to calculate the determination coefficient (R 2 ), respectively, to get the three evaluation index values mse_voltage, mae_voltage and r2_voltage, to measure the accuracy and fitting degree of the model for voltage prediction from different angles.
[0189] Specific energy prediction evaluation, similar to voltage prediction evaluation.
[0190] Through y_energy_pred = scaler_y_energy.inverse_transform(outputs_energy.cpu().numpy()) and y_energy_test_original = scaler_y_energy.inverse_transform(y_energy_test), the specific energy prediction value and the true value are restored to the original data range.
[0191] scaler_y_energy.inverse_transform(outputs_energy.cpu().numpy()) and y_energy_test_original=scaler_y_energy.inverse_transform(y_energy_test) perform an inverse normalization operation.
[0192] After obtaining the restored predicted value and true value, the corresponding evaluation index functions are used to calculate the mean square error mse_energy, mean absolute error mae_energy and determination coefficient r2_energy to evaluate the performance of the model in energy prediction.
[0193] Specific capacity prediction evaluation: The same operation is applied to the specific capacity prediction evaluation
[0194] y_capacity_pred=
[0195] scaler_y_capacity.inverse_transform(outputs_capacity.cpu().numpy()) and y_capacity_test_original = scaler_y_capacity.inverse_transform(y_capacity_test) are denormalized, and then the mean square error mse_capacity, mean absolute error mae_capacity and coefficient of determination r2_capacity are calculated to measure the performance of the model for comparative capacity prediction.
[0196] Step 12: Output the test results.
[0197] The print function is used to output the calculated evaluation index values in a certain format, using the formatted string (f-string) method, such as print(f'Test Voltage MSE:
[0198] {mse_voltage:.4f}'), print(f'Test Voltage MAE:{mae_voltage:.4f}') and print(f'Test Voltage R 2 :{r2_voltage:.4f}'), etc., output the mean square error, mean absolute error and determination coefficient of voltage, specific energy and specific capacity prediction respectively.
[0199] These output results intuitively show the prediction performance of the model on the test set, including the error size (reflected by the mean square error and the mean absolute error) and the fitting degree of the model to the data (reflected by the determination coefficient), providing a quantitative basis for evaluating the actual effect of the model, and further judging whether the model meets the requirements of the actual application scene for the accuracy and reliability of battery performance prediction.
[0200] In summary, the present application proposes an innovative prediction method.
[0201] First, the data is preprocessed, including encoding conversion of the "working_ion" column and other string feature columns, division of features and target variables, normalization of features and target variables, division of training set, validation set and test set, and adjustment of input data format.
[0202] Then define the GRU model structure, including GRU layer and three fully connected layers for predicting different performance indicators. Through the definition of "train_model" function, the model training and validation evaluation are realized. In the training process, the mean square error loss and Adam optimizer are used, the model parameters are updated multiple times, and the mean square error loss of each target variable prediction is calculated on the validation set to evaluate the model performance.
[0203] On this basis, Bayesian optimization is used to optimize the search of the model's hidden layer size, number of layers, learning rate and other hyperparameters. First, initial random exploration is performed, and then based on the existing evaluation results, the best parameter combination that minimizes the average loss of the validation set is iteratively searched.
[0204] Finally, the model is retrained using the best parameters, and the model performance is evaluated on the test set. The mean square error, mean absolute error and determination coefficient of voltage, specific energy and specific capacity prediction are calculated to verify the effectiveness and accuracy of the model, providing a systematic and efficient method for battery performance prediction.
Claims
1. A method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU, characterized in that: The following steps are involved: Define and train a GRU network model whose input is battery attribute information and whose output is battery voltage, specific energy, and specific capacity; Use Bayesian optimization to find the optimal parameter combination of the GRU network model; Retrain the GRU network model based on the optimal parameter combination; Use the trained GRU network model to predict battery voltage, specific energy and specific capacity.
2. The method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU according to claim 1, characterized in that: The GRU network model includes: a GRU layer and three fully connected layers.
3. The method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU according to claim 1, characterized in that: The specific steps for using Bayesian optimization to find the optimal parameter combination for the GRU network model include: The mean square error losses of voltage, specific energy, and specific capacity predictions on the validation set are summed and the average is taken as the average loss. Bayesian optimization is used to find the optimal parameter combination based on the average loss. The optimal parameter combination corresponds to the smallest average loss.
4. The method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU according to claim 1, characterized in that: The parameters in the parameter combination include: the size of the fully connected layer, the number of fully connected layers, and the learning rate.
5. The method for predicting battery voltage, specific energy and specific capacity based on Bayesian optimization-GRU according to claim 1, characterized in that: Battery property information includes: material composition and structural parameters.