Training neural networks with end-to-end optical backpropagation
Optics is an exciting route for the next generation of computing hardware for
machine learning, promising several orders of magnitude enhancement in both
computational speed and energy efficiency. However, to reach the full capacity
of an optical neural network it is necessary that the computing not only for
the inference, but also for the training be implemented optically. The primary
algorithm for training a neural network is backpropagation, in which the
calculation is performed in the order opposite to the information flow for
inference. While straightforward in a digital computer, optical implementation
of backpropagation has so far remained elusive, particularly because of the
conflicting requirements for the optical element that implements the nonlinear
activation function. In this work, we address this challenge for the first time
with a surprisingly simple and generic scheme. Saturable absorbers are employed
for the role of the activation units, and the required properties are achieved
through a pump-probe process, in which the forward propagating signal acts as
the pump and backward as the probe. Our approach is adaptable to various analog
platforms, materials, and network structures, and it demonstrates the
possibility of constructing neural networks entirely reliant on analog optical
processes for both training and inference tasks.
James Spall, Xianxin Guo, A. I. Lvovsky
# Training neural networks with end-to-end optical backpropagation

###### Abstract

Optics is an exciting route for the next generation of computing hardware for machine learning, promising several orders of magnitude enhancement in both computational speed and energy efficiency. However, to reach the full capacity of an optical neural network it is necessary that the computing not only for the inference, but also for the training be implemented optically. The primary algorithm for training a neural network is backpropagation, in which the calculation is performed in the order opposite to the information flow for inference. While straightforward in a digital computer, optical implementation of backpropagation has so far remained elusive, particularly because of the conflicting requirements for the optical element that implements the nonlinear activation function. In this work, we address this challenge for the first time with a surprisingly simple and generic scheme. Saturable absorbers are employed for the role of the activation units, and the required properties are achieved through a pump-probe process, in which the forward propagating signal acts as the pump and backward as the probe. Our approach is adaptable to various analog platforms, materials, and network structures, and it demonstrates the possibility of constructing neural networks entirely reliant on analog optical processes for both training and inference tasks.

## I Introduction

Machine learning, one of the most revolutionary scientific breakthroughs in the past decades, has completely transformed the technology landscape, enabling innovative applications in fields ranging from natural language processing to drug discovery. As the demand for increasingly sophisticated machine learning models continues to escalate, there is a pressing need for faster and more energy-efficient computing solutions. In this context, analog computing has emerged as a promising alternative to traditional digital electronics [1; 2; 3; 4; 5; 6; 7]. A particularly exciting platform for analog neural networks (NNs) is optics, in which the interference and diffraction of light during propagation implements the linear part of every computational layer [8; 9].

Most of the current analog computing research and development is aimed at using the NN for inference [8; 10]. Training such NNs, on the other hand, is a challenge. This is because the backpropagation algorithm [11], the workhorse of training in digital NNs, requires the calculation to be performed in the order opposite to the information flow for inference, which is difficult to implement on an analog physical platform. Hence analog models are typically trained offline (_in silico_), on a separate digital simulator, after which the parameters are transferred to the analog hardware. In addition to being slow and inefficient, this approach can lead to errors arising from imperfect simulation and systematic errors ('reality gap'). In optics, for example, these effects may result from dust, aberrations, spurious reflections and inaccurate calibration [12].

To enable learning in analog NNs, different approaches have been proposed and realized [13]. Several groups explored various 'hardware-in-the loop' schemes, in which, while the backpropagation was done _in silico_, the signal acquired from the analog NN operating in the inference regime was incorporated into the calculation of the feedback for optimizing the NN parameters [14; 15; 16; 17; 18; 19; 20]. This has partially reduced the training error, but has not addressed the low speed and inefficiency of _in silico_ training.

Recently, several optical neural networks (ONNs) were reported that were trained online (_in situ_) using methods alternative to backpropagation. Bandyopadhyay _et al._ trained an ONN based on integrated photonic circuits using simultaneous perturbation stochastic approximation, i.e. randomly perturbing all ONN parameters and using the observed change of the loss function to approximate its gradient [21]. Filipovich _et al._ applied direct feedback alignment, wherein the error calculated at the output of the ONN is used to update the parameters of all layers [22]. However both these methods are inferior to backpropagation as they take much longer to converge, especially for sufficiently deep ONNs [23].

An optical implementation of the backpropagation algorithm was proposed by Hughes _et al._[24], and recently demonstrated experimentally [25], showing that the training methods of current digital NNs can be applied to analog hardware. However, their scheme omitted a crucial step for optical implementation: backpropagation through nonlinear activation layers. Their method requires digital nonlinear activation and multiple optoelectronic inter-conversions inside the network, complicating the training process. Furthermore, the method applies only to a specific type of ONN that uses interferometer meshes for the linear layer, and does not generalise to other ONN architectures. Complete implementation of the backpropagation algorithm in optics, through
all the linear and nonlinear layers, that can generalise to many ONN systems, remains a highly challenging goal.

In this work, we address this long-standing challenge and present the first complete optical implementation of the backpropagation algorithm in a two-layer ONN. The gradients of the loss function with respect to the NN parameters are calculated by light travelling through the system in the reverse direction. The main difficulty of all-optical training lies in the requirement that the nonlinear optical element used for the activation function needs to exhibit different properties for the forward and backward propagating signals. Fortunately, as demonstrated in our earlier theoretical work [26] and explained below, there does exist a group of nonlinear phenomena, which exhibits the required set of properties with sufficient precision.

We optically train our ONNs to perform classification tasks, and our results surpass those trained with a conventional _in silico_ method. Our optical training scheme can be further generalized to other platforms using different linear layers and analog activation functions, making it an ideal tool for exploring the vast potential of analog computing for training neural networks.

## II Optical training algorithm

We consider a multilayer perceptron -- a common type of NN which consists of multiple linear layers that establish weighted connections between neurons, inter-laid by activation functions that enable the network to learn complex nonlinear functions. To train the NN, one presents it with a training set of labeled examples and iteratively adjusts the NN parameters (weights and biases) to find the correct mapping between the inputs and outputs.

The training steps are summarised in Fig. 1(d). The weight matrices, denoted \(W^{(i)}\) for the \(i\)-th layer, are first initialized with random values. Each iteration of training starts by entering the input examples from the training set as input vectors \(x=a^{(0)}\) into the NN, and forward propagating through all of its layers. In every layer \(i\), one performs a matrix-vector multiplication (MVM) of the weight matrix and the activation vector,

\[z^{(i)}=W^{(i)}\times a^{(i-1)}, \tag{1}\]

followed by element-wise application of the activation function \(g(\cdot)\) to the resulting vector:

\[a^{(i)}=g\left(z^{(i)}\right). \tag{2}\]

The output \(y=a^{(L)}\) of an \(L\)-layer NN allows one to compute the loss function \(\mathcal{L}(y,t)\) that determines the difference between the network predictions \(y\) and ground truth labels \(t\) from the training set. The backpropagation algorithm helps calculating the gradient of this loss function with respect to all the parameters in the network, through what is essentially an application of the chain rule of calculus. The network parameters are then updated using these gradients and optimization algorithms such as stochastic gradient descent. The training process is repeated until convergence.

The gradients we require are given by [11] as

\[\frac{\partial\mathcal{L}}{\partial W^{(i)}}=\delta^{(i)}\otimes a^{(i-1)}, \tag{3}\]

where \(\delta^{(i)}\) is referred to as the "error vector" at the \(i\)th layer and \(\otimes\) denotes the outer product. The error vector is calculated as

\[\delta^{(i-1)}=\left({W^{(i)}}^{T}\times\delta^{(i)}\right)g^{\prime}\left(z^{ (i-1)}\right), \tag{4}\]

