code: def test_stochastic_gradients(
      indices_1: Sequence[int],
      indices_2: Sequence[int],
      num_data1: int,
      num_data2: int,
      max_iter: int,
  ) -> None:
      """
      In response to bug #281, we need to make sure stochastic update
      happens correctly in tf optimizer mode.
      To do this compare stochastic updates with deterministic updates
      that should be equivalent.
  
      Data term in svgp likelihood is
      \sum_{i=1^N}E_{q(i)}[\log p(y_i | f_i )
  
      This sum is then approximated with an unbiased minibatch estimate.
      In this test we substitute a deterministic analogue of the batchs
      sampler for which we can predict the effects of different updates.
      """
      X: AnyNDArray
      Y: AnyNDArray
      X, Y = (
          np.atleast_2d(np.array([0.0, 1.0])).T,
          np.atleast_2d(np.array([-1.0, 3.0])).T,
      )
      Z: AnyNDArray = np.atleast_2d(np.array([0.5]))
  
      def get_model(num_data: int) -> SVGP:
          return SVGP(
              kernel=gpflow.kernels.SquaredExponential(),
              num_data=num_data,
              likelihood=gpflow.likelihoods.Gaussian(),
              inducing_variable=Z,
          )
  
      def training_loop(
          indices: Sequence[int], num_data: int, max_iter: int
      ) -> SVGP:
          model = get_model(num_data)
          opt = tf.optimizers.SGD(learning_rate=0.001)
          data = X[indices], Y[indices]
          for _ in range(max_iter):
              with tf.GradientTape() as tape:
                  loss = model.training_loss(data)
              grads = tape.gradient(loss, model.trainable_variables)
              opt.apply_gradients(zip(grads, model.trainable_variables))
          return model
  
      model_1 = training_loop(
          indices_1, num_data=num_data1, max_iter=max_iter
      )
      model_2 = training_loop(
          indices_2, num_data=num_data2, max_iter=max_iter
      )
      assert _check_models_close(model_1, model_2)
  
file_path: GPflow/tests/gpflow/models/test_svgp.py
imports: from typing import Sequence
  import numpy
  import pytest
  import tensorflow
  from numpy.testing import assert_allclose
  import gpflow
  from gpflow import set_trainable
  from gpflow.base import AnyNDArray
  from gpflow.models import SVGP
label_desc: Use image node_id 5 for calling a global function with example usage: test_stochastic_gradients(indices_1, indices_2, num_data1, num_data2, max_iter) and returns: SVGP, model