Created
July 31, 2020 15:51
-
-
Save khanhnamle1994/efdcd24a368b56734abc7435e3e8d690 to your computer and use it in GitHub Desktop.
Explainable RBM model architecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rbm(movies_df): | |
""" | |
Implement RBM architecture in TensorFlow | |
:param movies_df: data frame that stores movies information | |
:return: variables to be used during TensorFlow training | |
""" | |
hiddenUnits = 100 # Number of hidden layers | |
visibleUnits = len(movies_df) # Number of visible layers | |
# Create respective placeholder variables for storing visible and hidden layer biases and weights | |
vb = tf.placeholder("float", [visibleUnits]) # Number of unique movies | |
hb = tf.placeholder("float", [hiddenUnits]) # Number of features | |
W = tf.placeholder("float", [visibleUnits, hiddenUnits]) # Weights that connect the hidden and visible layers | |
# Pre-process the input data | |
v0 = tf.placeholder("float", [None, visibleUnits]) | |
_h0 = tf.nn.sigmoid(tf.matmul(v0, W) + hb) | |
h0 = tf.nn.relu(tf.sign(_h0 - tf.random_uniform(tf.shape(_h0)))) | |
# Reconstruct the pre-processed input data (Sigmoid and ReLU activation functions are used) | |
_v1 = tf.nn.sigmoid(tf.matmul(h0, tf.transpose(W)) + vb) | |
v1 = tf.nn.relu(tf.sign(_v1 - tf.random_uniform(tf.shape(_v1)))) | |
h1 = tf.nn.sigmoid(tf.matmul(v1, W) + hb) | |
# Set RBM training parameters | |
alpha = 0.1 # Set learning rate | |
w_pos_grad = tf.matmul(tf.transpose(v0), h0) # Set positive gradients | |
w_neg_grad = tf.matmul(tf.transpose(v1), h1) # Set negative gradients | |
# Calculate contrastive divergence to maximize | |
CD = (w_pos_grad - w_neg_grad) / tf.to_float(tf.shape(v0)[0]) | |
# Create methods to update the weights and biases | |
update_w = W + alpha * CD | |
update_vb = vb + alpha * tf.reduce_mean(v0 - v1, 0) | |
update_hb = hb + alpha * tf.reduce_mean(h0 - h1, 0) | |
# Set error function (RMSE) | |
err = v0 - v1 | |
err_sum = tf.sqrt(tf.reduce_mean(err * err)) | |
# Initialize variables | |
cur_w = np.zeros([visibleUnits, hiddenUnits], np.float32) # Current weight | |
cur_vb = np.zeros([visibleUnits], np.float32) # Current visible unit biases | |
cur_hb = np.zeros([hiddenUnits], np.float32) # Current hidden unit biases | |
prv_w = np.zeros([visibleUnits, hiddenUnits], np.float32) # Previous weight | |
prv_vb = np.zeros([visibleUnits], np.float32) # Previous visible unit biases | |
prv_hb = np.zeros([hiddenUnits], np.float32) # Previous hidden unit biases | |
return v0, W, vb, hb, update_w, prv_w, prv_vb, prv_hb, update_vb, update_hb, cur_w, cur_vb, cur_hb, err_sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment