{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "expmkveO04pw" }, "source": [ "## Learning Rate Calibration of Gradient Descent in PyBOP\n", "\n", "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.\n", "\n", "### Setting up the Environment\n", "\n", "Before we begin, we need to ensure that we have all the necessary tools. We will install PyBOP and upgrade dependencies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "X87NUGPW04py", "outputId": "0d785b07-7cff-4aeb-e60a-4ff5a669afbf" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/engs2510/Documents/Git/Second_PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "/Users/engs2510/Documents/Git/Second_PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install --upgrade pip ipywidgets -q\n", "%pip install pybop -q" ] }, { "cell_type": "markdown", "metadata": { "id": "jAvD5fk104p0" }, "source": [ "### Importing Libraries\n", "\n", "With the environment set up, we can now import PyBOP alongside other libraries we will need:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "SQdt4brD04p1" }, "outputs": [], "source": [ "import numpy as np\n", "\n", "import pybop\n", "\n", "pybop.plot.PlotlyManager().pio.renderers.default = \"notebook_connected\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's fix the random seed in order to generate consistent output during development, although this does not need to be done in practice." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(8)" ] }, { "cell_type": "markdown", "metadata": { "id": "5XU-dMtU04p2" }, "source": [ "## Generating Synthetic Data\n", "\n", "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.\n", "\n", "### Defining Parameters and Model\n", "\n", "We start by creating an example parameter set, constructing the single-particle model (SPM) and generating the synthetic data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "parameter_set = pybop.ParameterSet.pybamm(\"Chen2020\")\n", "parameter_set.update(\n", " {\n", " \"Negative electrode active material volume fraction\": 0.65,\n", " \"Positive electrode active material volume fraction\": 0.51,\n", " }\n", ")\n", "model = pybop.lithium_ion.SPM(parameter_set=parameter_set)\n", "initial_state = {\"Initial SoC\": 0.4}\n", "experiment = pybop.Experiment(\n", " [\n", " (\n", " \"Discharge at 0.5C for 6 minutes (4 second period)\",\n", " \"Charge at 0.5C for 6 minutes (4 second period)\",\n", " ),\n", " ]\n", " * 2\n", ")\n", "values = model.predict(initial_state=initial_state, experiment=experiment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding Noise to Voltage Data\n", "\n", "To make the parameter estimation more realistic, we add Gaussian noise to the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sigma = 0.002\n", "corrupt_values = values[\"Voltage [V]\"].data + np.random.normal(\n", " 0, sigma, len(values[\"Voltage [V]\"].data)\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "X8-tubYY04p_" }, "source": [ "## Identifying the Parameters" ] }, { "cell_type": "markdown", "metadata": { "id": "PQqhvSZN04p_" }, "source": [ "We will now set up the parameter estimation process by defining the datasets for optimisation and selecting the model parameters we wish to estimate." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating a Dataset\n", "\n", "The dataset for optimisation is composed of time, current, and the noisy voltage data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zuvGHWID04p_" }, "outputs": [], "source": [ "dataset = pybop.Dataset(\n", " {\n", " \"Time [s]\": values[\"Time [s]\"].data,\n", " \"Current function [A]\": values[\"Current [A]\"].data,\n", " \"Voltage [V]\": corrupt_values,\n", " }\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "ffS3CF_704qA" }, "source": [ "### Defining Parameters to Estimate\n", "\n", "We select the parameters for estimation and set up their prior distributions and bounds:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WPCybXIJ04qA" }, "outputs": [], "source": [ "parameters = pybop.Parameters(\n", " pybop.Parameter(\n", " \"Negative electrode active material volume fraction\",\n", " prior=pybop.Uniform(0.45, 0.7),\n", " bounds=[0.4, 0.8],\n", " true_value=0.65,\n", " ),\n", " pybop.Parameter(\n", " \"Positive electrode active material volume fraction\",\n", " prior=pybop.Uniform(0.45, 0.7),\n", " bounds=[0.4, 0.8],\n", " true_value=0.51,\n", " ),\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "n4OHa-aF04qA" }, "source": [ "### Setting up the Optimisation Problem with incorrect sigma value\n", "\n", "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "etMzRtx404qA" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NOTE: Boundaries ignored by \n" ] } ], "source": [ "problem = pybop.FittingProblem(model, parameters, dataset)\n", "cost = pybop.SumSquaredError(problem)\n", "optim = pybop.GradientDescent(cost, sigma0=0.2, max_iterations=100)" ] }, { "cell_type": "markdown", "metadata": { "id": "caprp-bV04qB" }, "source": [ "### Running the Optimisation\n", "\n", "We proceed to run the optimisation algorithm to estimate the parameters with the updated learning rate (`sigma0`)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "-9OVt0EQ04qB" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Halt: No significant change for 15 iterations.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.64605501 0.51469905]\n", " Final cost: 0.0013514039920203883\n", " Optimisation time: 2.287174940109253 seconds\n", " Number of iterations: 52\n", " SciPy result available: No\n" ] } ], "source": [ "results = optim.run()" ] }, { "cell_type": "markdown", "metadata": { "id": "-4pZsDmS04qC" }, "source": [ "### Viewing the Estimated Parameters\n", "\n", "After the optimisation, we can examine the estimated parameter values. In this case, the optimiser misses the optimal solution by a large amount." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Hgz8SV4i04qC", "outputId": "e1e42ae7-5075-4c47-dd68-1b22ecc170f6" }, "outputs": [ { "data": { "text/plain": [ "array([0.64605501, 0.51469905])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results.x # This will output the estimated parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot the time-series prediction for the given solution. As we suspected, the optimiser found a very poor solution. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pybop.plot.quick(problem, problem_inputs=results.x, title=\"Optimised Comparison\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calibrating the Learning Rate \n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.001\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.53090948 0.59339862]\n", " Final cost: 0.014783332157851634\n", " Optimisation time: 4.26934027671814 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.012285714285714285\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.60345692 0.56598814]\n", " Final cost: 0.002790217328868725\n", " Optimisation time: 4.251996040344238 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.023571428571428573\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.62409599 0.5385165 ]\n", " Final cost: 0.0017177540172860569\n", " Optimisation time: 4.208575963973999 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.03485714285714286\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.63571019 0.52533286]\n", " Final cost: 0.0014363025957628353\n", " Optimisation time: 4.245105028152466 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.046142857142857145\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.641494 0.51930683]\n", " Final cost: 0.0013701538891978493\n", " Optimisation time: 4.226551055908203 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.05742857142857143\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: Maximum number of iterations (100) reached.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.64414678 0.5165881 ]\n", " Final cost: 0.0013574904717929912\n", " Optimisation time: 4.241044044494629 seconds\n", " Number of iterations: 100\n", " SciPy result available: No\n", "0.06871428571428571\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: No significant change for 15 iterations.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.64460623 0.51615892]\n", " Final cost: 0.0013563752974500437\n", " Optimisation time: 3.6841771602630615 seconds\n", " Number of iterations: 87\n", " SciPy result available: No\n", "0.08\n", "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: No significant change for 15 iterations.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.64463942 0.51613016]\n", " Final cost: 0.001356018556480287\n", " Optimisation time: 3.1757869720458984 seconds\n", " Number of iterations: 75\n", " SciPy result available: No\n" ] } ], "source": [ "sigmas = np.linspace(0.001, 0.08, 8) # Change this to a smaller range for a quicker run\n", "xs = []\n", "optims = []\n", "for sigma in sigmas:\n", " print(sigma)\n", " problem = pybop.FittingProblem(model, parameters, dataset)\n", " cost = pybop.SumSquaredError(problem)\n", " optim = pybop.GradientDescent(cost, sigma0=sigma, max_iterations=100)\n", " results = optim.run()\n", " optims.append(optim)\n", " xs.append(results.x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Sigma: 0.001 | Num Iterations: 100 | Best Cost: 0.014783332157851634 | Results: [0.53090948 0.59339862] |\n", "| Sigma: 0.012285714285714285 | Num Iterations: 100 | Best Cost: 0.002790217328868725 | Results: [0.60345692 0.56598814] |\n", "| Sigma: 0.023571428571428573 | Num Iterations: 100 | Best Cost: 0.0017177540172860569 | Results: [0.62409599 0.5385165 ] |\n", "| Sigma: 0.03485714285714286 | Num Iterations: 100 | Best Cost: 0.0014363025957628353 | Results: [0.63571019 0.52533286] |\n", "| Sigma: 0.046142857142857145 | Num Iterations: 100 | Best Cost: 0.0013701538891978493 | Results: [0.641494 0.51930683] |\n", "| Sigma: 0.05742857142857143 | Num Iterations: 100 | Best Cost: 0.0013574904717929912 | Results: [0.64414678 0.5165881 ] |\n", "| Sigma: 0.06871428571428571 | Num Iterations: 87 | Best Cost: 0.0013563752974500437 | Results: [0.64460623 0.51615892] |\n", "| Sigma: 0.08 | Num Iterations: 75 | Best Cost: 0.001356018556480287 | Results: [0.64463942 0.51613016] |\n" ] } ], "source": [ "for optim, sigma in zip(optims, sigmas):\n", " print(\n", " f\"| Sigma: {sigma} | Num Iterations: {optim.result.n_iterations} | Best Cost: {optim.optimiser.f_best()} | Results: {optim.optimiser.x_best()} |\"\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perhaps a better way to view this information is to plot the optimiser convergences," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "for optim, sigma in zip(optims, sigmas):\n", " pybop.plot.convergence(optim, title=f\"Sigma: {sigma}\")\n", " pybop.plot.parameters(optim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cost Landscapes\n", "\n", "Finally, we can visualise the cost landscape and the path taken by the optimiser:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Plot the cost landscape with optimisation path and updated bounds\n", "bounds = np.array([[0.4, 0.8], [0.4, 0.8]])\n", "for optim, sigma in zip(optims, sigmas):\n", " pybop.plot.surface(optim, bounds=bounds, title=f\"Sigma: {sigma}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Updating the Learning Rate\n", "\n", "Let's take `sigma0 = 0.08` as the best learning rate for this problem and look at the time-series trajectories." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NOTE: Boundaries ignored by \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Halt: No significant change for 15 iterations.\n", "OptimisationResult:\n", " Initial parameters: [0.46478836 0.56896942]\n", " Optimised parameters: [0.64463942 0.51613016]\n", " Final cost: 0.001356018556480287\n", " Optimisation time: 3.1555721759796143 seconds\n", " Number of iterations: 75\n", " SciPy result available: No\n" ] }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "optim = pybop.Optimisation(cost, optimiser=pybop.GradientDescent, sigma0=0.08)\n", "results = optim.run()\n", "pybop.plot.quick(problem, problem_inputs=results.x, title=\"Optimised Comparison\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conclusion\n", "\n", "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." ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 4 }