-
Notifications
You must be signed in to change notification settings - Fork 36
/
DBN.py
executable file
·135 lines (112 loc) · 5.08 KB
/
DBN.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
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
from RBM import RBM
class DBN(nn.Module):
def __init__(self,
visible_units = 256,
hidden_units = [64 , 100],
k = 2,
learning_rate = 1e-5,
learning_rate_decay = False,
xavier_init = False,
increase_to_cd_k = False,
use_gpu = False
):
super(DBN,self).__init__()
self.n_layers = len(hidden_units)
self.rbm_layers =[]
self.rbm_nodes = []
# Creating different RBM layers
for i in range(self.n_layers ):
input_size = 0
if i==0:
input_size = visible_units
else:
input_size = hidden_units[i-1]
rbm = RBM(visible_units = input_size,
hidden_units = hidden_units[i],
k= k,
learning_rate = learning_rate,
learning_rate_decay = learning_rate_decay,
xavier_init = xavier_init,
increase_to_cd_k = increase_to_cd_k,
use_gpu=use_gpu)
self.rbm_layers.append(rbm)
# rbm_layers = [RBM(rbn_nodes[i-1] , rbm_nodes[i],use_gpu=use_cuda) for i in range(1,len(rbm_nodes))]
self.W_rec = [nn.Parameter(self.rbm_layers[i].W.data.clone()) for i in range(self.n_layers-1)]
self.W_gen = [nn.Parameter(self.rbm_layers[i].W.data) for i in range(self.n_layers-1)]
self.bias_rec = [nn.Parameter(self.rbm_layers[i].h_bias.data.clone()) for i in range(self.n_layers-1)]
self.bias_gen = [nn.Parameter(self.rbm_layers[i].v_bias.data) for i in range(self.n_layers-1)]
self.W_mem = nn.Parameter(self.rbm_layers[-1].W.data)
self.v_bias_mem = nn.Parameter(self.rbm_layers[-1].v_bias.data)
self.h_bias_mem = nn.Parameter(self.rbm_layers[-1].h_bias.data)
for i in range(self.n_layers-1):
self.register_parameter('W_rec%i'%i, self.W_rec[i])
self.register_parameter('W_gen%i'%i, self.W_gen[i])
self.register_parameter('bias_rec%i'%i, self.bias_rec[i])
self.register_parameter('bias_gen%i'%i, self.bias_gen[i])
def forward(self , input_data):
'''
running the forward pass
do not confuse with training this just runs a foward pass
'''
v = input_data
for i in range(len(self.rbm_layers)):
v = v.view((v.shape[0] , -1)).type(torch.FloatTensor)#flatten
p_v,v = self.rbm_layers[i].to_hidden(v)
return p_v,v
def reconstruct(self,input_data):
'''
go till the final layer and then reconstruct
'''
h = input_data
p_h = 0
for i in range(len(self.rbm_layers)):
h = h.view((h.shape[0] , -1)).type(torch.FloatTensor)#flatten
p_h,h = self.rbm_layers[i].to_hidden(h)
v = h
for i in range(len(self.rbm_layers)-1,-1,-1):
v = v.view((v.shape[0] , -1)).type(torch.FloatTensor)
p_v,v = self.rbm_layers[i].to_visible(v)
return p_v,v
def train_static(self, train_data,train_labels,num_epochs=50,batch_size=10):
'''
Greedy Layer By Layer training
Keeping previous layers as static
'''
tmp = train_data
for i in range(len(self.rbm_layers)):
print("-"*20)
print("Training the {} st rbm layer".format(i+1))
tensor_x = tmp.type(torch.FloatTensor) # transform to torch tensors
tensor_y = train_labels.type(torch.FloatTensor)
_dataset = torch.utils.data.TensorDataset(tensor_x,tensor_y) # create your datset
_dataloader = torch.utils.data.DataLoader(_dataset,batch_size=batch_size,drop_last = True) # create your dataloader
self.rbm_layers[i].train(_dataloader , num_epochs,batch_size)
# print(train_data.shape)
v = tmp.view((tmp.shape[0] , -1)).type(torch.FloatTensor)#flatten
p_v , v = self.rbm_layers[i].forward(v)
tmp = v
# print(v.shape)
return
def train_ith(self, train_data,train_labels,num_epochs,batch_size,ith_layer):
'''
taking ith layer at once
can be used for fine tuning
'''
if(ith_layer-1>len(self.rbm_layers) or ith_layer<=0):
print("Layer index out of range")
return
ith_layer = ith_layer-1
v = train_data.view((train_data.shape[0] , -1)).type(torch.FloatTensor)
for ith in range(ith_layer):
p_v, v = self.rbm_layers[ith].forward(v)
tmp = v
tensor_x = tmp.type(torch.FloatTensor) # transform to torch tensors
tensor_y = train_labels.type(torch.FloatTensor)
_dataset = torch.utils.data.TensorDataset(tensor_x,tensor_y) # create your datset
_dataloader = torch.utils.data.DataLoader(_dataset , batch_size=batch_size,drop_last=True)
self.rbm_layers[ith_layer].train(_dataloader, num_epochs,batch_size)
return