<jupyter_start><jupyter_text>Natural gradientsThis notebook shows some basic usage of the natural gradient optimizer, both on its own and in combination with Adam optimizer.<jupyter_code>import warnings
import numpy as np
import gpflow
import tensorflow as tf

from gpflow.ci_utils import ci_niter, ci_range
from gpflow.models import VGP, GPR, SGPR, SVGP
from gpflow.optimizers import NaturalGradient
from gpflow.optimizers.natgrad import XiSqrtMeanVar

%matplotlib inline
%precision 4

np.random.seed(0)
tf.random.set_seed(0)

N, D = 100, 2
batch_size = 50

# inducing points
M = 10

x = np.random.uniform(size=(N, D))
y = np.sin(10 * x)

data = (x, y)
inducing_variable = tf.random.uniform((M, D))
adam_learning_rate = 0.01
iterations = ci_niter(5)<jupyter_output><empty_output><jupyter_text>VGP is a GPR The following section demonstrates how natural gradients can turn VGP into GPR *in a single step, if the likelihood is Gaussian*. Let's start by first creating a standard GPR model with Gaussian likelihood:<jupyter_code>gpr = GPR(data, kernel=gpflow.kernels.Matern52())<jupyter_output><empty_output><jupyter_text>The likelihood of the exact GP model is:<jupyter_code>gpr.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>Now we will create an approximate model which approximates the true posterior via a variational Gaussian distribution.We initialize the distribution to be zero mean and unit variance.<jupyter_code>vgp = VGP(data, kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Gaussian())<jupyter_output><empty_output><jupyter_text>The likelihood of the approximate GP model is:<jupyter_code>vgp.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>Obviously, our initial guess for the variational distribution is not correct, which results in a lower bound to the likelihood of the exact GPR model. We can optimize the variational parameters in order to get a tighter bound.  In fact, we only need to take **one step** in the natural gradient direction to recover the exact posterior:<jupyter_code>natgrad_opt = NaturalGradient(gamma=1.0)
variational_params = [(vgp.q_mu, vgp.q_sqrt)]
natgrad_opt.minimize(lambda: - vgp.log_marginal_likelihood(), var_list=variational_params)<jupyter_output><empty_output><jupyter_text>The likelihood of the approximate GP model after a single NatGrad step:<jupyter_code>vgp.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>Optimize both variational parameters and kernel hyperparameters togetherIn the Gaussian likelihood case we can iterate between an Adam update for the hyperparameters and a NatGrad update for the variational parameters. That way, we achieve optimization of hyperparameters as if the model were a GPR. The trick is to forbid Adam from updating the variational parameters by setting them to not trainable.<jupyter_code># Stop Adam from optimizing the variational parameters
vgp.q_mu.trainable = False
vgp.q_sqrt.trainable = False

adam_opt_for_vgp = tf.optimizers.Adam(adam_learning_rate)
adam_opt_for_gpr = tf.optimizers.Adam(adam_learning_rate)
for i in range(iterations):
    adam_opt_for_gpr.minimize(
        lambda: - gpr.log_marginal_likelihood(), 
        var_list=gpr.trainable_variables)
    likelihood = gpr.log_likelihood()
    tf.print(f'GPR with Adam: iteration {i + 1} likelihood {likelihood:.04f}')
for i in range(iterations):
    adam_opt_for_vgp.minimize(
        lambda: - vgp.log_marginal_likelihood(), 
        var_list=vgp.trainable_variables)
    natgrad_opt.minimize(
        lambda: - vgp.log_marginal_likelihood(), 
        var_list=variational_params)
    likelihood = vgp.log_likelihood()
    tf.print(f'VGP with NaturalGradient and Adam: iteration {i + 1} likelihood {likelihood:.04f}')<jupyter_output>VGP with NaturalGradient and Adam: iteration 1 likelihood -230.6707
VGP with NaturalGradient and Adam: iteration 2 likelihood -230.2508
VGP with NaturalGradient and Adam: iteration 3 likelihood -229.8304
VGP with NaturalGradient and Adam: iteration 4 likelihood -229.4093
VGP with NaturalGradient and Adam: iteration 5 likelihood -228.9877<jupyter_text>Compare GPR and VGP lengthscales after optimization:<jupyter_code>print(f'GPR lengthscales = {gpr.kernel.lengthscale.numpy():.04f}')
print(f'VGP lengthscales = {vgp.kernel.lengthscale.numpy():.04f}')<jupyter_output>GPR lengthscales = 0.9686
VGP lengthscales = 0.9686<jupyter_text>Natural gradients also work for the sparse modelSimilarly, natural gradients turn SVGP into SGPR in the Gaussian likelihood case. We can again combine natural gradients with Adam to update both variational parameters and hyperparameters too.Here we'll just do a single natural step demonstration.<jupyter_code>svgp = SVGP(kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Gaussian(), inducing_variable=inducing_variable)
sgpr = SGPR(data, kernel=gpflow.kernels.Matern52(), inducing_variable=inducing_variable)

