tensorflow / tensorflow/probability

Keras not accepting character `/` from build_factored_surrogate_posterior

Open
#1,799 4 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

Given this brief code:

import pandas as pd
import tensorflow_probability as tfp
import tensorflow as tf


data = pd.read_csv('https://raw.githubusercontent.com/WillianFuks/tfcausalimpact/master/tests/fixtures/arma_data.csv')[['y']].astype('float32')
data.index = pd.date_range(start='2024-01-01', periods=len(data), freq='D')
obs = data.iloc[:70]
model = tfp.sts.Sum([tfp.sts.LocalLevel(observed_time_series=obs)])

optimizer = tf.optimizers.Adam(learning_rate=0.1)
variational_steps = 200
variational_posteriors = tfp.sts.build_factored_surrogate_posterior(model=model)

@tf.function()
def _run_vi():
    tfp.vi.fit_surrogate_posterior(
        target_log_prob_fn=model.joint_log_prob(
            observed_time_series=obs
        ),
        surrogate_posterior=variational_posteriors,
        optimizer=optimizer,
        num_steps=variational_steps
    )
    samples = variational_posteriors.sample(100)
    return samples, None
samples, _ = _run_vi()

Its execution raises:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-24-6998a2ed04ce>](https://localhost:8080/#) in <cell line: 22>()
     20     samples = variational_posteriors.sample(100)
     21     return samples, None
---> 22 samples, _ = _run_vi()
     23 

12 frames
[/usr/local/lib/python3.10/dist-packages/keras/src/backend/common/variables.py](https://localhost:8080/#) in __init__(self, initializer, shape, dtype, trainable, name)
     17         name = name or auto_name(self.__class__.__name__)
     18         if not isinstance(name, str) or "/" in name:
---> 19             raise ValueError(
     20                 "Argument `name` must be a string and "
     21                 "cannot contain character `/`. "

ValueError: in user code:

    File "<ipython-input-21-6998a2ed04ce>", line 10, in _run_vi  *
        tfp.vi.fit_surrogate_posterior(
    File "/usr/local/lib/python3.10/dist-packages/tensorflow_probability/python/vi/optimization.py", line 724, in fit_surrogate_posterior  **
        return minimize(
    File "/usr/local/lib/python3.10/dist-packages/tensorflow_probability/python/math/minimize.py", line 616, in minimize
        _, traced_values = _minimize_common(
    File "/usr/local/lib/python3.10/dist-packages/tensorflow_probability/python/math/minimize.py", line 156, in _minimize_common
        initial_optimizer_state) = optimizer_step_fn(
    File "/usr/local/lib/python3.10/dist-packages/tensorflow_probability/python/math/minimize.py", line 435, in optimizer_step
        train_op = optimizer.apply_gradients(zip(grads, watched_variables))
    File "/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/base_optimizer.py", line 269, in apply_gradients
        self.apply(grads, trainable_variables)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/base_optimizer.py", line 308, in apply
        self.build(trainable_variables)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/adam.py", line 93, in build
        self.add_variable_from_reference(
    File "/usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/optimizer.py", line 33, in add_variable_from_reference
        return super().add_variable_from_reference(
    File "/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/base_optimizer.py", line 205, in add_variable_from_reference
        return self.add_variable(
    File "/usr/local/lib/python3.10/dist-packages/keras/src/optimizers/base_optimizer.py", line 184, in add_variable
        variable = backend.Variable(
    File "/usr/local/lib/python3.10/dist-packages/keras/src/backend/common/variables.py", line 19, in __init__
        raise ValueError(

    ValueError: Argument `name` must be a string and cannot contain character `/`. Received: name=build_factored_surrogate_posterior/loc_0_momentum

Apparently the integration with Keras raised conflicts on the naming patterns used in tfp.

Versions used:
tensorflow==2.16.1
tensorflow-probability[tf]==0.24.0
tf-keras==2.16.0

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 Colab example with the listed TensorFlow, TensorFlow Probability, and tf-keras versions. Trace the optimizer path through tensorflow_probability/python/vi/optimization.py and the Keras optimizer files shown in the traceback; done means the example runs without rejecting the generated name containing /.

Written by the indexing model from the issue text.

Assessment

Tech stack
keras, python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.