going through layers in reverse sequence. The expression for the error vector \(\delta^{(L)}\) in the last layer depends on the choice of the loss function, but for the common loss functions of mean-squared error and cross-entropy (with an appropriate choice of activation function) it is simply the difference between the NN output and the label: \(\delta^{(L)}=y-t\).

Therefore, to calculate the gradients at each layer we need one vector from the forward pass through the network (the activations) and one vector from the backward pass (the errors).

We see from Eq. (4) that the error backpropagation consists of two operations. First we must perform an MVM, mirroring the feedforward linear operation (1). In an ONN, this can be done by light that propagates backwards through the same linear optical arrangement [27]. The second operation consists in modulation of the MVM output by the activation function derivative and poses a significant challenge for optical implementation. This is because most optical media exhibit similar properties for forward and backward propagation. On the other hand, our application requires an optical element that is (1) nonlinear in the forward direction, (2) linear in the backward direction and (3) modulates the backward light amplitude by the derivative of the forward activation function.

We have solved this challenge with our optical backpropagation protocol, which calculates the right-hand side of Eq. (4) entirely optically with no opto-electronic conversion or digital processing. The first component of our solution is the observation that many optical media exhibit nonlinear properties for strong optical fields, but are approximately linear for weak fields. Hence, we can satisfy the conditions (1) and (2) by maintaining the back-injected beam at a much lower intensity level than the forward. Furthermore, there exists a set of nonlinear phenomena that also addresses the requirement (3). An example is saturable absorption (SA). The transmissivity of SA medium in the backward direction turns out to approximate the derivative of its intensity-dependent transmission in the forward direction \(g^{\prime}\left(z^{(i-1)}\right)\). This approximation is valid up to a certain numerical factor and only for small values of \(z^{(i-1)}\); however, as shown in
our prior work [26], this is sufficient for successful training.

## III Multilayer Onn

Our ONN as shown in Fig. 1(a,b) is implemented in a free-space tabletop setting. The neuron values are encoded in the transverse spatial structure of the propagating light field amplitude. Spatial light modulators are used to encode the input vectors and weight matrices. The NN consists of two fully-connected linear layers implemented with optical MVM following our previously demonstrated experimental design [28].

This design has a few characteristics that make it suitable for use in a deep neural network. First, it is re

Figure 1: **Illustration of optical training.****(a)** Network architecture of the ONN used in this work, which consists of two fully-connected linear layers and a hidden layer. **(b)** Simplified experimental schematic of the ONN. Each linear layer performs optical MVM with a cylindrical lens and a spatial light modulator (SLM) that encodes the weight matrix. Hidden layer activations are computed using SA in an atomic vapour cell. Light propagates in both directions during optical training. **(c)** Working principle of SA activation. The forward beam (pump) is shown by solid red arrows, backward (probe) by purple wavy arrows. The probe transmission depends on the strength of the pump and approximates the gradient of the SA function. For high forward intensity (top panel), a large portion of the atoms are excited to the upper level. Stimulated emission produced by these atoms largely compensates the absorption due to the atoms in the ground level. For weak pump (bottom panel), the excited level population is low and the absorption is significant. **(d)** Neural network training procedure. **(e)** Optical training procedure. Both signal and error propagation in the two directions are fully implemented optically. Loss function calculation and parameter update are left for electronics without interrupting the optical information flow.
configurable, so that both neuron values and network weights can be arbitrarily changed. Second, multiple MVM blocks can be cascaded to form a multilayer network, as the output of one MVM naturally forms the input of the next MVM. Using a coherent beam also allows us to encode both positive- and negative-valued weights. Finally, the MVM works in both directions, meaning the inputs and outputs are reversible, which is critical for the implementation of our optical backpropagation algorithm. The hidden layer activation between the two layers is implemented optically by means of SA in a rubidium atomic vapour cell [Fig. 1(c)].

