I have been trying to save and load a Tensorflow distribution. There don't seem to be any good examples out there on how to do this.
Specifically I have been trying to save a PixelCNN++ distribution which inherits from tfp.distributions.Distribution. A minimum working example can be found below (this is taken from the PixelCNN documentation).
# Build a small Pixel CNN++ model to train on MNIST.
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_probability as tfp
tfd = tfp.distributions
tfk = tf.keras
tfkl = tf.keras.layers
tf.enable_v2_behavior()
# Load MNIST from tensorflow_datasets
data = tfds.load('mnist')
train_data, test_data = data['train'], data['test']
def image_preprocess(x):
x['image'] = tf.cast(x['image'], tf.float32)
return (x['image'],) # (input, output) of the model
batch_size = 16
train_it = train_data.map(image_preprocess).batch(batch_size).shuffle(1000)
image_shape = (28, 28, 1)
# Define a Pixel CNN network
dist = tfd.PixelCNN(
image_shape=image_shape,
num_resnet=1,
num_hierarchies=2,
num_filters=32,
num_logistic_mix=5,
dropout_p=.3,
)
# Define the model input
image_input = tfkl.Input(shape=image_shape)
# Define the log likelihood for the loss fn
log_prob = dist.log_prob(image_input)
# Define the model
model = tfk.Model(inputs=image_input, outputs=log_prob)
model.add_loss(-tf.reduce_mean(log_prob))
# Compile and train the model
model.compile(
optimizer=tfk.optimizers.Adam(.001),
metrics=[])
model.fit(train_it, epochs=10, verbose=True)
# sample five images from the trained model
samples = dist.sample(5)
How do I save the distribution? Or instead, how do I extract the distribution from the model (which can be easily saved) in order to sample from it?