@@ -61,17 +61,17 @@ def contrastive_divergence(self, lr=0.1, k=1, input=None):
61
61
self .input = input
62
62
63
63
''' CD-k '''
64
- pre_sigmoid_ph , ph_mean , ph_sample = self .sample_h_given_v (self .input )
64
+ ph_mean , ph_sample = self .sample_h_given_v (self .input )
65
65
66
66
chain_start = ph_sample
67
67
68
68
for step in xrange (k ):
69
69
if step == 0 :
70
- pre_sigmoid_nvs , nv_means , nv_samples ,\
71
- pre_sigmoid_nhs , nh_means , nh_samples = self .gibbs_hvh (chain_start )
70
+ nv_means , nv_samples ,\
71
+ nh_means , nh_samples = self .gibbs_hvh (chain_start )
72
72
else :
73
- pre_sigmoid_nvs , nv_means , nv_samples ,\
74
- pre_sigmoid_nhs , nh_means , nh_samples = self .gibbs_hvh (nh_samples )
73
+ nv_means , nv_samples ,\
74
+ nh_means , nh_samples = self .gibbs_hvh (nh_samples )
75
75
76
76
# chain_end = nv_samples
77
77
@@ -86,37 +86,37 @@ def contrastive_divergence(self, lr=0.1, k=1, input=None):
86
86
87
87
88
88
def sample_h_given_v (self , v0_sample ):
89
- pre_sigmoid_h1 , h1_mean = self .propup (v0_sample )
89
+ h1_mean = self .propup (v0_sample )
90
90
h1_sample = self .numpy_rng .binomial (size = h1_mean .shape , # discrete: binomial
91
91
n = 1 ,
92
92
p = h1_mean )
93
93
94
- return [pre_sigmoid_h1 , h1_mean , h1_sample ]
94
+ return [h1_mean , h1_sample ]
95
95
96
96
97
97
def sample_v_given_h (self , h0_sample ):
98
- pre_sigmoid_v1 , v1_mean = self .propdown (h0_sample )
98
+ v1_mean = self .propdown (h0_sample )
99
99
v1_sample = self .numpy_rng .binomial (size = v1_mean .shape , # discrete: binomial
100
100
n = 1 ,
101
101
p = v1_mean )
102
102
103
- return [pre_sigmoid_v1 , v1_mean , v1_sample ]
103
+ return [v1_mean , v1_sample ]
104
104
105
105
def propup (self , v ):
106
106
pre_sigmoid_activation = numpy .dot (v , self .W ) + self .hbias
107
- return [ pre_sigmoid_activation , sigmoid (pre_sigmoid_activation )]
107
+ return sigmoid (pre_sigmoid_activation )
108
108
109
109
def propdown (self , h ):
110
110
pre_sigmoid_activation = numpy .dot (h , self .W .T ) + self .vbias
111
- return [ pre_sigmoid_activation , sigmoid (pre_sigmoid_activation )]
111
+ return sigmoid (pre_sigmoid_activation )
112
112
113
113
114
114
def gibbs_hvh (self , h0_sample ):
115
- pre_sigmoid_v1 , v1_mean , v1_sample = self .sample_v_given_h (h0_sample )
116
- pre_sigmoid_h1 , h1_mean , h1_sample = self .sample_h_given_v (v1_sample )
115
+ v1_mean , v1_sample = self .sample_v_given_h (h0_sample )
116
+ h1_mean , h1_sample = self .sample_h_given_v (v1_sample )
117
117
118
- return [pre_sigmoid_v1 , v1_mean , v1_sample ,
119
- pre_sigmoid_h1 , h1_mean , h1_sample ]
118
+ return [v1_mean , v1_sample ,
119
+ h1_mean , h1_sample ]
120
120
121
121
122
122
def get_reconstruction_cross_entropy (self ):
@@ -139,6 +139,35 @@ def reconstruct(self, v):
139
139
return reconstructed_v
140
140
141
141
142
+ '''
143
+ RBM w/ continuous-valued inputs (Linear Energy)
144
+ '''
145
+ class CRBM (RBM ):
146
+ def propdown (self , h ):
147
+ pre_activation = numpy .dot (h , self .W .T ) + self .vbias
148
+ return pre_activation
149
+
150
+
151
+
152
+ def sample_v_given_h (self , h0_sample ):
153
+ a_h = self .propdown (h0_sample )
154
+ en = numpy .exp (- a_h )
155
+ ep = numpy .exp (a_h )
156
+
157
+ v1_mean = 1 / (1 - en ) - 1 / a_h
158
+ U = numpy .array (self .numpy_rng .uniform (
159
+ low = 0 ,
160
+ high = 1 ,
161
+ size = v1_mean .shape ))
162
+
163
+ v1_sample = numpy .log ((1 - U * (1 - ep ))) / a_h
164
+
165
+
166
+ return [v1_mean , v1_sample ]
167
+
168
+
169
+
170
+
142
171
def test_rbm (learning_rate = 0.1 , k = 1 , training_epochs = 1000 ):
143
172
data = numpy .array ([[1 ,1 ,1 ,0 ,0 ,0 ],
144
173
[1 ,0 ,1 ,0 ,0 ,0 ],
@@ -148,7 +177,6 @@ def test_rbm(learning_rate=0.1, k=1, training_epochs=1000):
148
177
[0 ,0 ,1 ,1 ,1 ,0 ]])
149
178
150
179
151
-
152
180
rng = numpy .random .RandomState (123 )
153
181
154
182
# construct RBM
@@ -168,5 +196,35 @@ def test_rbm(learning_rate=0.1, k=1, training_epochs=1000):
168
196
print rbm .reconstruct (v )
169
197
170
198
199
+
200
+ def test_crbm (learning_rate = 0.1 , k = 1 , training_epochs = 1000 ):
201
+ data = numpy .array ([[0.4 , 0.5 , 0.5 , 0. , 0. , 0. ],
202
+ [0.5 , 0.3 , 0.5 , 0. , 0. , 0. ],
203
+ [0.4 , 0.5 , 0.5 , 0. , 0. , 0. ],
204
+ [0. , 0. , 0.5 , 0.3 , 0.5 , 0. ],
205
+ [0. , 0. , 0.5 , 0.4 , 0.5 , 0. ],
206
+ [0. , 0. , 0.5 , 0.5 , 0.5 , 0. ]])
207
+
208
+
209
+ rng = numpy .random .RandomState (123 )
210
+
211
+ # construct CRBM
212
+ rbm = CRBM (input = data , n_visible = 6 , n_hidden = 5 , numpy_rng = rng )
213
+
214
+ # train
215
+ for epoch in xrange (training_epochs ):
216
+ rbm .contrastive_divergence (lr = learning_rate , k = k )
217
+ cost = rbm .get_reconstruction_cross_entropy ()
218
+ print >> sys .stderr , 'Training epoch %d, cost is ' % epoch , cost
219
+
220
+
221
+ # test
222
+ v = numpy .array ([[0.5 , 0.5 , 0. , 0. , 0. , 0. ],
223
+ [0. , 0. , 0. , 0.5 , 0.5 , 0. ]])
224
+
225
+ print rbm .reconstruct (v )
226
+
227
+
171
228
if __name__ == "__main__" :
172
- test_rbm ()
229
+ # test_rbm()
230
+ test_crbm ()
0 commit comments