## Results

### Linear layers

We first set up the linear layers that serve as the backbone of our ONN, and we make sure that they work accurately and simultaneously in both directions -- a highly challenging task that has never been achieved before to our best knowledge.

This involves three MVMs: first layer in the forward direction (MVM-1), second layer in both forward (MVM-2a) and backward (MVM-2b) directions. To characterise these MVMs, we apply random vectors and matrices and simultaneously measure the output of all three: the results for 300 random MVMs are presented in Fig. 2(a). To quantify the MVM performance, we define the signal-to-noise ratio (SNR, see Methods for details). As illustrated by the histograms, MVM-1 has the greatest SNR of 14.9, and MVM-2a has a lower SNR of 7.1, as a result of noise accumulation from both layers and the reduced signal range. MVM-2b has a slightly lower SNR of 6.7, because the optical system is optimized for the forward direction. Comparing these experimental results with a simple numerical model, we estimate 1.3% multiplicative noise in our MVMs, which is small enough not to degrade the ONN performance [12].

### Nonlinearity

With the linear layers fully characterized, we now measure the response of the activation units in both directions. With the vapor cell placed in the setup and the laser tuned to resonance with the atomic transition, we pass the output of MVM-1 through the vapor cell in the forward direction. The response as presented in Fig. 2(b) shows strong nonlinearity. We fit the data with the theoretically expected SA transmissivity (see Supplementary for details), thereby finding the optical depth to be \(\alpha_{0}=7.3\), which is sufficient to achieve high accuracy in ONNs [26]. The optical depth and the associated nonlinearity can be easily tuned to fit different network requirements by controlling the temperature of the vapor cell. In the backward direction, we pass weak probe beams through the vapor cell and measure the output. Both the forward and backward beams are simultaneously present in the vapor cell during the measurement.

In Fig. 2(c) we measure the effect of the forward amplitude \(z^{(1)}\) on the transmission of the backward beam through the SA. The theoretical fit for these data -- the expected backward transmissivity calculated from the physical properties of SA -- is shown by the red curve. For comparison, the orange curve shows the rescaled exact derivative \(g^{\prime}\left(z^{(1)}\right)\) of the SA function, which is the dependence required for the calculation (4) of the training signal. Although the two curves are not identical, they both match the experimental data for a broad range of neuron values generated from the random MVM, hence the setting is appropriate for training.

### All optical classification

After setting up the two-layer ONN, we perform end-to-end optical training and inference on classification tasks: distinguishing two classes of data points on a two-dimensional plane (Fig. 3). We implement a fully-connected feed-forward architecture, with three input neurons, five hidden layer neurons and two output neurons (Fig. 1). Two input neurons are used to encode the input data point coordinates \((x_{1},x_{2})\), and the third input neuron of constant value is used to set the first layer bias. The class label is encoded by a 'one-hot' vector \((0,1)\) or \((1,0)\), and we use categorical cross-entropy as the loss function.

We optically train the ONN on three 400-element datasets with different nonlinear boundary shapes, which we refer to as 'rings', 'XOR' and 'arches' [Fig. 3(a)]. Another 200 similar elements of each set are used for validation, i.e. to measure the loss and accuracy after each epoch of training. The test set consists of a uniform grid of equally-spaced \((x_{1},x_{2})\) values. The optical inference results for the test set are displayed in Fig. 3(a) by light purple and orange backgrounds, whereas the blue circles and orange triangles show the training set elements.

For all three datasets, each epoch consists of 20 minibatches, with a mini-batch size of 20, and we use the Adam optimizer to update the weights and biases from the gradients. We tune hyperparamters such as learning rate and number of epochs to maximise network performance. Table 1 summarises the network architecture and hyperparameters used for each dataset.

