In this notebook, we calibrate the learning rate for the gradient descent optimiser on a parameter identification problem. The gradient descent learning rate is taken as the sigma0
value passed to the pybop.Optimisation
class, or via problem.sigma0
or cost.sigma0
if it is passed earlier in the workflow.
Before we begin, we need to ensure that we have all the necessary tools. We will install PyBOP and upgrade dependencies:
%pip install --upgrade pip ipywidgets -q
%pip install pybop -q
With the environment set up, we can now import PyBOP alongside other libraries we will need:
import numpy as np
import pybop
Let's fix the random seed in order to generate consistent output during development, although this does not need to be done in practice.
np.random.seed(8)
To demonstrate parameter estimation, we first need some data. We will generate synthetic data using a forward model, which requires defining a parameter set and the model itself.
We start by creating an example parameter set, constructing the single-particle model (SPM) and generating the synthetic data.
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
parameter_set.update(
{
"Negative electrode active material volume fraction": 0.65,
"Positive electrode active material volume fraction": 0.51,
}
)
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)
initial_state = {"Initial SoC": 0.4}
experiment = pybop.Experiment(
[
(
"Discharge at 0.5C for 6 minutes (4 second period)",
"Charge at 0.5C for 6 minutes (4 second period)",
),
]
* 2
)
values = model.predict(initial_state=initial_state, experiment=experiment)
To make the parameter estimation more realistic, we add Gaussian noise to the data.
sigma = 0.002
corrupt_values = values["Voltage [V]"].data + np.random.normal(
0, sigma, len(values["Voltage [V]"].data)
)
We will now set up the parameter estimation process by defining the datasets for optimisation and selecting the model parameters we wish to estimate.
The dataset for optimisation is composed of time, current, and the noisy voltage data:
dataset = pybop.Dataset(
{
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": corrupt_values,
}
)
We select the parameters for estimation and set up their prior distributions and bounds:
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Uniform(0.45, 0.7),
bounds=[0.4, 0.8],
true_value=0.65,
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Uniform(0.45, 0.7),
bounds=[0.4, 0.8],
true_value=0.51,
),
)
With the datasets and parameters defined, we can set up the optimisation problem, its cost function, and the optimiser. For gradient descent, the sigma0
value corresponds to the learning rate. Let's set this hyperparmeter incorrectly to view how we calibrate it. In this example, let's start with sigma0=0.2
.
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.SumSquaredError(problem)
optim = pybop.GradientDescent(cost, sigma0=0.2, max_iterations=100)
NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'>
We proceed to run the optimisation algorithm to estimate the parameters with the updated learning rate (sigma0
).
x, final_cost = optim.run()
After the optimisation, we can examine the estimated parameter values. In this case, the optimiser misses the optimal solution by a large amount.
x # This will output the estimated parameters
array([0.64601239, 0.51476693])
Let's plot the time-series prediction for the given solution. As we suspected, the optimiser found a very poor solution.
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison");
Now that we've seen how poor an incorrect sigma0
value is for this optimisation problem, let's calibrate this value to find the optimal solution in the lowest number of iterations.
sigmas = np.linspace(0.001, 0.08, 8) # Change this to a smaller range for a quicker run
xs = []
optims = []
for sigma in sigmas:
print(sigma)
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.SumSquaredError(problem)
optim = pybop.GradientDescent(cost, sigma0=sigma, max_iterations=100)
x, final_cost = optim.run()
optims.append(optim)
xs.append(x)
0.001 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.012285714285714285 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.023571428571428573 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.03485714285714286 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.046142857142857145 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.05742857142857143 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.06871428571428571 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'> 0.08 NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'>
for optim, sigma in zip(optims, sigmas):
print(
f"| Sigma: {sigma} | Num Iterations: {optim.result.n_iterations} | Best Cost: {optim.pints_optimiser.f_best()} | Results: {optim.pints_optimiser.x_best()} |"
)
| Sigma: 0.001 | Num Iterations: 100 | Best Cost: 0.014783315503783024 | Results: [0.53090951 0.59339848] | | Sigma: 0.012285714285714285 | Num Iterations: 100 | Best Cost: 0.002790218204711622 | Results: [0.60345692 0.56598814] | | Sigma: 0.023571428571428573 | Num Iterations: 100 | Best Cost: 0.0017177540344804646 | Results: [0.62409599 0.5385165 ] | | Sigma: 0.03485714285714286 | Num Iterations: 100 | Best Cost: 0.0014363017319172118 | Results: [0.63571021 0.52533283] | | Sigma: 0.046142857142857145 | Num Iterations: 100 | Best Cost: 0.0013701663792480554 | Results: [0.64149364 0.51930697] | | Sigma: 0.05742857142857143 | Num Iterations: 100 | Best Cost: 0.001357601973802709 | Results: [0.64413933 0.51659996] | | Sigma: 0.06871428571428571 | Num Iterations: 94 | Best Cost: 0.0013551364068768164 | Results: [0.64516461 0.51563868] | | Sigma: 0.08 | Num Iterations: 77 | Best Cost: 0.001355542610768788 | Results: [0.64475868 0.51599912] |
Perhaps a better way to view this information is to plot the optimiser convergences,
for optim, sigma in zip(optims, sigmas):
pybop.plot_convergence(optim, title=f"Sigma: {sigma}")
pybop.plot_parameters(optim)
Finally, we can visualise the cost landscape and the path taken by the optimiser:
# Plot the cost landscape with optimisation path and updated bounds
bounds = np.array([[0.4, 0.8], [0.4, 0.8]])
for optim, sigma in zip(optims, sigmas):
pybop.plot2d(optim, bounds=bounds, steps=10, title=f"Sigma: {sigma}")
Let's take sigma0 = 0.08
as the best learning rate for this problem and look at the time-series trajectories.
optim = pybop.Optimisation(cost, optimiser=pybop.GradientDescent, sigma0=0.08)
x, final_cost = optim.run()
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison");
NOTE: Boundaries ignored by <class 'pints._optimisers._gradient_descent.GradientDescent'>
This notebook covers how to calibrate the learning rate for the gradient descent optimiser. This provides an introduction into hyper-parameter tuning that will be discussed in further notebooks.