for model in svgp, sgpr:
    model.likelihood.variance.assign(0.1)<jupyter_output><empty_output><jupyter_text>Analytically optimal sparse model likelihood:<jupyter_code>sgpr.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>SVGP likelihood before natural gradient step:<jupyter_code>svgp.log_likelihood(data).numpy()
variational_params = [(svgp.q_mu, svgp.q_sqrt)]

def svgp_loss_cb():
    return - svgp.log_marginal_likelihood(data)

natgrad_opt = NaturalGradient(gamma=1.0)
natgrad_opt.minimize(svgp_loss_cb, var_list=variational_params)<jupyter_output><empty_output><jupyter_text>SVGP likelihood after a single natural gradient step:<jupyter_code>svgp.log_likelihood(data).numpy()<jupyter_output><empty_output><jupyter_text>MinibatchesA crucial property of the natural gradient method is that it still works with minibatches.In practice though, we need to use a smaller gamma.<jupyter_code>natgrad_opt = NaturalGradient(gamma=0.1)

data_minibatch = tf.data.Dataset.from_tensor_slices(data).prefetch(N).repeat().shuffle(N).batch(batch_size)
data_minibatch_it = iter(data_minibatch)

def svgp_stochastic_loss_cb() -> tf.Tensor:
    batch = next(data_minibatch_it)
    return - svgp.log_marginal_likelihood(batch)

for _ in range(ci_niter(100)):
    natgrad_opt.minimize(svgp_stochastic_loss_cb, var_list=variational_params)<jupyter_output><empty_output><jupyter_text>Minibatch SVGP likelihood after NatGrad optimization:<jupyter_code>np.average([svgp.log_likelihood(next(data_minibatch_it)) for _ in ci_range(100)])<jupyter_output><empty_output><jupyter_text>Comparison with ordinary gradients in the conjugate case (Take home message: natural gradients are always better)Compared to SVGP with ordinary gradients with minibatches, the natural gradient optimizer is much faster in the Gaussian case. Here we'll do hyperparameter learning together with optimization of the variational parameters, comparing the interleaved natural gradient approach and the one using ordinary gradients for the hyperparameters and variational parameters jointly.**NOTE:** Again we need to compromise for smaller gamma value, which we'll keep *fixed* during the optimization.<jupyter_code>svgp_ordinary = SVGP(kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Gaussian(), inducing_variable=inducing_variable)
svgp_natgrad = SVGP(kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Gaussian(), inducing_variable=inducing_variable)

# ordinary gradients with Adam for SVGP
ordinary_adam_opt = tf.optimizers.Adam(adam_learning_rate)

# NatGrads and Adam for SVGP
# Stop Adam from optimizing the variational parameters
svgp_natgrad.q_mu.trainable = False
svgp_natgrad.q_sqrt.trainable = False

# Create the optimize_tensors for SVGP
natgrad_adam_opt = tf.optimizers.Adam(adam_learning_rate)

natgrad_opt = NaturalGradient(gamma=0.1)
variational_params = [(svgp_natgrad.q_mu, svgp_natgrad.q_sqrt)]<jupyter_output><empty_output><jupyter_text>Let's optimize the models:<jupyter_code>data_minibatch = tf.data.Dataset.from_tensor_slices(data).prefetch(N).repeat().shuffle(N).batch(batch_size)
data_minibatch_it = iter(data_minibatch)


def svgp_ordinary_loss_cb() -> tf.Tensor:
    batch = next(data_minibatch_it)
    return - svgp_ordinary.log_marginal_likelihood(batch)


def svgp_natgrad_loss_cb() -> tf.Tensor:
    batch = next(data_minibatch_it)
    return - svgp_natgrad.log_marginal_likelihood(batch)


for _ in range(ci_niter(100)):
    ordinary_adam_opt.minimize(svgp_ordinary_loss_cb, var_list=svgp_ordinary.trainable_variables)


