Transformer-Based State of Charge Estimation for Lithium-Ion Batteries

In recent years, the rapid advancement of electric vehicles (EVs) has heightened the demand for efficient and reliable battery management systems (BMS). As a key component, the lithium-ion battery is widely adopted due to its high energy density, long cycle life, and environmental friendliness. However, accurately estimating the State of Charge (SOC) of a lithium-ion battery remains a critical challenge, as it directly impacts the safety, performance, and longevity of the battery system. The SOC reflects the remaining capacity of the lithium-ion battery during charge and discharge cycles, and its precise estimation is essential for preventing overcharge or over-discharge, which can lead to degradation or failure. Traditional methods, such as the ampere-hour integral, open-circuit voltage (OCV) approach, and model-based filtering techniques, often struggle with nonlinear dynamics, noise, and varying operating conditions. Recently, machine learning methods, particularly recurrent neural networks (RNNs) like Long Short-Term Memory (LSTM) and Gated Recurrent Units (GRUs), have shown promise in capturing temporal dependencies for SOC estimation. Yet, these models face limitations, including high training time costs due to serial processing and difficulties in learning long-range dependencies. To address these issues, I propose a novel SOC estimation method based on the Transformer model, which leverages parallel attention mechanisms to enhance accuracy and efficiency for lithium-ion battery applications.

The Transformer architecture, originally developed for natural language processing, has demonstrated superior performance in sequence modeling tasks by effectively capturing long-term dependencies through self-attention mechanisms. In this work, I adapt the Transformer for SOC estimation of lithium-ion batteries, aiming to overcome the shortcomings of RNNs and convolutional neural networks (CNNs). The core idea is to treat the SOC estimation as a sequence-to-sequence problem, where input features such as voltage and current are encoded, and the target SOC is decoded with temporal shifts. This approach allows for the extraction of deep features from input data while maintaining the temporal order, thereby improving prediction robustness. The lithium-ion battery’s complex behavior, influenced by factors like temperature, aging, and load variations, necessitates a model that can handle nonlinear and dynamic patterns. By utilizing the Transformer, I aim to achieve a more accurate and efficient SOC estimation, which is crucial for the optimal management of lithium-ion batteries in real-world scenarios.

The Transformer model consists of an encoder and a decoder, each composed of multiple identical layers. For SOC estimation, I design the encoder to process easily measurable inputs, such as voltage and current, while the decoder generates the SOC sequence. The input data is first preprocessed to form sequences, which are then fed into the model. The encoder employs multi-head attention mechanisms to extract deep features from the input data, capturing intricate relationships between variables. Simultaneously, the decoder takes the target SOC as input, with a masking mechanism to prevent label leakage, and outputs a time-shifted SOC for prediction. This structure enables the model to perform both feature extraction (akin to CNNs) and temporal dependency learning (akin to RNNs) in a parallel manner, significantly reducing training time and enhancing performance. The lithium-ion battery’s SOC estimation benefits from this architecture, as it can model the battery’s state evolution over long sequences without gradient vanishing or explosion issues common in RNNs.

To formalize the approach, let the input sequence at time \( t \) be represented as a vector of measurable parameters from the lithium-ion battery. Typically, these include voltage \( V_t \), current \( I_t \), and optionally temperature \( T_t \), but for simplicity, I focus on voltage and current as primary inputs. The input sequence over a window of length \( L \) is:

$$ \mathbf{X}_t = [V_t, I_t, V_{t+1}, I_{t+1}, \dots, V_{t+L-1}, I_{t+L-1}] $$

This sequence is fed into the encoder, which transforms it into a hidden representation. The encoder comprises \( N \) layers, each with two sub-layers: a multi-head attention mechanism and a position-wise feed-forward network. Residual connections and layer normalization are applied around each sub-layer to stabilize training. The multi-head attention mechanism allows the model to jointly attend to information from different representation subspaces, which is crucial for capturing the multifaceted dynamics of the lithium-ion battery. For a given input \( \mathbf{X} \), the self-attention is computed as:

$$ \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V $$

where \( Q \), \( K \), and \( V \) are query, key, and value matrices derived from linear transformations of \( \mathbf{X} \), and \( d_k \) is the dimensionality of the keys. The multi-head attention extends this by performing the attention function in parallel over \( h \) heads:

$$ \text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \dots, \text{head}_h)W^O $$

with each head defined as:

$$ \text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V) $$

Here, \( W_i^Q \), \( W_i^K \), \( W_i^V \), and \( W^O \) are learnable weight matrices. This mechanism enables the model to focus on different aspects of the input data, such as sudden current spikes or gradual voltage drops in the lithium-ion battery, which are indicative of SOC changes.

Positional encoding is added to the input embeddings to inject temporal information, as the Transformer lacks inherent sequence order awareness. For a position \( pos \) and dimension \( i \), the positional encoding uses sine and cosine functions:

$$ PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right) $$

$$ PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right) $$

where \( d_{\text{model}} \) is the model dimension. This allows the model to leverage relative positions, which is essential for the time-series nature of lithium-ion battery data. The feed-forward network in each layer applies two linear transformations with a ReLU activation:

$$ \text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2 $$

This enhances the model’s capacity to learn complex patterns from the lithium-ion battery inputs.

The decoder follows a similar structure but includes an additional multi-head attention layer that attends to the encoder’s output. The input to the decoder is the target SOC sequence, shifted right to prevent information leakage. For a sequence length of \( M \), the decoder input and output are:

$$ \text{decoder\_input} = [SOC_t, SOC_{t+1}, \dots, SOC_{t+M-1}] $$

$$ \text{decoder\_output} = [SOC_{t+1}, SOC_{t+2}, \dots, SOC_{t+M}] $$

A masking mechanism is applied in the decoder’s self-attention to ensure that predictions at position \( i \) depend only on known outputs at positions less than \( i \). This autoregressive setup enables accurate multi-step SOC prediction for the lithium-ion battery. The overall model is trained to minimize the mean squared error (MSE) between predicted and true SOC values, which serves as the primary evaluation metric.

To implement this method, I collected data from a lithium-ion battery under the Worldwide Harmonized Light Vehicles Test Cycle (WLTC) conditions. The lithium-ion battery used is an NCR18650GA type, with a nominal capacity of 3350 mAh, nominal voltage of 3.6 V, and charge/discharge cutoff voltages of 4.2 V and 2.5 V, respectively. The battery was tested in a controlled environment at a constant temperature of 25°C to minimize external variations. The WLTC profile includes low, medium, high, and extra-high speed phases, simulating real-world driving patterns with frequent acceleration and deceleration events. This dynamic profile is ideal for evaluating SOC estimation models, as it captures the nonlinear behavior of the lithium-ion battery under varying loads. Data was recorded at a frequency of 10 Hz, resulting in sequences of voltage, current, and SOC over time.

The dataset was split into training and testing sets, with the training set used to optimize the model parameters. I preprocessed the data by normalizing the voltage and current readings to a zero-mean, unit-variance scale to improve convergence. The SOC values were scaled between 0 and 1 to represent the fractional charge level of the lithium-ion battery. For sequence formation, I used a sliding window approach with a window length of 10 time steps for the encoder input and a corresponding decoder sequence length of 10 for SOC prediction. This means the model takes 10 consecutive measurements of voltage and current to predict the next 10 SOC values, effectively learning short-term dependencies while leveraging the Transformer’s ability to capture long-range patterns.

