Introducing Weights to Individual Terms in the Cost Function used in FittingProblem? #726
-
Dear PyBOP Developers, I just discovered your code last month and it is phenomenal! I'm currently fitting voltage and heat generation rates from my batteries and it's amazing how well my model can fit the data. However, one thing I've noticed is that when fitting multiple signals (like Voltage [V] and Heating [W]) in the data, the fitting process is more skewed to fit my heat generation data. I suspect it's because in the units available the numerical values for heating (in [W]) is larger than voltages. And so the overall cost function is more dominated by the heat data. Does FittingProblem class has an option to add weights to different terms of the cost function? Is that feature available or do I need to hack into it somehow? Any suggestions are appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @muhammadhasyim ! It's great to hear you are using PyBOP for your model fitting. Weights are enabled in two places:
|
Beta Was this translation helpful? Give feedback.
-
In addition to @NicolaCourtier's suggestions above, we are currently undertaking a large refactor of the model, problem and cost classes. One such addition in this refactor is that you will be able to add costs to individual signals as they will be implemented as model variables and solved directly within the pybamm solver. An example of this is below, I'm hoping this refactor will land in builder = pybop.builders.Pybamm()
builder.set_dataset(dataset)
builder.set_simulation(
model,
parameter_values=parameter_values,
solver=IDAKLUSolver(atol=1e-6, rtol=1e-6),
)
builder.add_parameter(
pybop.Parameter(
"Negative electrode active material volume fraction", initial_value=0.6
)
)
builder.add_parameter(
pybop.Parameter(
"Positive electrode active material volume fraction", initial_value=0.6
)
)
builder.add_cost(pybop.PybammSumSquaredError(model_var = "Voltage [V]", data_column="Voltage [V]"), weight = 2.0)
builder.add_cost(pybop.PybammSumSquaredError(model_var = "Temperature [C]", data_column="Temperature [C]]"), weight = 0.5)
problem = builder.build()
optim = pybop.Adam(problem)
result = optim.run() You can take a look at the refactor in more detail on the https://github.com/pybop-team/PyBOP/tree/restructure branch. However, it's not quite ready for testing. |
Beta Was this translation helpful? Give feedback.
-
Both answers are excellent! Thank you, these are more than enough for me to get started. |
Beta Was this translation helpful? Give feedback.
Hi @muhammadhasyim ! It's great to hear you are using PyBOP for your model fitting.
Weights are enabled in two places:
A
pybop.WeightedCost
allows you to weight costs associated with either the same or different problems, e.g. you can start by defining separate costs for the voltage and heating. If these costs are based on a identical problem, then the underlying simulation should be run only once for each set of parameters. Seeexamples/scripts/getting_started/weighted_cost.py
.Alternatively, any fitting cost (that is an error measure) can be defined with an array of weights the same length as your domain data. See the documentation for the
pybop.FittingCost
. It would be great if you…