pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

# Generate the sample probabilitydef sample_prob(self, probs):return tf.nn.relu(tf.sign(probs - tf.random.uniform(tf.shape(probs))))We will need functions to reconstruct the input:Chapter 10def rbm_reconstruct(self,X):h = tf.nn.sigmoid(tf.matmul(X, self.w) + self.hb)reconstruct = tf.nn.sigmoid(tf.matmul(h, tf.transpose(self.w)) +self.vb)return reconstructTo train the RBM created we define the train() function. The function calculatesthe positive and negative grad term of contrastive divergence and uses the weightupdate equation to update the weights and biases:# Training method for the modeldef train(self, X, epochs=10):loss = []for epoch in range(epochs):#For each step/batchfor start, end in zip(range(0, len(X), self.batchsize),range(self.batchsize,len(X), self.batchsize)):batch = X[start:end]#Initialize with sample probabilitiesself.hb))self.vb))h0 = self.sample_prob(self.prob_h_given_v(batch, self.w,v1 = self.sample_prob(self.prob_v_given_h(h0, self.w,h1 = self.prob_h_given_v(v1, self.w, self.hb)#Create the Gradientspositive_grad = tf.matmul(tf.transpose(batch), h0)negative_grad = tf.matmul(tf.transpose(v1), h1)#Update learning ratesself.w = self.w + self.learning_rate *(positive_grad -negative_grad) / tf.dtypes.cast(tf.shape(batch)[0],tf.float32)self.vb = self.vb + self.learning_rate * tf.reduce_mean(batch - v1, 0)self.hb = self.hb + self.learning_rate * tf.reduce_mean(h0 - h1, 0)[ 395 ]

Unsupervised Learning#Find the error rateerr = tf.reduce_mean(tf.square(batch - v1))print ('Epoch: %d' % epoch,'reconstruction error: %f' % err)loss.append(err)return lossNow that our class is ready, we instantiate an object of RBM and train it on the MNISTdataset:(train_data, _), (test_data, _) = tf.keras.datasets.mnist.load_data()train_data = train_data/np.float32(255)train_data = np.reshape(train_data, (train_data.shape[0], 784))test_data = test_data/np.float32(255)test_data = np.reshape(test_data, (test_data.shape[0], 784))#Size of inputs is the number of inputs in the training setinput_size = train_data.shape[1]rbm = RBM(input_size, 200)err = rbm.train(train_data,50)In the following code, you can see the learning curve of our RBM:plt.plot(err)plt.xlabel('epochs')plt.ylabel('cost')Figure 8: Learning curve for the RBM model[ 396 ]

# Generate the sample probability

def sample_prob(self, probs):

return tf.nn.relu(tf.sign(probs - tf.random.uniform(tf.

shape(probs))))

We will need functions to reconstruct the input:

Chapter 10

def rbm_reconstruct(self,X):

h = tf.nn.sigmoid(tf.matmul(X, self.w) + self.hb)

reconstruct = tf.nn.sigmoid(tf.matmul(h, tf.transpose(self.w)) +

self.vb)

return reconstruct

To train the RBM created we define the train() function. The function calculates

the positive and negative grad term of contrastive divergence and uses the weight

update equation to update the weights and biases:

# Training method for the model

def train(self, X, epochs=10):

loss = []

for epoch in range(epochs):

#For each step/batch

for start, end in zip(range(0, len(X), self.

batchsize),range(self.batchsize,len(X), self.batchsize)):

batch = X[start:end]

#Initialize with sample probabilities

self.hb))

self.vb))

h0 = self.sample_prob(self.prob_h_given_v(batch, self.w,

v1 = self.sample_prob(self.prob_v_given_h(h0, self.w,

h1 = self.prob_h_given_v(v1, self.w, self.hb)

#Create the Gradients

positive_grad = tf.matmul(tf.transpose(batch), h0)

negative_grad = tf.matmul(tf.transpose(v1), h1)

#Update learning rates

self.w = self.w + self.learning_rate *(positive_grad -

negative_grad) / tf.dtypes.cast(tf.shape(batch)[0],tf.float32)

self.vb = self.vb + self.learning_rate * tf.reduce_

mean(batch - v1, 0)

self.hb = self.hb + self.learning_rate * tf.reduce_

mean(h0 - h1, 0)

[ 395 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!