How to do it...

This recipe covers the steps for the evaluation of weights obtained from an RBM:

  1. Run the following code to generate the image of 400 hidden nodes:
uw = t(weights[[length(weights)]])   # Extract the most recent weight matrix 
numXpatches = 20 # Number of images in X-axis (user input)
numYpatches=20 # Number of images in Y-axis (user input)
pixels <- list()
op <- par(no.readonly = TRUE)
par(mfrow = c(numXpatches,numYpatches), mar = c(0.2, 0.2, 0.2, 0.2), oma = c(3, 3, 3, 3))
for (i in 1:(numXpatches*numYpatches)) {
denom <- sqrt(sum(uw[i, ]^2))
pixels[[i]] <- matrix(uw[i, ]/denom, nrow = numYpatches, ncol = numXpatches)
image(pixels[[i]], axes = F, col = gray((0:32)/32))
}
par(op)
  1. Select a sample of four actual input digits from the training data:
sample_image <- trainX[1:4,]
  1. Then, visualize these sample digits using the following code:
mw=melt(sample_image) 
mw$X3=floor((mw$X2-1)/28)+1
mw$X2=(mw$X2-1)%%28 + 1;
mw$X3=29-mw$X3
ggplot(data=mw)+geom_tile(aes(X2,X3,fill=value))+facet_wrap(~X1,nrow=2)+
scale_fill_continuous(low='black',high='white')+coord_fixed(ratio=1)+
labs(x=NULL,y=NULL,)+
theme(legend.position="none")+
theme(plot.title = element_text(hjust = 0.5))
  1. Now, reconstruct these four sample images using the final weights and biases obtained:
hh0 = tf$nn$sigmoid(tf$matmul(X, W) + hb) 
vv1 = tf$nn$sigmoid(tf$matmul(hh0, tf$transpose(W)) + vb)
feed = sess$run(hh0, feed_dict=dict( X= sample_image, W= prv_w, hb= prv_hb))
rec = sess$run(vv1, feed_dict=dict( hh0= feed, W= prv_w, vb= prv_vb))
  1. Then, visualize the reconstructed sample digits using the following code:
mw=melt(rec) 
mw$X3=floor((mw$X2-1)/28)+1
mw$X2=(mw$X2-1)%%28 + 1
mw$X3=29-mw$X3
ggplot(data=mw)+geom_tile(aes(X2,X3,fill=value))+facet_wrap(~X1,nrow=2)+
scale_fill_continuous(low='black',high='white')+coord_fixed(ratio=1)+
labs(x=NULL,y=NULL,)+
theme(legend.position="none")+
theme(plot.title = element_text(hjust = 0.5))
..................Content has been hidden....................

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