Main Content

Train Deep Learning Network for Battery State of Charge Estimation

Since R2024b

This example shows how to train a deep neural network to predict battery state of charge (SOC).

SOC is the level of charge of an electric battery relative to its capacity, measured as a percentage. This example shows how to train a deep learning network to predict battery state of charge given the temperature, voltage, and current.

This example is step three in a series of examples that take you through a battery state of charge estimation workflow. You can run each step independently or work through the steps in order. This example follows the Prepare Data for Battery State of Charge Estimation Using Deep Learning example. For more information about the full workflow, see Battery State of Charge Estimation Using Deep Learning.

Load the test data. If you have run the previous step, then the example uses the data that you prepared in the previous step. Otherwise, the example prepares the data as shown in Prepare Data for Battery State of Charge Estimation Using Deep Learning.

XTrain and XVal are the training and validation inputs, where each data set is a cell array containing the temperature, voltage, and current across 500 time steps. YTrain and YVal are the training and validation outputs, where each data set is a cell array containing the SOC across 500 time steps.

if ~exist("XTrain","var") || ~exist("YTrain","var") || ~exist("XVal","var") || ~exist("YVal","var")
    [XTrain,YTrain,XVal,YVal] = prepareBSOCData;
end

Define Network Architecture

Define the network architecture. Set the number of input features to three (temperature, voltage, and current).

numFeatures = 3;

Set the number of output features to one (SOC).

numResponses = 1;

An LSTM neural network is a type of recurrent neural network (RNN) that can learn long-term dependencies between time steps of sequence data. In this example, use an LSTM network architecture with two blocks that each consist of an LSTM layer followed by a dropout layer. The dropout layers help to avoid overfitting. To interactively build and visualize the network, use the Deep Network Designer app.

layers = [...
    sequenceInputLayer(numFeatures)
    lstmLayer(256,OutputMode="sequence")
    dropoutLayer(0.2)
    lstmLayer(128,OutputMode="sequence")
    dropoutLayer(0.2)
    fullyConnectedLayer(numResponses)
    sigmoidLayer];

Specify Training Options

Specify the training options. Choosing among the options requires empirical analysis. To explore different training option configurations by running experiments, you can use the Experiment Manager app.

  • Train using Adam optimization.

  • Train with an initial learning rate of 0.01.

  • Train for 150 epochs.

  • Train with a mini-batch size of 32.

  • In each mini-batch, left-pad the sequences so they have the same length. Left-padding prevents the RNN from predicting padding values at the ends of sequences.

  • Shuffle the data every epoch.

  • Monitor performance using validation data.

  • Display the training progress in a plot.

  • Track the RMSE value.

  • Disable the verbose output.

options = trainingOptions("adam", ...
    InitialLearnRate=0.01, ...
    MaxEpochs=150, ...
    MiniBatchSize=32, ...
    SequencePaddingDirection="left", ...
    Shuffle="every-epoch",...
    ValidationData={XVal,YVal}, ...
    ValidationFrequency=20, ...
    Plots="training-progress", ...
    Metrics="rmse", ...
    Verbose=false);

Train Network

Train the LSTM neural network using the trainnet function. For sequence prediction tasks, use mean squared error (MSE) loss. By default, the trainnet function uses a GPU if one is available. Using a GPU requires a Parallel Computing Toolbox™ license and a supported GPU device. For information on supported devices, see GPU Computing Requirements (Parallel Computing Toolbox). Otherwise, the function uses the CPU. To specify the execution environment, use the ExecutionEnvironment training option.

To train the network, set the doTraining flag to true. Otherwise, load a pretrained network.

doTraining = false;

if doTraining
    recurrentNet = trainnet(XTrain,YTrain,layers,"mse",options);
else
    load("pretrainedBSOCNetwork.mat")
end

The trained network is now ready to use for prediction. To test the network, see Train Deep Learning Network for Battery State of Charge Estimation. Alternatively, to first compress your network to decrease its size, see Compress Deep Learning Network for Battery State of Charge Estimation. You can also open the next example using the openExample function.

openExample("deeplearning_shared/CompressModelForBatteryStateOfChargeEstimationExample")
openExample("deeplearning_shared/TestModelForBatteryStateOfChargeEstimationExample")

Optionally, if you have Requirements Toolbox™, then in the next section, you can link and test network architecture requirements.

Link Network Architecture Requirements Using Requirements Toolbox

This section links the AI component requirements to the network and requires Requirements Toolbox™ and MATLAB Test™. This section does not show how to create or link requirements, only how to implement and verify the links. For more information about defining these requirements, see Define Requirements for Battery State of Charge Estimation. For general information about how to create and manage requirements, see Use Requirements to Develop and Verify MATLAB Functions.

Linking requirements is important to ensure you can trace your network requirements to the implementation and verify them through testing.

Check for a Requirements Toolbox™ license.

if ~license("test","simulink_requirements")    
    disp("This part of the example requires Requirements Toolbox.");    
    return
end

Save the trained network.

save("recurrentNetworkTest","recurrentNet");

In this example, the network must take as input a 1-by-3 array and must output a single array (SOC).

Open the network architecture requirements. To add columns that indicate the implementation and verification status of the requirements, click Columns and then select Implementation Status and Verification Status. If you see a yellow banner, then click Analyze now. You can see each of the requirements and their implementation and verification status. The verification status for each requirement is yellow, indicating that the status is unknown. The status turns to red if the requirement fails or green if the requirement passes.

open("testNetworkRequirements.m")
slreq.open("BatterySOCReqNetArch.slreqx");

Select one of the network requirements. Each requirement is implemented by NetworkInputOutputJustification and verified by a test.

Implement Requirements

By creating a network with the right input size and output size, you have implemented the requirements. You can see this justification by selecting NetworkInputOutputJustification in the requirements editor.

Verify Requirements

The next step is to verify the requirements. To verify requirements, create tests to check the network architecture. You can find the network architecture tests in the testNetworkRequirements file, attached to this example as a supporting file. The file contains two tests:

  • testNetworkInput — Test that the model accepts a 1-by-3 array as input.

  • testNetworkOutput — Test that the model returns a single output.

        function testNetworkInput(testCase)
            net = testCase.recurrentNetwork;
           
            inputSize = net.Layers(1).InputSize;
            verifyEqual(testCase,inputSize,3);
        end

        function testNetworkOutput(testCase)
            net = testCase.recurrentNetwork;

            outputSize = size(forward(net,dlarray([0.5 0.5 0.5],"TC")));
            verifyEqual(testCase,outputSize,[1 1]);
        end

When you open the test file, you can see that the software highlights the test names. Highlighted tests are linked to requirements. To see which requirement a test links to, right-click on the line and select Requirements.

In Requirements Editor, right-click the requirements set BatterySOCReqNetArch and click Run Tests. In the Run Tests dialog box, select the testNetworkInput and testNetworkOutput tests and click Run Tests.

The tests check that each of the network requirements are met and the verification status turns to green (Passed).

Next step: Test Deep Learning Network for Battery State of Charge Estimation or Compress Deep Learning Network for Battery State of Charge Estimation (optional). You can also open the next examples using the openExample function.

openExample("deeplearning_shared/CompressModelForBatteryStateOfChargeEstimationExample")
openExample("deeplearning_shared/TestModelForBatteryStateOfChargeEstimationExample")

See Also

(Requirements Toolbox) | | | |

Related Topics