Creating an RBM using the TensorFlow Keras layers API
Explore how to implement a Restricted Boltzmann Machine (RBM) with TensorFlow 2's Keras layers API. Understand constructing custom layers, applying contrastive divergence, and training the RBM to generate digit images from the MNIST dataset. Gain hands-on experience leveraging TensorFlow's gradient tape and efficient sampling methods to optimize model training and visualize learned features.
We'll cover the following...
Now that we have an appreciation of some of the theoretical underpinnings of the RBM, let's learn how we can implement it using the TensorFlow 2 library. For this purpose, we’ll represent the RBM as a custom layer type using the Keras layers API. We’ll implement the whole model in code, showing how we can leverage TensorFlow 2’s gradient tape functionality to implement CD as a custom learning algorithm.
Implementing the RBM
Firstly, we extend tf.keras.layer:
We input a number of hidden units, visible units, a learning rate for CD updates, and the number of steps to take with each CD pass. For the layers API, we are only required to implement two functions: build() and call(). build() is executed when we call model.compile(), and is used to initialize the weights of the network, including inferring the right size of the weights given the input dimensions:
We also need a way to perform both forward and reverse samples from the model. For the forward pass, we need to compute sigmoidal activations from the input, and then stochastically turn the hidden units on or off based on the activation probability between
Likewise, we need a way to sample in reverse for the visible units:
We also implement call() in the RBM class, which provides the forward pass we would use if we were to use the fit() method of the ...