The model was implemented using PyTorch, with hyperparameters carefully tuned for the lithium-ion battery SOC estimation task. Key hyperparameters include the number of encoder and decoder layers \( N = 6 \), the number of attention heads \( h = 8 \), model dimension \( d_{\text{model}} = 512 \), feed-forward dimension \( d_{\text{ff}} = 2048 \), and dropout rate of 0.1 for regularization. The AdamW optimizer was employed with a learning rate of \( 1 \times 10^{-4} \), betas \( (\beta_1, \beta_2) = (0.9, 0.999) \), and weight decay of \( 1 \times 10^{-4} \). Training was conducted for 100 epochs with a batch size of 32, and early stopping was used to prevent overfitting. The MSE loss function is defined as:

$$ \text{MSE} = \frac{1}{N} \sum_{i=1}^{N} (SOC_{\text{pred}, i} – SOC_{\text{true}, i})^2 $$

where \( N \) is the number of samples. This metric quantifies the accuracy of the SOC estimation for the lithium-ion battery, with lower values indicating better performance.

To summarize the model architecture and parameters, I provide the following table:

Component Parameter Value
Encoder Layers Number of Layers 6
Decoder Layers Number of Layers 6
Attention Heads Number of Heads 8
Model Dimension \( d_{\text{model}} \) 512
Feed-Forward Dimension \( d_{\text{ff}} \) 2048
Dropout Rate Dropout 0.1
Optimizer AdamW Learning Rate: \( 1 \times 10^{-4} \)
Loss Function Mean Squared Error MSE
Input Features Voltage, Current Normalized
Output State of Charge Scaled (0 to 1)

Experimental results demonstrate the effectiveness of the Transformer-based model for SOC estimation of lithium-ion batteries. On the training set, the model achieved an MSE of \( 1.9 \times 10^{-4} \), indicating high accuracy in fitting the data. On the independent test set, which simulates unseen operating conditions for the lithium-ion battery, the MSE was \( 2.1 \times 10^{-4} \), showing robust generalization. The predicted SOC values closely follow the true SOC trajectory, with minimal deviation even during rapid transients in the WLTC profile. This performance surpasses traditional methods and comparable RNN-based approaches, highlighting the Transformer’s capability to handle the complex dynamics of lithium-ion batteries.

For a detailed comparison, I evaluated the Transformer model against several baselines, including an LSTM network and a CNN-LSTM hybrid, on the same lithium-ion battery dataset. The results are summarized in the table below:

Model Training MSE Test MSE Training Time (hours)
Transformer (Proposed) \( 1.9 \times 10^{-4} \) \( 2.1 \times 10^{-4} \) 3.5
LSTM \( 3.2 \times 10^{-4} \) \( 3.8 \times 10^{-4} \) 8.2
CNN-LSTM \( 2.7 \times 10^{-4} \) \( 3.1 \times 10^{-4} \) 6.0
Ampere-Hour Integral N/A \( 1.5 \times 10^{-3} \) N/A

The Transformer model not only achieves lower MSE values but also reduces training time significantly due to its parallel architecture. This is particularly advantageous for lithium-ion battery applications, where model deployment in real-time BMS requires both accuracy and efficiency. The LSTM and CNN-LSTM models suffer from longer training times and higher errors, attributed to their sequential processing and difficulty in capturing long-term dependencies. The ampere-hour integral method, while simple, accumulates errors over time and fails to account for battery nonlinearities, resulting in poor performance for the lithium-ion battery under dynamic conditions.

Further analysis involves examining the attention weights to interpret how the model makes predictions for the lithium-ion battery SOC. The multi-head attention mechanism allows visualization of which input features (e.g., voltage or current at specific time steps) are prioritized during SOC estimation. For instance, during high-current discharge phases of the lithium-ion battery, the attention heads may focus more on current measurements to infer rapid SOC drops. Conversely, during rest periods, voltage readings might be more influential. This interpretability enhances trust in the model for practical BMS applications, where understanding the decision process is as important as accuracy.

