-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbm_train.py
182 lines (165 loc) · 7.12 KB
/
rbm_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#function for training the DBN
import matplotlib.pyplot as plt
import numpy as np
import time
import function as func
import rbm_visualize as rbmv
#RBM
def sample_rbm_forward(visible, c, w):
return np.where(np.random.rand(w.shape[0],visible.shape[1]) < func.sigmoid(np.tile(c,(1,visible.shape[1]))+w.dot(visible)), 1, -1)
def sample_rbm_backward(hidden, c, w):
return np.where(np.random.rand(w.shape[1],hidden.shape[1]) < func.sigmoid(np.tile(c,(1,hidden.shape[1]))+np.transpose(w).dot(hidden)), 1, -1)
def sample_grbm_backward(hidden, b, w):
return np.random.normal(np.tile(b,(1,hidden.shape[1]))+np.transpose(w).dot(hidden), 0.01)#np.where(np.random.rand(w.shape[1],hidden.shape[1]) < sigmoid(np.tile(b,(1,hidden.shape[1]))+np.transpose(w).dot(hidden)), 0, 1) #this is no more sampling!!!!!!!!!!!!!
def backandforw(hidden, b, c, w, k, mode):#propagate backward and forward between 2 layers k times
for i in range(k):
if mode==0:
visible_k=sample_grbm_backward(hidden, b, w)
if mode==1:
visible_k=sample_rbm_backward(hidden, b, w)
hidden=sample_rbm_forward(visible_k, c, w)
return hidden, visible_k
def spatial_actualise_weight(visible, b, c, w, k, epsilon, alpha, mode, Wt):#proceed to contrastive divergence and update weigts
hidden=sample_rbm_forward(visible, c, w)
hidden_c, visible_c=backandforw(hidden, b, c, w, k, mode[0])
w+=(1/(visible.shape[1]))*epsilon*(hidden.dot(np.transpose(visible))-hidden_c.dot(np.transpose(visible_c)))*Wt
b+=(1/(visible.shape[1]))*epsilon*(np.sum(visible-visible_c, axis=1)).reshape(visible.shape[0],1)
c+=(1/(visible.shape[1]))*epsilon*(np.sum(hidden-hidden_c, axis=1)).reshape(w.shape[0],1)
if mode[1]!=0:#sparsity
cwv=c+w.dot(visible)
dsigm=func.dsigmoid(cwv)
q=((2*mode[1]-1)-(1/visible.shape[1])*(2*np.sum(func.sigmoid(cwv), axis=1)-1)).reshape(w.shape[0],1)
#q=q*np.abs(np.power(q,2))
w+=epsilon*alpha*q*(1/visible.shape[1])*(dsigm.dot(np.transpose(visible)))*Wt
c+=epsilon*alpha*q*(1/visible.shape[1])*(np.sum(dsigm, axis=1).reshape(w.shape[0],1))
def actualise_weight(visible, b, c, w, k, epsilon, alpha, mode):#proceed to contrastive divergence and update weigts
hidden=sample_rbm_forward(visible, c, w)
hidden_c, visible_c=backandforw(hidden, b, c, w, k, mode[0])
w+=(1/(visible.shape[1]))*epsilon*(hidden.dot(np.transpose(visible))-hidden_c.dot(np.transpose(visible_c)))
b+=(1/(visible.shape[1]))*epsilon*(np.sum(visible-visible_c, axis=1)).reshape(visible.shape[0],1)
c+=(1/(visible.shape[1]))*epsilon*(np.sum(hidden-hidden_c, axis=1)).reshape(w.shape[0],1)
if mode[1]!=0:#sparsity
cwv=c+w.dot(visible)
dsigm=func.dsigmoid(cwv)
q=((2*mode[1]-1)-(2*np.mean(func.sigmoid(cwv), axis=1)-1)).reshape(w.shape[0],1)
w+=epsilon*alpha*q*(1/visible.shape[1])*(dsigm.dot(np.transpose(visible)))
c+=epsilon*alpha*q*(1/visible.shape[1])*(np.sum(dsigm, axis=1).reshape(w.shape[0],1))
def train_rbm(visible, b, c, w, iterr_rbm, mode, epsilon, alpha, x_test, dataset_size, cd, f):
ind=[]
e=[]
E=[]
fe=[]
d=0
g=0
t1=0
t2=0
iterr_rbm=iterr_rbm//cd
for i in range(iterr_rbm):
visible_batch=visible[:,d:d+32]
#visible_batch=visible_batch.reshape(visible.shape[0],1)
g+=1
if g==32:
d+=32
g=0
if d>dataset_size-34:
d=0
actualise_weight(visible_batch, b, c, w, cd, epsilon, alpha, mode)
if i%(iterr_rbm//20)==0:
ind.append(i)
if mode[0]==0:
tt=time.time()
e.append(rbmv.energy_grbm(x_test, b, c, w))
E.append(rbmv.energy_grbm(visible, b, c, w))
t1+=time.time()-tt
tt=time.time()
fe.append(rbmv.pseudo_likelihood_grbm(x_test, b, c, w, f))
#fE.append(rbmv.pseudo_likelihood_grbm(visible, b, c, w, 3))
t2+=time.time()-tt
else:
tt=time.time()
e.append(rbmv.energy_rbm(x_test, b, c, w))
E.append(rbmv.energy_rbm(visible, b, c, w))
t1+=time.time()-tt
tt=time.time()
fe.append(rbmv.pseudo_likelihood_rbm(x_test, b, c, w, f))
#fE.append(rbmv.pseudo_likelihood_rbm(visible, b, c, w, 3))
t2+=time.time()-tt
plt.hist(np.mean(func.sigmoid(c+w.dot(visible)), axis=1), bins=30)
plt.title("mean neurons activation")
plt.legend()
plt.show()
plt.plot(ind, e,label="test set")
plt.plot(ind, E, label="training set")
plt.legend(loc='upper right')
if mode[0] == 1:
plt.yscale('symlog')
plt.show()
plt.plot(ind, fe, label="free energy test set")
#plt.plot(ind, fE, label="free energy training set")
plt.legend(loc='lower right')
plt.show()
print("free-energy", fe[len(fe)-1])
print("energy time and pseudo likelihood time ", t1, t2)
return [ind,fe]
def train_spatial_rbm(visible, b, c, w, iterr_rbm, mode, epsilon, alpha, x_test, dataset_size, cd, f, Wt):
ind=[]
e=[]
E=[]
fe=[]
er=[]
d=0
g=0
t1=0
t2=0
iterr_rbm=iterr_rbm//cd
for i in range(iterr_rbm):
visible_batch=visible[:,d:d+32]
#visible_batch=visible_batch.reshape(visible.shape[0],1)
g+=1
if g==32:
d+=32
g=0
if d>dataset_size-34:
d=0
spatial_actualise_weight(visible_batch, b, c, w, cd, epsilon, alpha, mode, Wt)
if i%(iterr_rbm//20)==0:
ind.append(i)
if mode[0]==0:
tt=time.time()
e.append(rbmv.energy_grbm(x_test, b, c, w))
E.append(rbmv.energy_grbm(visible, b, c, w))
t1+=time.time()-tt
tt=time.time()
fe.append(rbmv.pseudo_likelihood_grbm(x_test, b, c, w, f))
#fE.append(rbmv.pseudo_likelihood_grbm(visible, b, c, w, 3))
er.append(rbmv.error(x_test, b, c, w))
t2+=time.time()-tt
else:
tt=time.time()
e.append(rbmv.energy_rbm(x_test, b, c, w))
E.append(rbmv.energy_rbm(visible, b, c, w))
t1+=time.time()-tt
tt=time.time()
fe.append(rbmv.pseudo_likelihood_rbm(x_test, b, c, w, f))
#fE.append(rbmv.pseudo_likelihood_rbm(visible, b, c, w, 3))
er.append(rbmv.error(x_test, b, c, w))
t2+=time.time()-tt
plt.hist(np.mean(func.sigmoid(c+w.dot(visible)), axis=1), bins=30)
plt.show()
plt.plot(ind, e,label="test set")
plt.plot(ind, E, label="training set")
plt.legend(loc='upper right')
if mode[0] == 1:
plt.yscale('symlog')
plt.show()
plt.plot(ind, fe, label="free energy test set")
#plt.plot(ind, fE, label="free energy training set")
plt.legend(loc='lower right')
plt.show()
print("free-energy", fe[len(fe)-1])
print("energy time and pseudo likelihood time ", t1, t2)
plt.plot(ind, er)
plt.show()
return [ind,fe]