-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBM.py
230 lines (175 loc) · 6.89 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
221
222
223
224
225
226
227
228
229
230
from ast import arg
import imp
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as scio
from tqdm import tqdm
import argparse
from pathlib import Path
ORI = Path(".")
experiment_path = ORI / "experiments" / "RBM"
data_path = ORI / 'data/binaryalphadigs.mat'
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument('--digit', nargs="+", help='which digit/letter to learn, must be a list', type=int, default=[3])
parser.add_argument('--iter', help='number of iteration to train', type=int, default = 1000)
parser.add_argument('--nb_img', help='number of images to generate', type=int, default = 3)
parser.add_argument('--show_img', help='showing generated images', action ='store_true')
parser.add_argument('--save_img', help='saving generated images', action ='store_true')
args = parser.parse_args()
return args
def lire_alpha_digit(data, label):
"""
Create data from matlab raw data file.
---
Parameters:
label : n_array (n, )
Number which to extract images
data : matrix
Data set from matlab file
"""
out = []
for number in label:
extracted = np.array([data[number][i].flatten() for i in range(len(data[number]))])
out.append(extracted)
return np.concatenate(out)
class RBM():
"""
This class is our RBM structure
---
Parameters:
p : int (1,)
The number of neurons in the visible layer
q : int (1,)
The number of neurons in the hidden layer
"""
def __init__(self, p, q):
self.W = np.random.normal(0, 0.01, (p, q)) # random initialization with distribution N(0, 0.01)
self.a = np.zeros(p)
self.b = np.zeros(q)
def entree_sortie_RBM(self, data):
"""
This function compute the hidden layer given the visible layer.
According to theory: P(h_j = 1 | v) = sigm(b_j + \sum{w_{i,j} v_i}).
---
Parameters:
RBM: class
Our RBM
data:
Our data
"""
hidden = 1. / (1. + np.exp(-self.b.reshape(1,-1) - data @ self.W ))
return hidden
def sortie_entree_RBM(self, data):
"""
This function compute the visible layer given the hidden layer.
According to theory: P(v_i = 1 | h) = sigm(a_i + \sum{w_{i,j} h_j}).
---
Parameters:
RBM: class
Our RBM
data:
Our data
"""
visible = 1. / (1. + np.exp(- self.a.reshape(1,-1) - data @ self.W.T))
return visible
def update(self, val_update):
"""
This function will update our RBM with values in val_update
---
Parameters :
val : vector (3,)
Containing the values to be updated to W, b, a of our RBM
"""
self.W += val_update[0]
self.a += val_update[1]
self.b += val_update[2]
def fit(self, x, iteration, lr, batch_size):
"""
This function will train our RBM model to correspoding input images x
---
Parameters:
RBM: class
Our RBM structure
x: matrix (num_of_samples, p)
Input flatten image
iteration: int (1, )
The number of iteration of training process
lr: int (1, )
Learning rate
batch_size: int (1,)
Batch size in training
"""
p,q = self.W.shape
n = x.shape[0]
print(f"Training Started. {iteration} iterations.")
for i in tqdm(range(iteration)):
x_copy = x.copy()
np.random.shuffle(x_copy)
for batch in range(0, n, batch_size):
v_0 = x_copy[batch : min(batch + batch_size, n), :]
h_0 = (np.random.uniform(size = (len(v_0), q)) < self.entree_sortie_RBM(v_0)).astype('float')
v_1 = (np.random.uniform(size = (len(v_0), p)) < self.sortie_entree_RBM(h_0)).astype('float')
# Gradient
d_a = np.sum(v_0 - v_1, axis = 0)
d_b = np.sum(self.entree_sortie_RBM(v_0) - self.entree_sortie_RBM(v_1), axis = 0)
d_W = np.dot(v_0.T, self.entree_sortie_RBM(v_0)) - np.dot(v_1.T, self.entree_sortie_RBM(v_1))
lr_ = lr/len(v_0)
self.update([lr_ * d_W, lr_ * d_a, lr_ *d_b])
hidden = self.entree_sortie_RBM(x_copy)
approximated_data = self.sortie_entree_RBM(hidden)
error = np.linalg.norm(x_copy - approximated_data, ord='fro')**2 / x_copy.size
print('End Training')
print("--------------")
def generate_image(self, nb_images, nb_iteration):
"""
From trained RBM, this function will generate images.
---
Parameters:
RBM :
Our RBM class with the weights already trained and updated
nb_images : int (1,)
The number of samples that we want to generate
nb_iter : int (1,)
The number of iterations used during the generation
"""
imgs = []
p,q = self.W.shape
for i in range(nb_images):
input = (np.random.uniform(size=p) < 0.5).astype('float')
for iter in range(nb_iteration):
h = (np.random.uniform(size = q) < self.entree_sortie_RBM(input)).astype('float')
input = (np.random.uniform(size = p) < self.sortie_entree_RBM(h)).astype('float')
output = np.reshape(input, (20, 16))
imgs.append(output)
print(f"Generated {nb_images} images")
return imgs
def display(self):
print(self.W, self.a, self.b)
def visual_images(list_images):
nb_imgs = len(list_images)
# 5 images each column
nb_columns = 5 if nb_imgs >= 5 else nb_imgs
nb_rows = nb_imgs//5 + 1 if nb_imgs%5 != 0 else nb_imgs//5
fig, axs = plt.subplots(nb_rows, nb_columns, figsize=(2*nb_columns, 2*nb_rows))
for image, ax in zip(list_images, axs.flatten()):
ax.imshow(image, cmap='gray')
ax.axis('off')
if __name__ == '__main__':
args = arg_parse()
mat_contents = scio.loadmat(data_path)
x = lire_alpha_digit(mat_contents['dat'], args.digit)
epochs = args.iter
lr = 0.1
batch_size = 3
rbm = RBM(320, 200)
rbm.fit( x, epochs, lr, batch_size)
generated_images = rbm.generate_image(args.nb_img , 500)
if args.show_img:
visual_images(generated_images)
if args.save_img:
if experiment_path.is_dir() == False:
experiment_path.mkdir(parents=True, exist_ok=True)
for i in range(args.nb_img):
plt.imsave( experiment_path / f"image_RBM-{epochs}-epochs-{i}.png", generated_images[i], cmap ='gray')
print(f"Saved {args.nb_img} images")