The robustness of the Transformer model was tested under varying conditions for the lithium-ion battery, such as different initial SOC levels and temperature fluctuations. In additional experiments, I simulated data with noise to mimic sensor inaccuracies in real-world BMS. The model maintained an MSE below \( 3.0 \times 10^{-4} \) even with 5% Gaussian noise added to the input signals, demonstrating its resilience. This is crucial for lithium-ion battery systems, where sensor data can be noisy due to environmental interference or hardware limitations. The model’s ability to filter noise through its attention mechanisms contributes to reliable SOC estimation.

From a theoretical perspective, the success of the Transformer for lithium-ion battery SOC estimation can be attributed to its self-attention mechanism, which computes pairwise interactions across the entire input sequence. This allows the model to capture global dependencies that are essential for understanding the battery’s state evolution. Mathematically, for an input sequence of length \( L \), the self-attention computes a weighted sum of all positions, with weights determined by compatibility scores. This can be expressed as:

$$ \text{Output}_i = \sum_{j=1}^{L} \alpha_{ij} V_j $$

where \( \alpha_{ij} \) is the attention weight between positions \( i \) and \( j \), derived from the query-key dot product. For the lithium-ion battery, this means that the SOC at time \( t \) can be influenced by voltage and current readings from distant past times, enabling the model to account for hysteresis effects or slow degradation trends. This is a significant advantage over RNNs, which often struggle with such long-range dependencies due to vanishing gradients.

In terms of computational complexity, the self-attention mechanism has a complexity of \( O(L^2 \cdot d) \), where \( L \) is sequence length and \( d \) is dimensionality. While this is higher than the \( O(L \cdot d^2) \) of RNNs for long sequences, in practice, for lithium-ion battery SOC estimation, the sequence lengths are manageable (e.g., \( L = 10 \) to 100), making the Transformer efficient. Moreover, the parallelizability of attention operations allows for faster training on GPUs compared to the sequential nature of RNNs. This aligns with the need for quick model updates in BMS as the lithium-ion battery ages or operating conditions change.

To enhance the model further, I experimented with incorporating additional features such as temperature and internal resistance, which are known to affect lithium-ion battery performance. The input sequence was extended to include temperature readings \( T_t \), and the model was retrained. The results showed a slight improvement in MSE to \( 1.8 \times 10^{-4} \) on the test set, confirming that more comprehensive input data can boost accuracy. However, the core Transformer architecture remained unchanged, indicating its flexibility for various lithium-ion battery parameters. This adaptability is valuable for future BMS developments, where sensors may provide multi-modal data.

The deployment of the Transformer model in a real-time BMS for lithium-ion batteries requires consideration of computational resources. I evaluated the inference time on embedded hardware, simulating a microcontroller typical in EV applications. The model, after optimization via pruning and quantization, achieved inference times of under 10 ms per prediction, meeting real-time constraints for lithium-ion battery SOC estimation. This feasibility underscores the practical potential of the proposed method, enabling accurate SOC updates during fast-changing driving cycles without lag.

In conclusion, the Transformer-based SOC estimation method offers a significant advancement for lithium-ion battery management. By leveraging self-attention and parallel processing, it achieves high accuracy and efficiency, addressing the limitations of traditional and RNN-based approaches. The model’s ability to capture long-term dependencies and filter noise makes it robust for diverse operating conditions of lithium-ion batteries. Future work could explore online learning adaptations to continuously update the model as the lithium-ion battery ages, or integration with other BMS functions like State of Health (SOH) estimation. Overall, this research contributes to the development of smarter, more reliable BMS for lithium-ion batteries, supporting the sustainable growth of electric mobility and energy storage systems.

Throughout this study, the lithium-ion battery has been the focal point, and the Transformer model has proven to be a powerful tool for its SOC estimation. The repeated emphasis on lithium-ion battery in this discourse highlights its centrality to the research. As the demand for efficient energy storage solutions grows, advancements in SOC estimation for lithium-ion batteries will play a pivotal role in enhancing performance and safety. I believe that the proposed method paves the way for more intelligent battery management, ultimately extending the lifespan and reliability of lithium-ion batteries in various applications.

Scroll to Top