-
Notifications
You must be signed in to change notification settings - Fork 36
/
RBM.py
executable file
·220 lines (169 loc) · 7.48 KB
/
RBM.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import torch
import torchvision
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import math
from tqdm import tqdm
import sys
BATCH_SIZE = 64
class RBM(nn.Module):
'''
This class defines all the functions needed for an BinaryRBN model
where the visible and hidden units are both considered binary
'''
def __init__(self,
visible_units=256,
hidden_units = 64,
k=2,
learning_rate=1e-5,
learning_rate_decay = False,
xavier_init = False,
increase_to_cd_k = False,
use_gpu = False
):
'''
Defines the model
W:Wheights shape (visible_units,hidden_units)
c:hidden unit bias shape (hidden_units , )
b : visible unit bias shape(visisble_units ,)
'''
super(RBM,self).__init__()
self.desc = "RBM"
self.visible_units = visible_units
self.hidden_units = hidden_units
self.k = k
self.learning_rate = learning_rate
self.learning_rate_decay = learning_rate_decay
self.xavier_init = xavier_init
self.increase_to_cd_k = increase_to_cd_k
self.use_gpu = use_gpu
self.batch_size = 16
# Initialization
if not self.xavier_init:
self.W = torch.randn(self.visible_units,self.hidden_units) * 0.01 #weights
else:
self.xavier_value = torch.sqrt(torch.FloatTensor([1.0 / (self.visible_units + self.hidden_units)]))
self.W = -self.xavier_value + torch.rand(self.visible_units, self.hidden_units) * (2 * self.xavier_value)
self.h_bias = torch.zeros(self.hidden_units) #hidden layer bias
self.v_bias = torch.zeros(self.visible_units) #visible layer bias
def to_hidden(self ,X):
'''
Converts the data in visible layer to hidden layer
also does sampling
X here is the visible probabilities
:param X: torch tensor shape = (n_samples , n_features)
:return - X_prob - new hidden layer (probabilities)
sample_X_prob - Gibbs sampling of hidden (1 or 0) based
on the value
'''
X_prob = torch.matmul(X,self.W)
X_prob = torch.add(X_prob, self.h_bias)#W.x + c
X_prob = torch.sigmoid(X_prob)
sample_X_prob = self.sampling(X_prob)
return X_prob,sample_X_prob
def to_visible(self,X):
'''
reconstructs data from hidden layer
also does sampling
X here is the probabilities in the hidden layer
:returns - X_prob - the new reconstructed layers(probabilities)
sample_X_prob - sample of new layer(Gibbs Sampling)
'''
# computing hidden activations and then converting into probabilities
X_prob = torch.matmul(X ,self.W.transpose( 0 , 1) )
X_prob = torch.add(X_prob , self.v_bias)
X_prob = torch.sigmoid(X_prob)
sample_X_prob = self.sampling(X_prob)
return X_prob,sample_X_prob
def sampling(self,prob):
'''
Bernoulli sampling done based on probabilities s
'''
s = torch.distributions.Bernoulli(prob).sample()
return s
def reconstruction_error(self , data):
'''
Computes the reconstruction error for the data
handled by pytorch by loss functions
'''
return self.contrastive_divergence(data, False)
def reconstruct(self , X,n_gibbs):
'''
This will reconstruct the sample with k steps of gibbs Sampling
'''
v = X
for i in range(n_gibbs):
prob_h_,h = self.to_hidden(v)
prob_v_,v = self.to_visible(prob_h_)
return prob_v_,v
def contrastive_divergence(self, input_data ,training = True,
n_gibbs_sampling_steps=1,lr = 0.001):
# positive phase
positive_hidden_probabilities,positive_hidden_act = self.to_hidden(input_data)
# calculating W via positive side
positive_associations = torch.matmul(input_data.t() , positive_hidden_act)
# negetive phase
hidden_activations = positive_hidden_act
for i in range(n_gibbs_sampling_steps):
visible_probabilities , _ = self.to_visible(hidden_activations)
hidden_probabilities,hidden_activations = self.to_hidden(visible_probabilities)
negative_visible_probabilities = visible_probabilities
negative_hidden_probabilities = hidden_probabilities
# calculating W via negative side
negative_associations = torch.matmul(negative_visible_probabilities.t() , negative_hidden_probabilities)
# Update parameters
if(training):
batch_size = self.batch_size
g = (positive_associations - negative_associations)
grad_update = g / batch_size
v_bias_update = torch.sum(input_data - negative_visible_probabilities,dim=0)/batch_size
h_bias_update = torch.sum(positive_hidden_probabilities - negative_hidden_probabilities,dim=0)/batch_size
self.W += lr * grad_update
self.v_bias += lr * v_bias_update
self.h_bias += lr * h_bias_update
# Compute reconstruction error
error = torch.mean(torch.sum((input_data - negative_visible_probabilities)**2 , dim = 0))
return error,torch.sum(torch.abs(grad_update))
def forward(self,input_data):
'data->hidden'
return self.to_hidden(input_data)
def step(self,input_data,epoch,num_epochs):
'''
Includes the foward prop plus the gradient descent
Use this for training
'''
if self.increase_to_cd_k:
n_gibbs_sampling_steps = int(math.ceil((epoch/num_epochs) * self.k))
else:
n_gibbs_sampling_steps = self.k
if self.learning_rate_decay:
lr = self.learning_rate / epoch
else:
lr = self.learning_rate
return self.contrastive_divergence(input_data , True,n_gibbs_sampling_steps,lr);
def train(self,train_dataloader , num_epochs = 50,batch_size=16):
self.batch_size = batch_size
if(isinstance(train_dataloader ,torch.utils.data.DataLoader)):
train_loader = train_dataloader
else:
train_loader = torch.utils.data.DataLoader(train_dataloader, batch_size=batch_size)
for epoch in range(1 , num_epochs+1):
epoch_err = 0.0
n_batches = int(len(train_loader))
# print(n_batches)
cost_ = torch.FloatTensor(n_batches , 1)
grad_ = torch.FloatTensor(n_batches , 1)
for i,(batch,_) in tqdm(enumerate(train_loader),ascii=True,
desc="RBM fitting", file=sys.stdout):
batch = batch.view(len(batch) , self.visible_units)
if(self.use_gpu):
batch = batch.cuda()
cost_[i-1],grad_[i-1] = self.step(batch,epoch,num_epochs)
print("Epoch:{} ,avg_cost = {} ,std_cost = {} ,avg_grad = {} ,std_grad = {}".format(epoch,\
torch.mean(cost_),\
torch.std(cost_),\
torch.mean(grad_),\
torch.std(grad_)))
return