tensorflow / tensorflow/probability

tf.keras and tfp.tfp.layers. are not compatible

Open
#1,829 1 comment 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

hello fellow developers, it appears that the tf.keras and tfp.tfp.layers. are not compatible
i have this code="
**num_inducing_points = 40
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[1], dtype=x.dtype),
tf.keras.layers.Dense(1, kernel_initializer='ones', use_bias=False),
tfp.layers.VariationalGaussianProcess(
num_inducing_points=num_inducing_points,
kernel_provider=RBFKernelFn(dtype=x.dtype),
event_shape=[1],
inducing_index_points_initializer=tf.constant_initializer(
np.linspace(*x_range, num=num_inducing_points,
dtype=x.dtype)[..., np.newaxis]),
unconstrained_observation_noise_variance_initializer=(
tf.constant_initializer(
np.log(np.expm1(1.)).astype(x.dtype))),
),
])

//Do inference.
batch_size = 32
loss = lambda y, rv_y: rv_y.variational_loss(
y, kl_weight=np.array(batch_size, x.dtype) / x.shape[0])
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01), loss=loss)
model.fit(x, y, batch_size=batch_size, epochs=1000, verbose=False)

//Make predictions.
yhats = [model(x_tst) for _ in range(100)]**
"

and i get the following
error output=
"**:7: UserWarning: layer.add_variable is deprecated and will be removed in a future version. Please use the layer.add_weight() method instead.
self._amplitude = self.add_variable(
:12: UserWarning: layer.add_variable is deprecated and will be removed in a future version. Please use the layer.add_weight() method instead.
self._length_scale = self.add_variable(


ValueError Traceback (most recent call last)

in <cell line: 2>()
1 num_inducing_points = 40
----> 2 model = tf.keras.Sequential([
3 tf.keras.layers.InputLayer(input_shape=[1], dtype=x.dtype),
4 tf.keras.layers.Dense(1, kernel_initializer='ones', use_bias=False),
5 tfp.layers.VariationalGaussianProcess(

1 frames

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in add(self, layer, rebuild)
93 layer = origin_layer
94 if not isinstance(layer, Layer):
---> 95 raise ValueError(
96 "Only instances of keras.Layer can be "
97 f"added to a Sequential model. Received: {layer} "

ValueError: Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_probability.python.layers.distribution_layer.VariationalGaussianProcess object at 0x7acd5e77fc70> (of type <class 'tensorflow_probability.python.layers.distribution_layer.VariationalGaussianProcess'>)**"

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 provided notebook example that adds tfp.layers.VariationalGaussianProcess to a tf.keras.Sequential model, including the displayed ValueError. Check the layer and Keras compatibility involved; done means the example can construct the model without the incompatibility error.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.