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 from its development branch and upgrade some dependencies:
%pip install --upgrade pip ipywidgets
%pip install pybop -q
Requirement already satisfied: pip in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (24.0) Requirement already satisfied: ipywidgets in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (8.1.2) Requirement already satisfied: comm>=0.1.3 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipywidgets) (0.2.1) Requirement already satisfied: ipython>=6.1.0 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipywidgets) (8.22.1) Requirement already satisfied: traitlets>=4.3.1 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipywidgets) (5.14.1) Requirement already satisfied: widgetsnbextension~=4.0.10 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipywidgets) (4.0.10) Requirement already satisfied: jupyterlab-widgets~=3.0.10 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipywidgets) (3.0.10) Requirement already satisfied: decorator in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1) Requirement already satisfied: jedi>=0.16 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.1) Requirement already satisfied: matplotlib-inline in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.6) Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.43) Requirement already satisfied: pygments>=2.4.0 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (2.17.2) Requirement already satisfied: stack-data in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3) Requirement already satisfied: pexpect>4.3 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0) Requirement already satisfied: parso<0.9.0,>=0.8.3 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.3) Requirement already satisfied: ptyprocess>=0.5 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0) Requirement already satisfied: wcwidth in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13) Requirement already satisfied: executing>=1.2.0 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.0.1) Requirement already satisfied: asttokens>=2.1.0 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.4.1) Requirement already satisfied: pure-eval in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (0.2.2) Requirement already satisfied: six>=1.12.0 in /Users/engs2510/.pyenv/versions/3.11.7/envs/pybop/lib/python3.11/site-packages (from asttokens>=2.1.0->stack-data->ipython>=6.1.0->ipywidgets) (1.16.0) Note: you may need to restart the kernel to use updated packages. Note: you may need to restart the kernel to use updated packages.
With the environment set up, we can now import PyBOP alongside other libraries we will need:
import numpy as np
import pybop
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")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)
t_eval = np.arange(0, 900, 1)
values = model.predict(t_eval=t_eval)
To make the parameter estimation more realistic, we add Gaussian noise to the data.
sigma = 0.001
corrupt_values = values["Voltage [V]"].data + np.random.normal(0, sigma, len(t_eval))
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]": t_eval,
"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.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.7, 0.025),
bounds=[0.6, 0.9],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.025),
bounds=[0.5, 0.8],
),
]
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. The default value for this parameter is sigma0=0.1
.
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.SumSquaredError(problem)
optim = pybop.Optimisation(cost, optimiser=pybop.GradientDescent)
optim.set_max_iterations(100)
NOTE: Boundaries ignored by Gradient Descent
We proceed to run the optimisation algorithm to estimate the parameters with the default learning rate.
x, final_cost = optim.run()
Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions Error: Events ['Maximum voltage [V]'] are non-positive at initial conditions
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.71033729, 0.55945988])
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, parameter_values=x, title="Optimised Comparison");
Now that we've seen how poor the default 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.015, 5
) # 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.Optimisation(cost, optimiser=pybop.GradientDescent, sigma0=sigma)
optim.set_max_iterations(100)
x, final_cost = optim.run()
optims.append(optim)
xs.append(x)
0.001 NOTE: Boundaries ignored by Gradient Descent 0.0045 NOTE: Boundaries ignored by Gradient Descent 0.008 NOTE: Boundaries ignored by Gradient Descent 0.0115 NOTE: Boundaries ignored by Gradient Descent 0.015 NOTE: Boundaries ignored by Gradient Descent
for optim, sigma in zip(optims, sigmas):
print(
f"| Sigma: {sigma} | Num Iterations: {optim._iterations} | Best Cost: {optim.optimiser.f_best()} | Results: {optim.optimiser.x_best()} |"
)
| Sigma: 0.001 | Num Iterations: 47 | Best Cost: 0.0008637695988057631 | Results: [0.75766989 0.66379605] | | Sigma: 0.0045 | Num Iterations: 100 | Best Cost: 0.0011612381826846523 | Results: [0.72722617 0.66903422] | | Sigma: 0.008 | Num Iterations: 100 | Best Cost: 0.0012674705312650204 | Results: [0.72350919 0.66970245] | | Sigma: 0.0115 | Num Iterations: 100 | Best Cost: 0.001026517347060025 | Results: [0.73304834 0.66800235] | | Sigma: 0.015 | Num Iterations: 74 | Best Cost: 0.36327076213686793 | Results: [0.62594548 0.62583821] |
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.6, 0.9], [0.5, 0.8]])
for optim, sigma in zip(optims, sigmas):
pybop.plot2d(optim, bounds=bounds, steps=15, title=f"Sigma: {sigma}")
Let's take sigma0 = 0.0115
as the best learning rate for this problem and look at the time-series trajectories.
optim = pybop.Optimisation(cost, optimiser=pybop.GradientDescent, sigma0=0.0115)
x, final_cost = optim.run()
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison");
NOTE: Boundaries ignored by Gradient Descent
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.