# sf.apps.train.KL¶

class KL(data, vgbs)[source]

Bases: object

Kullback-Liebler divergence cost function.

In a standard unsupervised learning scenario, data are assumed to be sampled from an unknown distribution and a common goal is to learn that distribution. Training of a model distribution can be performed by minimizing the Kullback-Leibler (KL) divergence, which up to additive constants can be written as:

$KL = -\frac{1}{T}\sum_S \log[P(S)],$

where $$S$$ is an element of the data, $$P(S)$$ is the probability of observing that element when sampling from the GBS distribution, and $$T$$ is the total number of elements in the data. For the GBS distribution in the WAW parametrization, the gradient of the KL divergence can be written as

$\partial_\theta KL(\theta) = - \sum_{k=1}^m\frac{1}{w_k}(\langle n_k\rangle_{\text{data}}- \langle n_k\rangle_{\text{GBS}})\partial_\theta w_k,$

where $$\langle n_k\rangle$$ denotes the average photon numbers in mode k. This class provides methods to compute gradients and evaluate the cost function.

Example usage

>>> embedding = train.embed.Exp(4)
>>> A = np.ones((4, 4))
>>> vgbs = train.VGBS(A, 3, embedding, threshold=True)
>>> params = np.array([0.05, 0.1, 0.02, 0.01])
>>> data = np.zeros((4, 4))
>>> kl = cost.KL(data, vgbs)
>>> kl.evaluate(params)
-0.2866830267216749
array([-0.52812574, -0.5201932 , -0.53282312, -0.53437824])

Parameters
• data (array) – Array of samples representing the training data

• vgbs (train.VGBS) – Variational GBS class

 evaluate(params) Computes the value of the Kullback-Liebler divergence cost function. grad(params) Calculates the gradient of the Kullback-Liebler cost function with respect to the trainable parameters
evaluate(params)[source]

Computes the value of the Kullback-Liebler divergence cost function.

Example usage

>>> kl.evaluate(params)
-0.2866830267216749

Parameters

params (array) – the trainable parameters $$\theta$$

Returns

the value of the cost function

Return type

float

grad(params)[source]

Calculates the gradient of the Kullback-Liebler cost function with respect to the trainable parameters

Example usage

>>> kl.grad(params)
array([-0.52812574, -0.5201932 , -0.53282312, -0.53437824])

Parameters

params (array[float]) – the trainable parameters $$\theta$$

Returns

the gradient of the KL cost function with respect to $$\theta$$

Return type

array