Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorFlow Skipping loop optimization warning #779

Open
Thomas-Christie opened this issue Aug 13, 2023 · 1 comment
Open

TensorFlow Skipping loop optimization warning #779

Thomas-Christie opened this issue Aug 13, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Thomas-Christie
Copy link

Describe the bug
Hi, thanks for the excellent library. I have been using the library for a while now and have found that whenever I'm dealing with fitting GaussianProcessRegression models TensorFlow gives me the following warning:

W tensorflow/core/grappler/optimizers/loop_optimizer.cc:907] Skipping loop optimization for Merge node with control input: cond/branch_executed/_9

This seems to occur when the models are getting fitted by the BayesianOptimizer (https://github.com/secondmind-labs/trieste/blob/develop/trieste/bayesian_optimizer.py#L751C5-L755).

I just wanted to double check that this is unavoidable, and not something to do with my setup?

As a side note - the expected improvement notebook also doesn't seem to work in its current state; running the BO loop gives the following error:

NotImplementedError: Failed to save the optimization state. Some models do not support deecopying or serialization and cannot be saved. (This is particularly common for deep neural network models, though some of the model wrappers accept a model closure as a workaround.) For these models, the track_state`` argument of the :meth:~trieste.bayesian_optimizer.BayesianOptimizer.optimize method should be set to `False`. This means that only the final model will be available.`

To reproduce
Steps to reproduce the behaviour:

Behaviour can be reproduced by running the expected improvement notebook. Alternatively, you can run the following (which has been copied from the notebook):

import tensorflow as tf
from trieste.objectives import ScaledBranin
from trieste.experimental.plotting import plot_function_plotly
from trieste.space import Box
import trieste
import gpflow
import tensorflow_probability as tfp

from trieste.models.gpflow import GaussianProcessRegression

if __name__ == '__main__':
    np.random.seed(1793)
    tf.random.set_seed(1793)

    scaled_branin = ScaledBranin.objective
    search_space = ScaledBranin.search_space  # predefined search space
    search_space = Box([0, 0], [1, 1])  # define the search space directly

    observer = trieste.objectives.utils.mk_observer(scaled_branin)

    num_initial_points = 5
    initial_query_points = search_space.sample_sobol(num_initial_points)
    initial_data = observer(initial_query_points)

    def build_model(data):
        variance = tf.math.reduce_variance(data.observations)
        kernel = gpflow.kernels.Matern52(variance=variance, lengthscales=[0.2, 0.2])
        prior_scale = tf.cast(1.0, dtype=tf.float64)
        kernel.variance.prior = tfp.distributions.LogNormal(
            tf.cast(-2.0, dtype=tf.float64), prior_scale
        )
        kernel.lengthscales.prior = tfp.distributions.LogNormal(
            tf.math.log(kernel.lengthscales), prior_scale
        )
        gpr = gpflow.models.GPR(data.astuple(), kernel, noise_variance=1e-5)
        gpflow.set_trainable(gpr.likelihood, False)

        return GaussianProcessRegression(gpr, num_kernel_samples=100)

    model = build_model(initial_data)

    bo = trieste.bayesian_optimizer.BayesianOptimizer(observer, search_space)

    num_steps = 15
    result = bo.optimize(num_steps, initial_data, model, track_state=False)
    dataset = result.try_get_final_dataset()```

**System information**
 - OS: macOS 13.4.1
 - Python version: 3.10.10
 - Trieste version: Latest commit (https://github.com/secondmind-labs/trieste/commit/56101c092f28ed87398c4cd63fdece2f16909451)
 - TensorFlow version: 2.12.1
 - GPflow version: 2.9.0
@Thomas-Christie Thomas-Christie added the bug Something isn't working label Aug 13, 2023
@uri-granta
Copy link
Collaborator

Thanks for reporting this and sorry that it somehow fell between the gaps and was ignored for so long!

I've not managed to reproduce the warning with your sample script, which ran to completion (I tensorflow/core/kernels/logging_ops.cc:171] Optimization completed without errors) without that warning, suggesting it's either been fixed or is perhaps connected to running on macOS. There is definitely nothing that looks wrong with the script, and TF is often trigger happy with its warnings, so if the results are as expected I would recommend ignoring it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants