How to do it...

This section provides the steps for optimizing the error using reconstruction from an RBM:

  1. Initialize the current and previous vector of biases and matrices of weights:
cur_w = tf$Variable(tf$zeros(shape = shape(num_input, num_hidden), dtype=tf$float32)) 
cur_vb = tf$Variable(tf$zeros(shape = shape(num_input), dtype=tf$float32))
cur_hb = tf$Variable(tf$zeros(shape = shape(num_hidden), dtype=tf$float32))
prv_w = tf$Variable(tf$random_normal(shape=shape(num_input, num_hidden), stddev=0.01, dtype=tf$float32))
prv_vb = tf$Variable(tf$zeros(shape = shape(num_input), dtype=tf$float32))
prv_hb = tf$Variable(tf$zeros(shape = shape(num_hidden), dtype=tf$float32))
  1. Start a new TensorFlow session:
sess$run(tf$global_variables_initializer()) 
  1. Perform a first run with the full input data (trainX) and obtain the first set of weight matrix and bias vectors:
output <- sess$run(list(update_w, update_vb, update_hb), feed_dict = dict(X=trainX, 
W = prv_w$eval(),
vb = prv_vb$eval(),
hb = prv_hb$eval()))
prv_w <- output[[1]]
prv_vb <-output[[2]]
prv_hb <-output[[3]]
  1. Let's look at the error of the first run:
sess$run(err, feed_dict=dict(X= trainX, W= prv_w, vb= prv_vb, hb= prv_hb)) 
  1. The full model for the RBM can be trained using the following script:
epochs=15 
errors <- list()
weights <- list()
u=1
for(ep in 1:epochs){
for(i in seq(0,(dim(trainX)[1]-100),100)){
batchX <- trainX[(i+1):(i+100),]
output <- sess$run(list(update_w, update_vb, update_hb), feed_dict = dict(X=batchX,
W = prv_w,
vb = prv_vb,
hb = prv_hb))
prv_w <- output[[1]]
prv_vb <- output[[2]]
prv_hb <- output[[3]]
if(i%%10000 == 0){
errors[[u]] <- sess$run(err, feed_dict=dict(X= trainX, W= prv_w, vb= prv_vb, hb= prv_hb))
weights[[u]] <- output[[1]]
u <- u+1
cat(i , " : ")
}
}
cat("epoch :", ep, " : reconstruction error : ", errors[length(errors)][[1]]," ")
}
  1. Plot reconstruction using mean squared errors:
error_vec <- unlist(errors)
plot(error_vec,xlab="# of batches",ylab="mean squared reconstruction error",main="RBM-Reconstruction MSE plot")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset