tensorflow / tensorflow/probability

JointDistributionSequential cannot be serialized via tf.train.Checkpoint

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

I'm using tensorflow version 2.11.0 and tensorflow_probability 0.19.0.

The following code succeeds:

import tensorflow as tf
import tensorflow_probability as tfp

class MyModule(tf.Module):
    def __init__(self):
        self.dist = tfp.distributions.Normal(0.0, 1.0)
        
my_module = MyModule()

checkpoint = tf.train.Checkpoint(my_module)
checkpoint.save("checkpoint")

but the following code fails:

import tensorflow as tf
import tensorflow_probability as tfp

class MyModule(tf.Module):
    def __init__(self):
        self.dist = tfp.distributions.JointDistributionSequential(
            [
                tfp.distributions.Normal(0.0, 1.0),
                tfp.distributions.Normal(0.0, 1.0),
            ]
        )
        
my_module = MyModule()

checkpoint = tf.train.Checkpoint(my_module)
checkpoint.save("checkpoint")

with the error:

ValueError: Unable to save the object {-1: ListWrapper([<tfp.distributions.Normal 'Normal' batch_shape=[] event_shape=[] dtype=float32>, <tfp.distributions.Normal 'Normal' batch_shape=[] event_shape=[] dtype=float32>])} (a dictionary wrapper constructed automatically on attribute assignment). The wrapped dictionary contains a non-string key which maps to a trackable object or mutable data structure.

If you don't need this dictionary checkpointed, wrap it in a non-trackable object; it will be subsequently ignored.

It seems to be an issue with the JointDistribution._single_sample_distributions dict having int keys. If I add

my_module.dist._single_sample_distributions = {}

before checkpointing then it succeeds.

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

Reproduce the failure with the TensorFlow 2.11.0 and TensorFlow Probability 0.19.0 examples, then inspect JointDistribution._single_sample_distributions and its integer-keyed dictionary during tf.train.Checkpoint.save. Done means JointDistributionSequential can be checkpointed without manually replacing that dictionary, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
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.