Bayesian Optimization at LCLS
Using Gaussian Processes for Automated Tuning
Mitchell McIntire
mcintire@slac.stanford.edu

FEL Tuning at LCLS• The FEL beam is focused and tuned using quadrupole
magnets located along the beam line.
• Tuning has historically been done by hand by machine
operators and is very time consuming.
• Our approach: treat FEL tuning as an optimization problem.

2

FEL Tuning at LCLS• The FEL beam is focused and tuned using quadrupole
magnets located along the beam line.
• Tuning has historically been done by hand by machineoperators and is very time consuming.
• Our approach: treat FEL tuning as an optimization problem.

IN20

LI26

LI21Quads

LTU

3

2015 Tuning Data
• Data mining from the 2015 quad settings allows us tocollect data on tuning events.
• We filter for non-parasitic tuning events, removing e.g.
events like:

LTU Quads
4

2015 Tuning Data
• Data mining from the 2015 quad settings allows us tocollect data on tuning events.
• We filter for non-parasitic tuning events (below).
• These account for 200+ hours of machine time in 2015.

LI26 Quads
5Outline• Optimization• Bayesian optimization• Gaussian processes• Integration into LCLS
• Preliminary results

6

Review: Optimization• Optimization in brief: find 𝑥 that gives the best 𝑦• Requires evaluation of the objective function

7

Review: Optimization• Many types of optimization algorithms• Gradient methods

8Review: Optimization• Many types of optimization algorithms• Simulated annealing𝑇

9

Review: Optimization• Many types of optimization algorithms• Simulated annealing
𝑇

10

Review: Optimization• Many types of optimization algorithms• Genetic algorithms

https://commons.wikimedia.org/wiki/File:St_5-xband-antenna.jpg

11Review: Optimization
What do we want from an optimizer?

• Speed• Capability of handling noise• Interpretability

12

Review: Optimization
What do we want from an optimizer?

• Speed• Capability of handling noise• Interpretability

Bayesian optimization!

13

Bayesian optimization: High-level overview
Machine’s perspective:

Optimizer

14

Bayesian optimization: High-level overview
Machine’s perspective:
𝑦𝑡−1

Optimizer

15

Bayesian optimization: High-level overview
Machine’s perspective:
𝑦𝑡−1

Optimizer

16

Bayesian optimization: High-level overview
Machine’s perspective:

Optimizer

𝑥𝑡

17

Bayesian optimization: High-level overview
Machine’s perspective:

Optimizer

𝑥𝑡

18

Bayesian optimization: High-level overview
Machine’s perspective:𝑦𝑡

Optimizer

𝑥𝑡

19

Bayesian optimization: Mid-level overview
Optimizer’s perspective:

Model𝑡−1

Machine20

Bayesian optimization: Mid-level overview
Optimizer’s perspective:𝑦𝑡−1Model𝑡−1

Machine21

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡−1 , 𝑦𝑡−1 )Model𝑡−1

𝑦𝑡−1

Machine22

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡−1 , 𝑦𝑡−1 )𝑦𝑡−1Updating…
Model𝑡−1

Machine23

Bayesian optimization: Mid-level overview
Optimizer’s perspective:

Model𝑡Machine24

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡 , 𝑦𝑡 )Acquisition

Model𝑡Machine25

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡 , 𝑦𝑡 )Model𝑡Machine26

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡 , 𝑦𝑡 )Model𝑡𝑥𝑡

Machine27

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡 , 𝑦𝑡 )Model𝑡𝑥𝑡

Machine28

Bayesian optimization: Mid-level overview
Optimizer’s perspective:(𝑥𝑡 , 𝑦𝑡 )Model𝑡𝑦𝑡

Machine29

Bayesian optimization: Details
The optimizer is fully defined by its:

• Model• Acquisition function30Model: High-level• The model should be a mapping from parameter space
into probability distributions.
• For us, the model gives a probability distribution overpulse energies for each possible set of quad strengths.

𝑃 𝑦𝑦31

Gaussian processes• A Gaussian process (GP) is a nonparametric model thatpredicts a normal distribution at each point.
• The shape of the distribution is calculated by inference
over training data:
𝑃 𝑦𝑛𝑒𝑤 𝑥𝑛𝑒𝑤 , 𝑋𝑡𝑟𝑎𝑖𝑛 , 𝑌𝑡𝑟𝑎𝑖𝑛 ~ 𝒩(𝜇 𝑥 , 𝜎 𝑥 )32

