2.11. Restarting (continuing) an optimization

You can continue an optimization from where a previous one stopped. This works best with the CMAOptimizer.

../../_images/LJ_Ar_restart_losses.png

Fig. 2.5 Illustration of the loss function for a restarted run (blue, restarted from green) compared to an uninterrupted run (purple, shifted down for clarity) using a parallel CMA optimizer. The blue and green lines do not always perfectly overlap the purple line, because of the way the loss function is logged when running in parallel.

2.11.1. Restarting with the CMAOptimizer

Make a copy of the directory $AMSHOME/scripting/scm/params/examples/LJ_Ar_restart.

There are three different .conf.py files:

  • 100.conf.py, which will run an optimization for 100 iterations.
  • restart_100.conf.py, which specifies to restart from where the previous optimization left off for another 100 iterations.
  • 200.conf.py, which runs an optimization for 200 iterations uninterrupted (for comparison purposes).

The .conf.py files are not read from the GUI - instead you will need to recreate them if you use the ParAMS GUI.

2.11.1.1. Run the first 100 evaluations

Unlike the Getting Started: Lennard-Jones Potential for Argon tutorial, we here set max_evaluations to 100, and use a CMA-ES optimizer. CMA-ES is a derivative-free optimizer, and has many options. The three most important options are

  • sigma, describing how wide a distribution to initially sample,
  • popsize, giving the population size (how many evaluations are performed at each CMA iteration)
  • minsigma, giving a converge criterion for sigma. During the optimization, sigma will decrease, and if it reaches this value the optimization will stop.

Some more details about the CMA optimizer are given in the Run the ReaxFF parametrization section of the ReaxFF tutorial.

The seed option gives a seed for the random number generator. You should normally not set it, it is set here only so that your numbers will match the ones in the tutorial.

Start the ParAMS GUI: SCM → ParAMS
File → Open, and choose the job_collection.yaml file from the example directory
Switch to the Settings panel
In the drop-down list, choose Max Evaluations
On the max_evaluations line, type 100
In the drop-down list, choose Optimizer: CMAOptimizer
Replace the optimizer line with CMAOptimizer(sigma=0.05, popsize=4, minsigma=5e-4, seed=314159)
../../_images/restart_100.png
File → Save As
Save with the name 100.params
File → Run

The file 100.conf.py contains:

# Define a CMA optimizer
optimizer = CMAOptimizer(sigma=0.05, popsize=4, minsigma=5e-4, seed=314159) 

# Stop the optimization after 100 evaluations
max_evaluations = 100

Run the optimization with this command:

"$AMSBIN/params" -c 100.conf.py run -o 100

Here, -c 100.conf.py specifies the file to use, and -o 100 will place the results in a directory called 100.

In the output, you will see that the loss value fluctuates quite wildly in the beginning.

../../_images/LJ_Ar_restart_running_loss_100.png

Note

The CMAOptimizer works in parallel. In the output you may see the results seemingly printed “out of order”, for example the results for evaluation 61 might be printed before evaluation 60. That is completely normal for the CMAOptimizer in params.

2.11.1.2. Continue for another 100 iterations with a restart file

After the parametrization has finished:

Switch to the Settings panel
In the drop-down list, choose Optimizer: Restart CMAOptimizer
This changes the optimizer line to become CMAOptimizer(restartfile='cma-restart.pkl', exact_restart=True)
Verify that max_iterations is still 100
../../_images/restart_restart_100.png

The restartfile='cma-restart.pkl' will use the restart file from the previous optimization. Do not change this name.

The exact_restart=True option means that the optimization will continue in the same way as if it hadn’t stopped. Set exact_restart=False to instead get a random continuation.

File → Save As
Save with the name restart_100.params
File → Run

The restart_100.conf.py file contains:

# Specify a restart file
optimizer = CMAOptimizer(restartfile='100/cmadata/restart.pkl', exact_restart=True) 

# When using a CMA restart file, the initial parameters do not influence the optimization, so there is no need to evaluate with them
skip_x0 = True

# Stop the optimization after 100 evaluations
max_evaluations = 100

Here, the CMAOptimizer is initialized with restartfile='100/cmadata/restart.pkl'. Verify that the file restart.pkl exists in the directory 100/cmadata before running the optimization. The exact_restart=True option means that the optimization will continue in the same way as if it hadn’t stopped. Set exact_restart=False to instead get a random continuation.

The restart.pkl file contains all the details about the previous optimization, including the last parameters. The skip_x0 = True line means that the initial evaluation that is normally performed using the parameters defined in parameter_interface.yaml will be skipped.

Run the optimization with this command:

"$AMSBIN/params" -c restart_100.conf.py run -o restart_100

This optimization continues from where the previous one left off. This has the following consequences for the logged loss values:

  • The logged loss values are smaller than in the previous optimization
  • The logged loss values fluctuate less wildly than in the previous optimization
../../_images/LJ_Ar_restart_running_loss_restart_100.png

2.11.1.3. Compare to an uninterrupted run of 200 iterations

Go back to Run the first 100 evaluations, but set max_evaluations to 200 and make sure that the initial parameters are those from the example directory (and not the already optimized parameters).

If you need to change your parameters back:

File → Revert Parameters

Save with the name 200.params and run.

Plot (for example in Excel) the three running loss functions in

  • 100.results/optimization/training_set_results/running_loss.txt
  • restart_100.results/optimization/training_set_results/running_loss.txt
  • 200.results/optimization/training_set_results/running_loss.txt

Compare to the figure at the beginning of this tutorial (shift the curve for restart_100 by 100 steps).

Run "$AMSBIN/params" -c 200.conf.py run -o 200, and plot (for example in Excel) the three running loss functions in

  • 100/training_set_results/running_loss.txt
  • restart_100/training_set_results/running_loss.txt
  • 200/training_set_results/running_loss.txt

Compare to the figure at the beginning of this tutorial (shift the curve for restart_100 by 100 steps).

The logged loss functions are almost, but not perfectly identical. This is because the of the way loss function values are logged with the parallel CMA optimizer. See the figure at the top.

2.11.2. Restart with other optimizers (e.g. Nelder-Mead from Scipy)

For other optimizers, like the Nelder-Mead optimizer that was used in the first tutorial, there is no particular restart functionality in ParAMS. If you use these optimizers, simply continue from the optimized parameters.

  • GUI: The parameters are automatically updated to be the best parameters as the parametrization is running

  • Command-line: Specify in params.conf.py:

    parameter_interface = 'path/to/previous/optimization/training_set_results/latest/parameter_interface.yaml'