-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbm.py
266 lines (219 loc) · 10.2 KB
/
dbm.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import numpy as np
import cPickle
import os
import time
import tensorflow as tf
import matplotlib.pyplot as plt
import utils
import keras_auto_encoder
import keras_utils
def _copy_tensor_list(tensors):
new_tensors = []
for t in tensors:
new_tensors.append(tf.zeros_like(t) + t)
return new_tensors
class DBM(object):
def __init__(self, num_units, name=None):
self.num_units = num_units
self.name = name if name is not None else 'dbm'
self.weights = []
self.biases = []
with tf.variable_scope(self.name):
for i in range(self.num_layers):
if i < self.num_layers - 1:
self.weights.append(tf.get_variable(
'weights_%d' % i, shape=self.num_units[i:i+2],
initializer=tf.random_normal_initializer(0, 0.01)))
self.biases.append(tf.get_variable(
'bias_%d' % i, shape=[self.num_units[i]],
initializer=tf.constant_initializer(0.0)))
@property
def num_layers(self):
return len(self.num_units)
def init_states(self, vis):
"""Return a list of states filled with tensors (not Variables)."""
batch_size = tf.shape(vis)[0]
states = [vis]
for i in range(1, self.num_layers):
states.append(tf.random_uniform([batch_size, self.num_units[i]], 0, 1))
return states
def _compute_probs(self, states, offset):
"""Compute prob (in place) given a list of tensors."""
for i in range(offset, self.num_layers, 2):
pre_sigmoid = tf.zeros_like(states[i]) + self.biases[i]
if i > 0:
pre_sigmoid += tf.matmul(states[i-1], self.weights[i-1])
if i+i < self.num_layers:
pre_sigmoid += tf.matmul(states[i+1], tf.transpose(self.weights[i]))
states[i] = tf.nn.sigmoid(pre_sigmoid)
def _sample_probs(self, states, offset):
"""In place bernoulli sampling given a list of probs (in tensors)."""
for i in range(offset, self.num_layers, 2):
states[i] = utils.sample_bernoulli(states[i])
def mean_field(self, k, states):
"""In place mean-field updates till convergence."""
for i in range(k):
self._compute_probs(states, 1)
self._compute_probs(states, 2)
def pcd(self, k, states):
"""In place pcd updates, input should be a list of tensor."""
for i in range(k):
self._compute_probs(states, 1)
self._sample_probs(states, 1)
self._compute_probs(states, 0)
self._sample_probs(states, 0)
def collect_stats(self, states):
"""Collect stats given mean filed/pcd states; params not changed."""
num_chains = tf.to_float(tf.shape(states[0])[0])
stats_ws = []
stats_biases = []
for i in range(self.num_layers):
if i < self.num_layers - 1:
layer_stats = tf.matmul(tf.transpose(states[i]), states[i+1]) / num_chains
stats_ws.append(layer_stats)
stats_biases.append(tf.reduce_mean(states[i], reduction_indices=[0]))
return stats_ws, stats_biases
def compute_gradient(self, mf_states, pcd_states, mf_k, pcd_k):
"""Compute gradients, mf_states and pcd_states will be updated in place."""
self.mean_field(mf_k, mf_states)
self.pcd(pcd_k, pcd_states)
pos_stats_ws, pos_stats_biases = self.collect_stats(mf_states)
neg_stats_ws, neg_stats_biases = self.collect_stats(pcd_states)
dws = []
dbiases = []
for i in range(self.num_layers):
if i < self.num_layers - 1:
dws.append(pos_stats_ws[i] - neg_stats_ws[i])
dbiases.append(pos_stats_biases[i] - neg_stats_biases[i])
return dws, dbiases
def compute_loss(self, vis):
states = self.init_states(vis)
self._compute_probs(states, 1)
self._sample_probs(states, 1)
self._compute_probs(states, 0)
recon_vprob = states[0]
loss = vis * tf.log(recon_vprob) + (1-vis) * tf.log(1 - recon_vprob)
return - tf.reduce_mean(tf.reduce_sum(loss, reduction_indices=[1]))
def train_step(self, lr, vis, pcd_states, mf_k, pcd_k):
mf_states = self.init_states(vis)
new_pcd_states = _copy_tensor_list(pcd_states)
dws, dbiases = self.compute_gradient(mf_states, new_pcd_states, mf_k, pcd_k)
loss = self.compute_loss(vis)
updates = []
for i in range(self.num_layers):
if i < self.num_layers - 1:
updates.append(self.weights[i].assign_add(lr * dws[i]))
updates.append(self.biases[i].assign_add(lr * dbiases[i]))
return loss, updates, new_pcd_states
def sample_from_dbm(self, num_examples, num_steps):
init = tf.random_uniform([num_examples, self.num_units[0]], 0, 1)
states = self.init_states(init)
self.pcd(num_steps-1, states)
self._compute_probs(states, 1)
self._sample_probs(states, 1)
self._compute_probs(states, 0)
return states[0]
def train(dbm, xs, init_lr, num_epoch, batch_size,
mf_k, pcd_k, pcd_chain_size, output_dir):
num_batches = len(xs) / batch_size
assert num_batches * batch_size == len(xs)
vis = tf.placeholder(tf.float32, (None,) + xs.shape[1:], name='vis_input')
lr = tf.placeholder(tf.float32, (), name='lr')
pcd_states = [tf.placeholder(tf.float32, (pcd_chain_size, i)) for i in dbm.num_units]
pcd_vals = [np.random.uniform(0, 1, (pcd_chain_size, i)) for i in dbm.num_units]
loss, updates, new_pcd_states = dbm.train_step(lr, vis, pcd_states, mf_k, pcd_k)
if output_dir is not None:
sample_imgs = dbm.sample_from_dbm(100, 1000)
sess = utils.get_session()
with sess.as_default():
tf.initialize_all_variables().run()
for i in range(num_epoch):
np.random.shuffle(xs)
t = time.time()
loss_vals = np.zeros(num_batches)
for b in range(num_batches):
batch_xs = xs[b*batch_size : (b+1)*batch_size]
feed_dict = {vis: batch_xs,
lr: utils.scheduled_lr(init_lr, i, num_epoch)}
for key, val in zip(pcd_states, pcd_vals):
feed_dict[key] = val
loss_val, _, pcd_vals = sess.run(
[loss, updates, new_pcd_states], feed_dict=feed_dict)
loss_vals[b] = loss_val
print 'Epoch: %d, Train Loss: %s' % (i, loss_vals.mean())
print '\tTime took:', time.time() - t
if output_dir is not None:
if not os.path.exists(output_dir):
os.makedirs(output_dir)
imgs = sess.run(sample_imgs)
img_path = os.path.join(output_dir, 'epoch%d-plot.png' % i)
utils.vis_samples(imgs, 10, 10, (28, 28), img_path)
def train_with_decoder(dbm, xs, init_lr, num_epoch, batch_size,
mf_k, pcd_k, pcd_chain_size, output_dir, decoder_dir):
num_batches = len(xs) / batch_size
assert num_batches * batch_size == len(xs)
vis = tf.placeholder(tf.float32, (None,) + xs.shape[1:], name='vis_input')
lr = tf.placeholder(tf.float32, (), name='lr')
pcd_states = [tf.placeholder(tf.float32, (pcd_chain_size, i)) for i in dbm.num_units]
pcd_vals = [np.random.uniform(0, 1, (pcd_chain_size, i)) for i in dbm.num_units]
loss, updates, new_pcd_states = dbm.train_step(lr, vis, pcd_states, mf_k, pcd_k)
if output_dir is not None:
sample_imgs = dbm.sample_from_dbm(100, 1000)
output_dir = os.path.join(decoder_dir, output_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
sess = utils.get_session()
with sess.as_default():
tf.initialize_all_variables().run()
encoder, decoder = keras_utils.load_encoder_decoder(
(32, 32, 3),
keras_auto_encoder.deep_encoder1, os.path.join(decoder_dir, 'encoder'),
keras_auto_encoder.deep_decoder1, os.path.join(decoder_dir, 'decoder')
)
for i in range(num_epoch):
np.random.shuffle(xs)
t = time.time()
loss_vals = np.zeros(num_batches)
for b in range(num_batches):
batch_xs = xs[b*batch_size : (b+1)*batch_size]
feed_dict = {vis: batch_xs,
lr: utils.scheduled_lr(init_lr, i, num_epoch)}
for key, val in zip(pcd_states, pcd_vals):
feed_dict[key] = val
loss_val, _, pcd_vals = sess.run(
[loss, updates, new_pcd_states], feed_dict=feed_dict)
loss_vals[b] = loss_val
print 'Epoch: %d, Train Loss: %s' % (i+1, loss_vals.mean())
print '\tTime took:', time.time() - t
if (i+1) % 10 == 0 and output_dir is not None:
saver = tf.train.Saver()
save_path = saver.save(
sess, os.path.join(output_dir, 'epoch%d.ckpt' % (i+1)))
print '\tModel saved to:', save_path
imgs = sess.run(sample_imgs)
img_path = os.path.join(output_dir, 'epoch%d-plot.png' % (i+1))
decoder_input_shape = decoder.get_input_shape_at(0)[1:]
imgs = imgs.reshape((-1,) + decoder_input_shape)
imgs = decoder.predict(imgs)
utils.vis_cifar10(imgs, 10, 10, img_path)
if __name__ == '__main__':
lr = 0.001
num_epoch = 200
batch_size = 100
mf_k = 10
pcd_k = 5
pcd_chain_size = 100
output_dir = 'dbm-500-500-lr1e-3'
# (train_xs, _), _, _ = cPickle.load(file('mnist.pkl', 'rb'))
decoder_dir = 'old_noise_deep_model1'
dataset = os.path.join(decoder_dir, 'encoded_cifar10.pkl')
train_xs = cPickle.load(file(dataset, 'rb'))
num_imgs = train_xs.shape[0]
train_xs = train_xs.reshape(num_imgs, -1)
print train_xs.shape
batch_size = 20
dbm = DBM([640, 500, 500], output_dir)
train_with_decoder(dbm, train_xs, lr, num_epoch, batch_size,
mf_k, pcd_k, pcd_chain_size, output_dir, decoder_dir)
# train(dbm, train_xs, lr, num_epoch, batch_size,
# mf_k, pcd_k, pcd_chain_size, output_dir)