for _ in range(ci_niter(100)):
    natgrad_adam_opt.minimize(svgp_natgrad_loss_cb, var_list=svgp_natgrad.trainable_variables)
    natgrad_opt.minimize(svgp_natgrad_loss_cb, var_list=variational_params)<jupyter_output><empty_output><jupyter_text>SVGP likelihood after ordinary `Adam` optimization:<jupyter_code>np.average([svgp_ordinary.log_likelihood(next(data_minibatch_it)) for _ in ci_range(100)])<jupyter_output><empty_output><jupyter_text>SVGP likelihood after `NaturalGradient` and `Adam` optimization:<jupyter_code>np.average([svgp_natgrad.log_likelihood(next(data_minibatch_it)) for _ in ci_range(100)])<jupyter_output><empty_output><jupyter_text>Comparison with ordinary gradients in the non-conjugate case Binary classification (Take home message: natural gradients are usually better)We can use natural gradients even when the likelihood isn't Gaussian. It isn't guaranteed to be better, but it usually is better in practical situations.<jupyter_code>y_binary = np.random.choice([1., -1], size=x.shape)
vgp_data = (x, y_binary)

vgp_bernoulli = VGP(vgp_data, kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Bernoulli())
vgp_bernoulli_natgrad = VGP(vgp_data, kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Bernoulli())

# ordinary gradients with Adam for VGP with Bernoulli likelihood
adam_opt = tf.optimizers.Adam(adam_learning_rate)

# NatGrads and Adam for VGP with Bernoulli likelihood
# Stop Adam from optimizing the variational parameters
vgp_bernoulli_natgrad.q_mu.trainable = False
vgp_bernoulli_natgrad.q_sqrt.trainable = False

# Create the optimize_tensors for VGP with natural gradients
natgrad_adam_opt = tf.optimizers.Adam(adam_learning_rate)
natgrad_opt = NaturalGradient(gamma=0.1)
variational_params = [(vgp_bernoulli_natgrad.q_mu, vgp_bernoulli_natgrad.q_sqrt)]
# Optimize vgp_bernoulli
for _ in range(ci_niter(100)):
    adam_opt.minimize(
        lambda: - vgp_bernoulli.log_marginal_likelihood(),
        var_list=vgp_bernoulli.trainable_variables)

# Optimize vgp_bernoulli_natgrad
for _ in range(ci_niter(100)):
    adam_opt.minimize(
        lambda: - vgp_bernoulli_natgrad.log_marginal_likelihood(),  
        var_list=vgp_bernoulli_natgrad.trainable_variables)
    natgrad_opt.minimize(
            lambda: - vgp_bernoulli_natgrad.log_marginal_likelihood(),
            var_list=variational_params)<jupyter_output><empty_output><jupyter_text>VGP likelihood after ordinary `Adam` optimization:<jupyter_code>vgp_bernoulli.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>VGP likelihood after `NaturalGradient` + `Adam` optimization:<jupyter_code>vgp_bernoulli_natgrad.log_likelihood().numpy()<jupyter_output><empty_output><jupyter_text>We can also choose to run natural gradients in another parameterization.The sensible choice is the model parameters (q_mu, q_sqrt), which is already in GPflow.<jupyter_code>vgp_bernoulli_natgrads_xi = VGP(vgp_data, kernel=gpflow.kernels.Matern52(), likelihood=gpflow.likelihoods.Bernoulli())

# Stop Adam from optimizing the variational parameters
vgp_bernoulli_natgrads_xi.q_mu.trainable = False
vgp_bernoulli_natgrads_xi.q_sqrt.trainable = False

# Create the optimize_tensors for VGP with Bernoulli likelihood
adam_opt = tf.optimizers.Adam(adam_learning_rate)
natgrad_opt = NaturalGradient(gamma=0.01)

variational_params = [(vgp_bernoulli_natgrads_xi.q_mu, vgp_bernoulli_natgrads_xi.q_sqrt, XiSqrtMeanVar())]
# Optimize vgp_bernoulli_natgrads_xi
for _ in range(ci_niter(100)):
    adam_opt.minimize(
        lambda: - vgp_bernoulli_natgrads_xi.log_marginal_likelihood(),                
        var_list=vgp_bernoulli_natgrads_xi.trainable_variables)

    natgrad_opt.minimize(
        lambda: - vgp_bernoulli_natgrads_xi.log_marginal_likelihood(),
        var_list=variational_params)<jupyter_output>WARNING: Logging before flag parsing goes to stderr.
W1021 21:53:24.205371 4725601728 backprop.py:980] Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.<jupyter_text>VGP likelihood after `NaturalGradient` with `XiSqrtMeanVar` + `Adam` optimization:<jupyter_code>vgp_bernoulli_natgrads_xi.log_likelihood().numpy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0828324857451071, 'fraction_numerical': 0.012944983818770227, 'mean_word_length': 6.9479485609308025, 'pattern_counts': {'":': 0, '<': 94, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '385016', 'license': 'Apache-2.0', 'n_tokens_mistral': 4495, 'n_tokens_neox': 4179, 'n_words': 993, 'path': 'doc/source/notebooks/advanced/natural_gradients.ipynb', 'repo_name': 'BracketJohn/GPflow', 'size': '12978'}