Figure 3(b) shows the optical training performance on the 'rings' dataset. We perform five repeated training runs, and plot the loss and accuracy for the validation set after each epoch of training. To visualise how the network is learning the boundary between the two classes, we also run a test dataset after each epoch. Examples of the network output after 1, 3, 6 and 10 epochs are shown. We see that the ONN quickly learns the nonlinear boundary and gradually improves the accuracy to 100%. This indicates a strong optical nonlinearity in the system and
a good gradient approximation in optical backpropagation. Details of the training procedure are provided in the following section.

\begin{table}
\begin{tabular}{|l|c|c|c|c|c|c|c|} \hline Dataset & \begin{tabular}{c} Input \\ neurons \\ \end{tabular} & \begin{tabular}{c} Hidden \\ neurons \\ \end{tabular} & \begin{tabular}{c} Output \\ neurons \\ \end{tabular} & \begin{tabular}{c} Learning \\ rate \\ \end{tabular} & Epochs & 
\begin{tabular}{c} Batches \\ per epoch \\ \end{tabular} & Batch size \\ \hline Rings & & & & & 0.01 & 16 & & \\ \hline XOR & 2 & 5 & 2 & 0.005 & 30 & 20 & 20 \\ \hline Arches & & & & 0.01 & 25 & & \\ \hline \end{tabular}
\end{table}
Table 1: **Summary of network architecture and hyperparameters used in optical and digital training.**

Figure 2: **Multi-layer ONN characterisation.****(a)** Scatter plots of measured against theory results for MVM-1 (first layer forwards), MVM-2a (second layer forwards) and MVM-2b (second layer backwards). All three MVM results are taken simultaneously. Histograms of the signal and noise error for each MVM are displayed underneath. **(b)** First-layer activations \(a_{\text{meas}}^{(1)}\) measured after the vapor cell, plotted against the theoretically expected linear MVM-1 output \(z_{\text{theory}}^{(1)}\) before the cell. The green line is a best fit curve of the theoretical SA nonlinear function. **(c)** The amplitude of a weak constant probe passed backwards through the vapor cell as a function of the pump \(z_{\text{theory}}^{(1)}\), with constant input probe. Measurements for both forward and backward beams are taken simultaneously.
Methods section, and results for the other two datasets in Supplementary Note 3.

To better understand the optical training process, we explore the evolution of the output neuron and error vector values in Fig. 3(c). First, we plot the mini-batch mean value of each output neuron, \(\bar{z}_{j}^{(2)}\), for the inputs from the two classes separately in the upper and lower panels, over the course of the training iterations. We see the output neuron values diverge in opposite ways for the two classes, such that the inputs can be distinguished and correctly classified.

Second, we similarly plot the evolution of the mini-batch mean output error, \(\bar{\delta}^{(2)}\), for each neuron. This is calculated as the difference between the network output vector \(a^{(2)}\) and the ground truth label \(y\), averaged over each mini-batch. As expected, we see the output errors converge towards zero as the system learns the correct boundary.

### Optical training vs _in-silico_ training

To demonstrate the optical training advantage, we perform _in-silico_ training of our ONN as a comparison. We digitally model our system with a neural network of the equivalent architecture, including identical learning rate, number of epochs and all other hyperparamters. The hidden layer nonlinearity and the associated gradient are given by the best fit curve and theoretical probe response of Fig. 2(b). The trained weights are subsequently used for inference with our ONN. The top and bottom rows in Fig. 3(a) plot the network output of the test boundary set, after the system has been trained optically and digitally, respectively, for all three datasets. In all cases, the optically trained network achieves almost perfect accuracy, whilst the digitally trained network is clearly not optimised, with the network prediction not matching the data. This is further evidence for the already well

