Optimize ACE hyper-parameters: minimize force time and fitting error.
Setup experiment
Load packages.
using AtomsBase, InteratomicPotentials, PotentialLearning
using Unitful, UnitfulAtomic
using LinearAlgebra, Random, DisplayAs
using DataFrames, Hyperopt
Define paths.
base_path = haskey(ENV, "BASE_PATH") ? ENV["BASE_PATH"] : "../../"
ds_path = "$base_path/examples/data/a-HfO2/a-HfO2-300K-NVT-6000.extxyz"
res_path = "$base_path/examples/Opt-ACE-aHfO2/results/";
Load utility functions.
include("$base_path/examples/utils/utils.jl");
Create experiment folder.
run(`mkdir -p $res_path`);
Load datasets
Load atomistic dataset: atomistic configurations (atom positions, geometry, etc.) + DFT data (energies, forces, etc.)
ds = load_data(ds_path, uparse("eV"), uparse("Å"))[1:1000]; # Load first 1K samples.
Split atomistic dataset into training and test
n_train, n_test = 50, 50 # Only 50 samples per dataset are used in this example.
conf_train, conf_test = split(ds, n_train, n_test)
(DataSet{num_configs = 50}
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy}
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy}
⋮
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy}, DataSet{num_configs = 50}
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy}
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy}
⋮
Configuration{S, AtomsBase.FlexibleSystem{3, AtomsBase.Atom, Unitful.Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}}, Forces, Energy})
Optimize hyper-parameters
Define a custom loss function. Here, we minimize fitting error and force calculation time. Possible metrics are e_mae
, e_rmse
, e_rsq
, f_mae
, f_rmse
, f_rsq
, and time_us
.
function custom_loss(
metrics::OrderedDict
)
e_mae = metrics[:e_mae]
f_mae = metrics[:f_mae]
time_us = metrics[:time_us]
e_mae_max = 0.05 # eV/atom
f_mae_max = 0.05 # eV/Å
w_e = e_mae/e_mae_max
w_f = f_mae/f_mae_max
w_t = 1.0E-3
loss = w_e * e_mae + w_f * e_mae + w_t * time_us
return loss
end;
Define model and hyper-parameter value ranges to be optimized.
model = ACE
pars = OrderedDict( :body_order => [2, 3, 4],
:polynomial_degree => [3, 4, 5],
:rcutoff => LinRange(4, 6, 10),
:wL => LinRange(0.5, 1.5, 10),
:csp => LinRange(0.5, 1.5, 10),
:r0 => LinRange(0.5, 1.5, 10));
Use latin hypercube sampling to find the optimal hyper-parameters. Alternatively, use random sampling (sampler = RandomSampler()).
sampler = CLHSampler(dims=[Categorical(3), Categorical(3), Continuous(),
Continuous(), Continuous(), Continuous()])
iap, res = hyperlearn!(model, pars, conf_train;
n_samples = 10, sampler = sampler,
loss = custom_loss, ws = [1.0, 1.0], int = true);
E_MAE:0.184 eV/atom, F_MAE:0.251 eV/Å, Time per force per atom:83.751 µs
E_MAE:0.137 eV/atom, F_MAE:0.217 eV/Å, Time per force per atom:111.008 µs
E_MAE:0.183 eV/atom, F_MAE:0.313 eV/Å, Time per force per atom:47.608 µs
E_MAE:0.173 eV/atom, F_MAE:0.302 eV/Å, Time per force per atom:56.41 µs
E_MAE:0.135 eV/atom, F_MAE:0.111 eV/Å, Time per force per atom:328.504 µs
E_MAE:0.196 eV/atom, F_MAE:0.175 eV/Å, Time per force per atom:93.402 µs
E_MAE:0.097 eV/atom, F_MAE:0.11 eV/Å, Time per force per atom:349.337 µs
E_MAE:0.134 eV/atom, F_MAE:0.22 eV/Å, Time per force per atom:86.827 µs
E_MAE:0.22 eV/atom, F_MAE:0.301 eV/Å, Time per force per atom:115.86 µs
E_MAE:0.127 eV/atom, F_MAE:0.207 eV/Å, Time per force per atom:149.277 µs
Post-process results
Save and show results.
@save_var res_path iap.β
@save_var res_path iap.β0
@save_var res_path iap.basis
@save_dataframe res_path res
res
Row | e_mae | e_rmse | e_rsq | f_mae | f_rmse | f_rsq | time_us | body_order | polynomial_degree | rcutoff | wL | csp | r0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Any | Any | Any | Any | Any | Any | Any | Any | Any | Any | Any | Any | Any | |
1 | 0.0970651 | 0.113797 | 0.837846 | 0.109806 | 0.141126 | 0.945789 | 349.337 | 2.0 | 4.0 | 4.44444 | 0.833333 | 0.5 | 0.722222 |
2 | 0.127196 | 0.15933 | 0.682123 | 0.207407 | 0.268214 | 0.804191 | 149.277 | 3.0 | 5.0 | 6.0 | 1.16667 | 0.722222 | 1.05556 |
3 | 0.133709 | 0.169442 | 0.640493 | 0.219647 | 0.286643 | 0.776358 | 86.8272 | 2.0 | 4.0 | 4.44444 | 0.833333 | 0.5 | 0.722222 |
4 | 0.134679 | 0.151332 | 0.713236 | 0.110639 | 0.141379 | 0.945595 | 328.504 | 2.0 | 4.0 | 4.0 | 0.722222 | 1.27778 | 1.16667 |
5 | 0.137439 | 0.17279 | 0.626147 | 0.21664 | 0.282534 | 0.782724 | 111.008 | 4.0 | 4.0 | 4.88889 | 1.38889 | 1.38889 | 1.38889 |
6 | 0.172597 | 0.219437 | 0.397043 | 0.302179 | 0.396194 | 0.572746 | 56.4098 | 4.0 | 3.0 | 5.11111 | 0.611111 | 0.944444 | 0.611111 |
7 | 0.182695 | 0.216332 | 0.413985 | 0.312526 | 0.409585 | 0.543376 | 47.6084 | 4.0 | 3.0 | 5.11111 | 0.611111 | 0.944444 | 0.611111 |
8 | 0.183823 | 0.23656 | 0.299275 | 0.251216 | 0.326366 | 0.710079 | 83.7507 | 4.0 | 4.0 | 4.88889 | 1.38889 | 1.38889 | 1.38889 |
9 | 0.196239 | 0.224415 | 0.369378 | 0.175272 | 0.225164 | 0.862004 | 93.402 | 2.0 | 4.0 | 4.0 | 0.722222 | 1.27778 | 1.16667 |
10 | 0.220447 | 0.277809 | 0.0335977 | 0.300963 | 0.392972 | 0.579666 | 115.86 | 3.0 | 5.0 | 6.0 | 1.16667 | 0.722222 | 1.05556 |
Plot error vs time.
err_time = plot_err_time(res)
@save_fig res_path err_time
DisplayAs.PNG(err_time)
This page was generated using Literate.jl.