Creating an RBM using the TensorFlow Keras layers API
Now that you have an appreciation of some of the theoretical underpinnings of the RBM, let's look at how we can implement it using the TensorFlow 2.0 library. For this purpose, we will represent the RBM as a custom layer type using the Keras layers API.
Code in this chapter was adapted to TensorFlow 2 from the original Theano (another deep learning Python framework) code from deeplearning.net.
Firstly, we extend tf.keras.layer
:
from tensorflow.keras import layers
import tensorflow_probability as tfp
class RBM(layers.Layer):
def __init__(self, number_hidden_units=10, number_visible_units=None, learning_rate=0.1, cd_steps=1):
super().__init__()
self.number_hidden_units = number_hidden_units
self.number_visible_units = number_visible_units
self.learning_rate = learning_rate
self.cd_steps = cd_steps
We input a number of hidden units, visible units, a...