Figure 3: **Optical training performance.****(a)** Decision boundary charts of the ONN inference output for three different classification tasks, after the ONN has been trained optically (top) or _in-silico_ (bottom). **(b)** Learning curves of the ONN for classification of the ‘rings’ dataset, showing mean and standard deviation of the validation loss and accuracy averaged over 5 repeated training runs. Shown above are decision boundary charts of the ONN output for the test set, after different epochs. **(c)** Evolution of output neuron values, and of output errors, for the training set inputs of the two classes.
documented advantages of hardware-in-the-loop training schemes.

## Discussion

Our optical training scheme is surprisingly simple and effective. It adds minimal computational overhead to the network, since it doesn't require _a priori_ simulation, or intricate mapping of network parameters to physical device settings. It also imposes minimal hardware complexity on the system, as it requires only a few additional beam splitters and detectors to measure the activation and error values for parameter updates.

Our scheme can be generalised and applied to many other analog neural networks with different physical implementations of the linear and nonlinear layers. We list a few examples in Table. 2. Common optical linear operations include MVM, diffraction and convolution. Compatible optical MVM examples include our free-space multiplier and photonic crossbar array [29], as they are both bidirectional, in the sense that optical field propagating backwards through these arrangements gets multiplied by the transpose of the weight matrix. Diffraction naturally works in both directions, hence diffractive neural networks constructed using different programmable amplitude and phase masks also satisfy the requirements [30]. Optical convolution, achieved with the Fourier transform by means of a lens, and mean pooling, achieved through an optical low pass filter, also work in both directions. Therefore, a convolutional neural network can be optically trained as well. Detailed analysis on the generalization to these linear layers can be found in Supplementary Note 4.

Regarding the generalization to other nonlinearity choices, the critical requirement is the ability to acquire gradients during backpropagation. Our pump-probe method is compatible with multiple types of optical nonlinearities: saturable absorption, saturable gain and intensity-dependent phase modulation [31]. Using saturable gain as the nonlinearity offers the added advantage of loss compensation in a deep network, and using intensity-dependent phase modulation nonlinearity, such as self-lensing, allows one to build complex-valued optical neural networks with potentially stronger learning capabilities [32; 26; 33].

In our ONN training implementation, some computational operations remain digital, specifically the calculation of the last layer error \(\delta^{(2)}\) and the outer product (3) between the activation and error vectors. Both these operations can be done optically if needed [26]. The error vector can in many cases be obtained by subtracting the ONN output from the label vector by way of destructive interference [12]. Interference can also be utilized to compute the outer product by fanning out the two vectors and overlapping them in a criss-cross fashion onto a pixelated photosensor.

Our optically trained ONN can be scaled up to improve computing performance. Previously, using a similar experimental setup, we have demonstrated an ONN with 100 neurons per layer and high multiplier accuracy [12], and 1000 neurons can be supported by commercial SLMs or liquid crystal displays. Optical encoders and detectors can work at speeds up to 100 GHz using off-the-shelf components, enabling ultra-fast end-to-end optical computing at low latency. Therefore computational speeds up to \(10^{17}\) operations per second are within reach, and our optical training method is compatible with this productivity rate.

## Methods

### Multilayer ONN

To construct the multi-layer ONN, we connect two optical multipliers in series. For the first layer (MVM-1), the input neuron vector \(x\) is encoded into the field amplitude of a coherent beam using a digital micromirror device (DMD), DMD-1. This is a binary amplitude modulator, and every physical pixel is a micromirror that can reflect at two angles representing 0 or 1. By grouping 128 physical pixels as a block, we are able to represent 7-bit positive-valued inputs on DMD-1, with the input value proportional to the number of binary pixels 'turned on' in each block.

Since MVM requires performing dot products of the input vector with every row of the matrix, we create multiple copies of the input vector on DMD-1, and image them onto the \(W^{(1)}\) matrix mask -- a phase-only liquid-crystal spatial light modulator (LC-SLM), SLM-1 -- for element-wise multiplication. The MVM-1 result \(z^{(1)}\) is obtained by summing the element-wise products using a cylindrical lens (first optical 'fan-in'), and passing the beam through a narrow adjustable slit to select the zero spatial frequency component. The weights in our ONN are real-valued, encoded by LC-SLMs with 8-bit resolution using a phase grating modulation method that enables arbitrary and accurate field control [34].

