# Buggy code
def __init__(
    self,
    data: OutputData,
    X_data_mean: tf.Tensor,
    X_data_var: tf.Tensor,
    kernel: Kernel,
    num_inducing_variables: Optional[int] = None,
    inducing_variable=None,
    X_prior_mean=None,
    X_prior_var=None,
):
    """
    Initialise Bayesian GPLVM object. This method only works with a Gaussian likelihood.

    :param data: data matrix, size N (number of points) x D (dimensions)
    :param X_data_mean: initial latent positions, size N (number of points) x Q (latent dimensions).
    :param X_data_var: variance of latent positions ([N, Q]), for the initialisation of the latent space.
    :param kernel: kernel specification, by default Squared Exponential
    :param num_inducing_variables: number of inducing points, M
    :param inducing_variable: matrix of inducing points, size M (inducing points) x Q (latent dimensions). By default
        random permutation of X_data_mean.
    :param X_prior_mean: prior mean used in KL term of bound. By default 0. Same size as X_data_mean.
    :param X_prior_var: prior variance used in KL term of bound. By default 1.
    """
    num_data, num_latent_gps = X_data_mean.shape
    super().__init__(kernel, likelihoods.Gaussian(), num_latent_gps=num_latent_gps)
    self.data = data
    assert X_data_var.ndim == 2

    self.X_data_mean = Parameter(X_data_mean)
    self.X_data_var = Parameter(X_data_var, transform=positive())

    self.num_data = num_data
    self.output_dim = data.shape[-1]

    assert np.all(X_data_mean.shape == X_data_var.shape)
    assert X_data_mean.shape[0] == data.shape[0], "X mean and Y must be same size."
    assert X_data_var.shape[0] == data.shape[0], "X var and Y must be same size."

    if (inducing_variable is None) == (num_inducing_variables is None):
        raise ValueError(
            "BayesianGPLVM needs exactly one of `inducing_variable` and `num_inducing_variables`"
        )

    if inducing_variable is None:
        # By default we initialize by subset of initial latent points
        # Note that tf.random.shuffle returns a copy, it does not shuffle in-place
        Z = tf.random.shuffle(X_data_mean)[:num_inducing_variables]
        inducing_variable = InducingPoints(Z)

    self.inducing_variable = inducingpoint_wrapper(inducing_variable)

    assert X_data_mean.shape[1] == self.num_latent_gps

    # deal with parameters for the prior mean variance of X
    if X_prior_mean is None:
        X_prior_mean = tf.zeros(
            (self.num_data, self.num_latent_gps), dtype=default_float()
        )
    if X_prior_var is None:
        X_prior_var = tf.ones((self.num_data, self.num_latent_gps))

    self.X_prior_mean = tf.convert_to_tensor(
        np.atleast_1d(X_prior_mean), dtype=default_float()
    )
    self.X_prior_var = tf.convert_to_tensor(
        np.atleast_1d(X_prior_var), dtype=default_float()
    )

    assert self.X_prior_mean.shape[0] == self.num_data
    assert self.X_prior_mean.shape[1] == self.num_latent_gps
    assert self.X_prior_var.shape[0] == self.num_data
    assert self.X_prior_var.shape[1] == self.num_latent_gps


# Traceback (ValueError)
Traceback (most recent call last):
File "main.py", line 177, in <module>
main(args)
File "main.py", line 64, in main
build_allele(args)
File "/path/to/1_model_sim/drivers.py", line 226, in build_allele
opt_model_list(m)
File "/path/to/1_model_sim/model.py", line 355, in opt_model_list
m.trainable_variables)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/optimizers/scipy.py", line 73, in minimize
func, initial_params, jac=True, method=method, **scipy_kwargs
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 610, in minimize
callback=callback, **options)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/scipy/optimize/lbfgsb.py", line 345, in _minimize_lbfgsb
f, g = func_and_grad(x)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/scipy/optimize/lbfgsb.py", line 295, in func_and_grad
f = fun(x, *args)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 327, in function_wrapper
return function(*(wrapper_args + args))
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 65, in __call__
fg = self.fun(x, *args)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/optimizers/scipy.py", line 95, in _eval
loss, grad = _tf_eval(tf.convert_to_tensor(x))
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 568, in __call__
result = self._call(*args, **kwds)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 615, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 497, in _initialize
*args, **kwds))
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 439, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/optimizers/scipy.py:88 _tf_eval  *
loss, grads = _compute_loss_and_gradients(closure, variables)
/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/optimizers/scipy.py:145 _compute_loss_and_gradients  *
loss = loss_closure()
/path/to/1_model_sim/model.py:354 None  *
opt.minimize(lambda: - m.log_marginal_likelihood(),
/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/models/gpr.py:75 log_marginal_likelihood  *
log_prob = multivariate_normal(Y, m, L)
/path/to/1_model_sim/venv/lib/python3.6/site-packages/gpflow/logdensities.py:95 multivariate_normal  *
d = x - mu
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/ops/math_ops.py:927 r_binary_op_wrapper
x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name="x")
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1314 convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_conversion_registry.py:52 _default_conversion_function
return constant_op.constant(value, dtype, name=name)
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py:258 constant
allow_broadcast=True)
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py:296 _constant_impl
allow_broadcast=allow_broadcast))
/path/to/1_model_sim/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_util.py:522 make_tensor_proto
"Cannot create a tensor proto whose content is larger than 2GB.")

ValueError: Cannot create a tensor proto whose content is larger than 2GB.

# Fixed code
def __init__(
    self,
    data: OutputData,
    X_data_mean: tf.Tensor,
    X_data_var: tf.Tensor,
    kernel: Kernel,
    num_inducing_variables: Optional[int] = None,
    inducing_variable=None,
    X_prior_mean=None,
    X_prior_var=None,
):
    """
    Initialise Bayesian GPLVM object. This method only works with a Gaussian likelihood.

    :param data: data matrix, size N (number of points) x D (dimensions)
    :param X_data_mean: initial latent positions, size N (number of points) x Q (latent dimensions).
    :param X_data_var: variance of latent positions ([N, Q]), for the initialisation of the latent space.
    :param kernel: kernel specification, by default Squared Exponential
    :param num_inducing_variables: number of inducing points, M
    :param inducing_variable: matrix of inducing points, size M (inducing points) x Q (latent dimensions). By default
        random permutation of X_data_mean.
    :param X_prior_mean: prior mean used in KL term of bound. By default 0. Same size as X_data_mean.
    :param X_prior_var: prior variance used in KL term of bound. By default 1.
    """
    num_data, num_latent_gps = X_data_mean.shape
    super().__init__(kernel, likelihoods.Gaussian(), num_latent_gps=num_latent_gps)
    self.data = data_input_to_tensor(data)
    assert X_data_var.ndim == 2

    self.X_data_mean = Parameter(X_data_mean)
    self.X_data_var = Parameter(X_data_var, transform=positive())

    self.num_data = num_data
    self.output_dim = self.data.shape[-1]

    assert np.all(X_data_mean.shape == X_data_var.shape)
    assert X_data_mean.shape[0] == self.data.shape[0], "X mean and Y must be same size."
    assert X_data_var.shape[0] == self.data.shape[0], "X var and Y must be same size."

    if (inducing_variable is None) == (num_inducing_variables is None):
        raise ValueError(
            "BayesianGPLVM needs exactly one of `inducing_variable` and `num_inducing_variables`"
        )

    if inducing_variable is None:
        # By default we initialize by subset of initial latent points
        # Note that tf.random.shuffle returns a copy, it does not shuffle in-place
        Z = tf.random.shuffle(X_data_mean)[:num_inducing_variables]
        inducing_variable = InducingPoints(Z)

    self.inducing_variable = inducingpoint_wrapper(inducing_variable)

    assert X_data_mean.shape[1] == self.num_latent_gps

    # deal with parameters for the prior mean variance of X
    if X_prior_mean is None:
        X_prior_mean = tf.zeros(
            (self.num_data, self.num_latent_gps), dtype=default_float()
        )
    if X_prior_var is None:
        X_prior_var = tf.ones((self.num_data, self.num_latent_gps))

    self.X_prior_mean = tf.convert_to_tensor(
        np.atleast_1d(X_prior_mean), dtype=default_float()
    )
    self.X_prior_var = tf.convert_to_tensor(
        np.atleast_1d(X_prior_var), dtype=default_float()
    )

    assert self.X_prior_mean.shape[0] == self.num_data
    assert self.X_prior_mean.shape[1] == self.num_latent_gps
    assert self.X_prior_var.shape[0] == self.num_data
    assert self.X_prior_var.shape[1] == self.num_latent_gps