-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdem.py
185 lines (151 loc) · 6.9 KB
/
dem.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
import tensorflow as tf
from autoencoder import build_model
from rbm import RBM
def tf_norm(x):
return tf.sqrt(tf.reduce_sum(tf.square(x)))
def tf_mean_norm(x):
return tf.reduce_mean(tf.abs(x))
class DEM(object):
"""Compositional model consists of encoder, assoc memory(rbm), decoder."""
def __init__(self, ae, rbm, encoder=None, decoder=None):
# self.ae = ae
if ae:
assert False
self.encoder = ae.encoder
self.decoder = ae.decoder
else:
assert encoder is not None and decoder is not None
self.encoder = encoder
self.decoder = decoder
self.rbm = rbm
assert len(self.encoder.get_output_shape_at(-1)) == 2, \
'Latent (z) space must be 1D.'
assert self.encoder.get_output_shape_at(-1)[-1] == self.num_z
@classmethod
def load_from_param_files(cls, x_shape, relu_max,
encode_fn, encoder_weights,
decode_fn, decoder_weights,
rbm_weights):
with tf.name_scope('encoder'):
encoder = build_model(
x_shape, relu_max, encode_fn, None, encoder_weights)
z_shape = encoder.get_output_shape_at(-1)[1:]
with tf.name_scope('decoder'):
decoder = build_model(
z_shape, relu_max, None, decode_fn, decoder_weights)
with tf.name_scope('rbm'):
rbm = RBM(None, None, rbm_weights)
assert z_shape[0] == rbm.num_vis, ('%s vs %s' % z_shape[0], rbm.num_vis)
return cls(None, rbm, encoder, decoder)
@property
def num_z(self):
return self.rbm.num_vis
@property
def num_h(self):
return self.rbm.num_hid
def get_trainable_vars(self, names):
trainable_vars = []
for name in names:
trainable_vars.extend(
tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=name))
return trainable_vars
def encode(self, sess, dataset, dataset_cls):
x = tf.placeholder(tf.float32, [None] + list(dataset.x_shape))
h = self.rbm._compute_up(self.encoder(x))
train_set_size = 1000
# num_batches = len(dataset.train_xs) / batch_size
encoded_train_xs = sess.run(h, {x: dataset.train_xs[:train_set_size]})
encoded_test_xs = sess.run(h, {x: dataset.test_xs})
return dataset_cls(encoded_train_xs, dataset.train_ys[:train_set_size],
encoded_test_xs, dataset.test_ys)
def free_energy(self, z):
"""build the graph to compute free energy given z :: placeholder"""
return self.rbm.free_energy(z)
def free_energy_wrt_x(self, x):
"""build the graph to compute free energy given x :: placeholder"""
z = self.encoder(x)
fe = tf.reduce_mean(self.rbm.free_energy(z))
# dfe_dz = tf.gradients(fe, z)[0]
# grad_norm = tf_mean_norm(dfe_dz)
# grad_norm = tf.stop_gradient(grad_norm)
# grad_norm = tf.Print(grad_norm, ['fe grad_norm:', grad_norm])
return fe# / grad_norm
def vhv(self, z_samples):
"""z->h->z public interface for GibbsSampler.
return: z_prob, z_samples
"""
return self.rbm.vhv(z_samples)
def rbm_loss_and_cost(self, z_data, z_model):
"""build the graph for assoc memory (rbm) cost and monitoring loss.
z_data: placeholder holds the encoded training data;
z_mdoel: placeholder holds the samples from sampler
"""
return self.rbm.loss_and_cost(z_data, z_model)
def autoencoder_cost(self, x):
x_recon = self.decoder(self.encoder(x))
cost = tf.reduce_mean(tf.square(x_recon - x))
# dcost_dx_recon = tf.gradients(cost, x_recon)[0]
# grad_norm = tf_mean_norm(dcost_dx_recon)
# grad_norm = tf.stop_gradient(grad_norm)
# grad_norm = tf.Print(grad_norm, ['ae grad_norm:', grad_norm])
return cost# / grad_norm
def encoder_cost(self, x, target_z):
"""build the graph for encoder cost.
x: placeholder for encoder input, should feed decoder-generated images;
target_z: placeholder for target code, should feed z used by decoder,
which are the samples from GibbsSampler;
"""
z = self.encoder(x)
cost = tf.reduce_mean(tf.square(target_z - z))
return cost
def decoder_cost(self, z, target_x):
"""build the graph for encoder cost.
z: placeholder for decoder input, should feed encoded training data;
target_x: placeholder for target output, should feed training data;
"""
x = self.decoder(z)
cost = tf.reduce_mean(tf.square(target_x - x))
return cost
def save_model(self, sess, output_dir, prefix):
encoder_weights_file = os.path.join(output_dir, prefix+'encoder.h5')
decoder_weights_file = os.path.join(output_dir, prefix+'decoder.h5')
self.encoder.save_weights(encoder_weights_file)
self.decoder.save_weights(decoder_weights_file)
self.rbm.save_model(sess, output_dir, prefix)
print 'model save at', encoder_weights_file
if __name__ == '__main__':
from dataset_wrapper import Cifar10Wrapper
from rbm import RBM
from autoencoder import AutoEncoder
from dem_trainer import DEMTrainer
import cifar10_ae
import gibbs_sampler
import utils
import keras.backend as K
import os
import h5py
import numpy as np
np.random.seed(66699)
sess = utils.create_session()
K.set_session(sess)
dataset = Cifar10Wrapper.load_default()
ae_folder = 'prod/cifar10_ae3_relu_6/'
# encoder_weights_file = os.path.join(ae_folder, 'encoder.h5')
# decoder_weights_file = os.path.join(ae_folder, 'decoder.h5')
# rbm_params_file = os.path.join(
# ae_folder, 'ptrbm_scheme1/ptrbm_hid2000_lr0.001_pcd25/epoch_500_rbm.h5')
encoder_weights_file = '/home/hhu/Developer/dem/prod/cifar10_ae3_relu_6/test_ae_fe_const_balance/epoch_500_encoder.h5'
decoder_weights_file = encoder_weights_file.replace('encoder.', 'decoder.')
rbm_params_file = encoder_weights_file.replace('encoder.', 'rbm.')
dem = DEM.load_from_param_files(dataset.x_shape, cifar10_ae.RELU_MAX,
cifar10_ae.encode, encoder_weights_file,
cifar10_ae.decode, decoder_weights_file,
rbm_params_file)
train_config = utils.TrainConfig(
lr=0.001, batch_size=100, num_epoch=500, use_pcd=True, cd_k=25)
sampler_generator = gibbs_sampler.create_sampler_generator(dem.rbm, None, 100, 1000)
sampler = gibbs_sampler.GibbsSampler.create_pcd_sampler(
dem.rbm, train_config.batch_size, train_config.cd_k)
output_dir = os.path.join(ae_folder, 'test_ae_fe')
dem_trainer = DEMTrainer(sess, dataset, dem, utils.vis_cifar10, output_dir)
dem_trainer.train(train_config, sampler, sampler_generator)