The beam next passes through a rubidium vapor cell to apply the activation function, such that immediately after the cell the beam encodes the hidden layer activation vector, \(a^{(1)}\). The beam continues to propagate and becomes the input for the second linear layer. Another cylindrical lens is used to expand the beam (first optical 'fan-out'), before modulation by the second weight matrix mask SLM-2. Finally, summation by a third cylindrical lens (second optical 'fan-in') completes the second MVM in the forward direction (MVM-2a), and the final beam profile encodes \(z^{(2)}\).

To read out the activation vectors required for the optical training, we insert beam splitters at the output of each MVM to tap-off a small portion of the beam. The real-valued vectors are measured by high-speed cameras, using coherent detection techniques detailed in Supplementary Note 2.
At the output layer of the ONN we use a digital softmax function to convert the output values into probabilities, and calculate the loss function and output error vector, which initiates the optical backpropagation.

### Optical backpropagation

The output error vector, \(\delta^{(2)}\) is encoded in the backward beam by using DMD-2 to modulate a beam obtained from the same laser as the forward propagating beam. The backward beam is introduced to the system through one of the arms of the beam splitter placed at the output of MVM-2a, and carefully aligned so as to overlap with the forward beam. SLM-2 performs element-wise multiplication by the transpose of the second weight matrix.

The cylindrical lens that performs 'fan-out' for the forward beam, performs 'fan-in' for the backward beam into a slit, completing the second layer backwards MVM (MVM-2b). Passing through the vapor cell modulates the beam by the derivative of the activation function, after which the beam encodes the hidden layer error vector \(\delta^{(1)}\). Another beam splitter and camera are used to tap off the backward beam and measure the result.

In practice, two halves of the same DMD act as DMD-1 and DMD-2, and a portion of SLM-1 is used to encode the sign of the error vector. A full experiment diagram is provided in Supplementary Note 1.

Each training iteration consists of optically measuring all of \(a^{(1)}\), \(z^{(2)}\) and \(\delta^{(1)}\). These vectors are used, along with the inputs \(x=a^{(0)}\), to calculate the weight gradients according to Eq. (3) and weight updates, which are then applied to the LC-SLMs. This process is repeated for all the mini-batches until the network converges.

### SA activation

The cell with atomic rubidium vapor is heated to 70 degrees by a simple heating jacket and temperature controller. The laser wavelength is locked to the \(D_{2}\) transition at 780nm.

The power of the forward propagating beam is adjusted to ensure the beam at the vapor cell is intense enough to saturate the absorption, whilst the maximum power of the backward propagating beam is attenuated to approximately 2% of the maximum forward beam power, to ensure a linear response when passing through the cell in the presence of a uniform pump.

In the experiment, the backward probe response does not match perfectly with the simple two-level atomic model, due to two factors.

First, the probe does not undergo 100% absorption even with the pump turnes off. Second, a strong pump beam causes the atoms to fluoresce in all directions, including along the backward probe path. Therefore, the backward signal has a background offset proportional to the forward signal. To compensate for these issues, three measurements are taken to determine the probe response \(\delta^{(1)}\) for each training iteration: pump only; probe only; and both pump and probe. In this way, the background terms due to pump fluorescence and unabsorbed probe could be negated.

**Acknowledgements** This work is supported by Innovate UK Smart Grant 10043476. X.G. acknowledges support from the Royal Commission for the Exhibition of 1851 Research Fellowship.

**Author contributions** X.G. and A.L. conceived the experiment. J.S. carried out the experiment and performed the data analysis. All the authors jointly prepared the manuscript. This work was done under the supervision of A.L.

**Competing interests** The authors declare no competing interests in this work.
