tensorflow / tensorflow/probability

MultiTaskGaussianProcessRegressionModel issue with tf.function

Open
#1,981 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hi, I have encountered an error when using tf.function decorator on a function calling MultiTaskGaussianProcessRegressionModel from the experimental package. The error occurs specifically when passing input_signature of unknown dimension to tf.function. Below are the details. Thanks!

Code
import tensorflow as tf
import tensorflow_probability as tfp

tfde = tfp.experimental.distributions
tfk = tfp.math.psd_kernels
tfke = tfp.experimental.psd_kernels

base_kernel = tfk.ExponentiatedQuadratic(
    amplitude=tf.convert_to_tensor(0.6, tf.float64),
    length_scale=tf.convert_to_tensor(0.5, tf.float64),
)
kernel = tfke.Independent(num_tasks=2, base_kernel=base_kernel)
observations = tf.constant([[0., 1.],[-0.5, -1.0]], tf.float64)
observation_index_points = tf.constant([[0],[1.0]], tf.float64)

@tf.function(input_signature=[tf.TensorSpec(shape=[None, 1], dtype=tf.float64)])
def predict(index_points):
    gp = tfde.MultiTaskGaussianProcessRegressionModel(
        kernel=kernel,
        observations=observations,
        observation_index_points=observation_index_points,
        index_points=index_points,
    )
    return gp.mean()

predict(tf.constant([[0.2],[0.4],[0.6]], tf.float64))

The code above works:

  • if we remove tf.function, or
  • if we remove input_signature inside with tf.function

We get the following error if we set shape=[None, 1] inside tf.TensorSpec:

UnboundLocalError: in user code:

    File "/tmp/ipykernel_299029/1064227357.py", line 24, in predict  *
        return gp.mean()
    File "/usr/.local/lib/python3.12/site-packages/tensorflow_probability/python/distributions/distribution.py", line 1536, in mean  **
        return self._mean(**kwargs)
    File "/usr/.local/lib/python3.12/site-packages/tensorflow_probability/python/experimental/distributions/multitask_gaussian_process_regression_model.py", line 852, in _mean
        self._get_flattened_marginal_distribution(
    File "/usr/.local/lib/python3.12/site-packages/tensorflow_probability/python/experimental/distributions/multitask_gaussian_process_regression_model.py", line 831, in _get_flattened_marginal_distribution
        covariance = self._compute_flattened_covariance(index_points)
    File "/usr/.local/lib/python3.12/site-packages/tensorflow_probability/python/experimental/distributions/multitask_gaussian_process_regression_model.py", line 811, in _compute_flattened_covariance
        cholinv_kzx = observation_scale.solve(kxz, adjoint_arg=True)

    UnboundLocalError: cannot access local variable 'dim' where it is not associated with a value

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with tf.function and input_signature shape [None, 1]. Inspect tensorflow_probability/python/experimental/distributions/multitask_gaussian_process_regression_model.py, especially _compute_flattened_covariance and the reported dim handling, then verify that the example no longer raises UnboundLocalError for unknown dimensions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.