GP Covariance Functions
A GP is fully defined by its:

• Prior mean function• Covariance function• Training data

A covariance function is essentially a similarity measure
between points in parameter space: 𝐾: ℝ𝑑 × ℝ𝑑 → ℝ

33

GP Covariance Functions• Covariance functions encode the type of functions wethink are likely, i.e. the way the data ‘should look’.
• A common choice is the squared exponential kernel:
𝐾 𝑥1 , 𝑥2 = 𝜃𝑒

http://pythonhosted.org/infpy/gps.html−𝑥1 −𝑥2 ⊤ 𝑥1 −𝑥22ℓ234

GP Covariance Functions• Covariance functions encode the type of functions wethink are likely, i.e. the way the data ‘should look’.
• We use a slight variation:
𝐾 𝑥1 , 𝑥2 = 𝜃𝑒−𝑥1 −𝑥2 ⊤ 𝑥1 −𝑥22ℓ2

http://pythonhosted.org/infpy/gps.html

𝐾 𝑥1 , 𝑥2 = 𝜃𝑒 − 𝑥1−𝑥2⊤ Λ−1 𝑥 −𝑥1235

Other Covariance Functions
Covariance functions can encode complex beliefs about thetype of functions that are likely:

http://pythonhosted.org/infpy/gps.html

36Acquisition function• Acquisition functions define exploration behavior.

• We use expected improvement (EI):
𝐼 𝑥 = max 𝑓 𝑥 − 𝑦 ∗ , 0 ,EI 𝑥 = 𝐸 𝐼 𝑥∞= 𝑦∗ 𝑦 − 𝑦 ∗ 𝑃 𝑦|𝑥 𝑑𝑦

37

Acquisition function• Acquisition functions define exploration behavior.

• We use expected improvement (EI):
𝐼 𝑥 = max 𝑓 𝑥 − 𝑦 ∗ , 0 ,EI 𝑥 = 𝐸 𝐼 𝑥∞= 𝑦∗ 𝑦 − 𝑦 ∗ 𝑃 𝑦|𝑥 𝑑𝑦

Easy to compute for aGaussian distribution

38

Example: Bayesian optimization

39

Example: Bayesian optimization

Acquisition point

40

Example: Bayesian optimization

Acquisition point

41

Example: Bayesian optimization

Acquisition point

42

Example: Bayesian optimization

Acquisition point

43

Example: Bayesian optimization

Acquisition point

44

Example: Bayesian optimization

Acquisition point

45

Example: Bayesian optimization

Acquisition point46

Example: Bayesian optimization

Acquisition point

47

Example: Bayesian optimization

Acquisition point48

Example: Bayesian optimization

Acquisition point

49

Integration into LCLS
• We use the following covariance function:
𝐾 𝑥1 , 𝑥2 = 𝜃𝑒 − 𝑥1−𝑥2⊤ Λ−1 𝑥 −𝑥12• The hyperparameters 𝜃 and Λ are calculated from historical
data, e.g. historical deviation of a certain quad’s settings.

• Via the Ocelot GUI (right),
arbitrary sets of quads canbe selected and optimized.

50

Analysis• The GP’s full model can be observed for analysis anddebugging, along with the acquisition function:

Observations

Model

Expected Improvement51

Analysis• The GP’s full model can be observed for analysis anddebugging, along with the acquisition function:

Observations

Model

Expected Improvement52

Results• Early results are promising, and the optimizer can perform welloptimizing 12 quads at once:• Man vs. Machine tests pending…
53

Future Work• We are not incorporating any physics into the optimizer!

• We want to use physical models and our knowledge
about the system to augment the optimization procedure.
• One possibility is marginalizing over hidden variables:
𝑃 𝑦|𝑥, 𝑋, 𝑌 =𝑃 𝑦|𝑥, 𝑋, 𝑌, 𝑧 𝑃 𝑧 𝑋, 𝑌 𝑑𝑧

• We could also work directly with physical parameters,e.g. 𝛼 and 𝛽, which describe the shape of the beam.

54