1+ """
2+ Restricted Boltzmann Machines (RBM)
3+ author: Ye Hu
4+ 2016/12/18
5+ """
6+ import timeit
7+ import numpy as np
8+ import tensorflow as tf
9+ from rbm import RBM
10+
11+
12+ class GBRBM (RBM ):
13+ """
14+ Gaussian-binary Restricted Boltzmann Machines
15+ Note we assume that the standard deviation is a constant (not training parameter)
16+ You better normalize you data with range of [0, 1.0].
17+ """
18+ def __init__ (self , inpt = None , n_visiable = 784 , n_hidden = 500 , sigma = 1.0 , W = None ,
19+ hbias = None , vbias = None , sample_visible = True ):
20+ """
21+ :param inpt: Tensor, the input tensor [None, n_visiable]
22+ :param n_visiable: int, number of visiable units
23+ :param n_hidden: int, number of hidden units
24+ :param sigma: float, the standard deviation (note we use the same σ for all visible units)
25+ :param W, hbias, vbias: Tensor, the parameters of RBM (tf.Variable)
26+ :param sample_visble: bool, if True, do gaussian sampling.
27+ """
28+ super (GBRBM , self ).__init__ (inpt , n_visiable , n_hidden , W , hbias , vbias )
29+ self .sigma = sigma
30+ self .sample_visible = sample_visible
31+
32+ @staticmethod
33+ def sample_gaussian (x , sigma ):
34+ return x + tf .random_normal (tf .shape (x ), mean = 0.0 , stddev = sigma )
35+
36+ def propdown (self , h ):
37+ """Compute the mean for visible units given hidden units"""
38+ return tf .matmul (h , tf .transpose (self .W )) + self .vbias
39+
40+ def sample_v_given_h (self , h0_sample ):
41+ """Sampling the visiable units given hidden sample"""
42+ v1_mean = self .propdown (h0_sample )
43+ v1_sample = v1_mean
44+ if self .sample_visible :
45+ v1_sample = GBRBM .sample_gaussian (v1_mean , self .sigma )
46+ return (v1_mean , v1_sample )
47+
48+ def propdown (self , h ):
49+ """Compute the sigmoid activation for visible units given hidden units"""
50+ return tf .nn .sigmoid (tf .matmul (h , tf .transpose (self .W )) / self .sigma ** 2 + self .vbias )
51+
52+ def free_energy (self , v_sample ):
53+ """Compute the free energy"""
54+ wx_b = tf .matmul (v_sample , self .W ) / self .sigma ** 2 + self .hbias
55+ vbias_term = tf .reduce_sum (0.5 * tf .square (v_sample - self .vbias ) / self .sigma ** 2 , axis = 1 )
56+ hidden_term = tf .reduce_sum (tf .log (1.0 + tf .exp (wx_b )), axis = 1 )
57+ return - hidden_term + vbias_term
58+
59+
60+ if __name__ == "__main__" :
61+ data = np .random .randn (1000 , 6 )
62+ x = tf .placeholder (tf .float32 , shape = [None , 6 ])
63+
64+ gbrbm = GBRBM (x , n_visiable = 6 , n_hidden = 5 )
65+
66+ learning_rate = 0.1
67+ k = 1
68+ batch_size = 20
69+ n_epochs = 10
70+
71+ cost = gbrbm .get_reconstruction_cost ()
72+ # Create the persistent variable
73+ #persistent_chain = tf.Variable(tf.zeros([batch_size, n_hidden]), dtype=tf.float32)
74+ persistent_chain = None
75+ train_ops = gbrbm .get_train_ops (learning_rate = learning_rate , k = 1 , persistent = persistent_chain )
76+ init = tf .global_variables_initializer ()
77+
78+ sess = tf .Session ()
79+ sess .run (init )
80+ for epoch in range (n_epochs ):
81+ avg_cost = 0.0
82+ for i in range (len (data )// batch_size ):
83+ sess .run (train_ops , feed_dict = {x : data [i * batch_size :(i + 1 )* batch_size ]})
84+ avg_cost += sess .run (cost , feed_dict = {x : data [i * batch_size :(i + 1 )* batch_size ]})/ batch_size
85+ print (avg_cost )
86+
87+ # test
88+ v = np .random .randn (10 , 6 )
89+ print (v )
90+
91+ preds = sess .run (gbrbm .reconstruct (x ), feed_dict = {x : v })
92+ print (preds )
93+
94+
0 commit comments