-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
225 lines (173 loc) · 8.1 KB
/
model.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
import tensorflow as tf
from tensorflow.contrib.layers import apply_regularization, l2_regularizer
import numpy as np
import pandas as pd
class MultiDAE(object):
def __init__(self, p_dims, q_dims=None, lam=0.01, lr=1e-3, random_seed=None):
self.p_dims = p_dims
if q_dims is None:
self.q_dims = p_dims[::-1]
else:
assert q_dims[0] == p_dims[-1], "Input and output dimension must equal each other for autoencoders."
assert q_dims[-1] == p_dims[0], "Latent dimension for p- and q-network mismatches."
self.q_dims = q_dims
self.dims = self.q_dims + self.p_dims[1:]
self.lam = lam
self.lr = lr
self.random_seed = random_seed
self.construct_placeholders()
def construct_placeholders(self):
self.input_ph = tf.placeholder(
dtype=tf.float32, shape=[None, self.dims[0]])
self.keep_prob_ph = tf.placeholder_with_default(1.0, shape=None)
def build_graph(self):
self.construct_weights()
saver, logits = self.forward_pass()
log_softmax_var = tf.nn.log_softmax(logits)
# per-user average negative log-likelihood
neg_ll = -tf.reduce_mean(tf.reduce_sum(
log_softmax_var * self.input_ph, axis=1))
# apply regularization to weights
reg = l2_regularizer(self.lam)
reg_var = apply_regularization(reg, self.weights)
# tensorflow l2 regularization multiply 0.5 to the l2 norm
# multiply 2 so that it is back in the same scale
loss = neg_ll + 2 * reg_var
train_op = tf.train.AdamOptimizer(self.lr).minimize(loss)
# add summary statistics
tf.summary.scalar('negative_multi_ll', neg_ll)
tf.summary.scalar('loss', loss)
merged = tf.summary.merge_all()
return saver, logits, loss, train_op, merged
def forward_pass(self):
# construct forward graph
h = tf.nn.l2_normalize(self.input_ph, 1)
h = tf.nn.dropout(h, self.keep_prob_ph)
for i, (w, b) in enumerate(zip(self.weights, self.biases)):
h = tf.matmul(h, w) + b
if i != len(self.weights) - 1:
h = tf.nn.tanh(h)
return tf.train.Saver(), h
def construct_weights(self):
self.weights = []
self.biases = []
# define weights
for i, (d_in, d_out) in enumerate(zip(self.dims[:-1], self.dims[1:])):
weight_key = "weight_{}to{}".format(i, i+1)
bias_key = "bias_{}".format(i+1)
self.weights.append(tf.get_variable(
name=weight_key, shape=[d_in, d_out],
initializer=tf.contrib.layers.xavier_initializer(
seed=self.random_seed)))
self.biases.append(tf.get_variable(
name=bias_key, shape=[d_out],
initializer=tf.truncated_normal_initializer(
stddev=0.001, seed=self.random_seed)))
# add summary stats
tf.summary.histogram(weight_key, self.weights[-1])
tf.summary.histogram(bias_key, self.biases[-1])
class MultiVAE(MultiDAE):
def construct_placeholders(self):
super(MultiVAE, self).construct_placeholders()
# placeholders with default values when scoring
self.is_training_ph = tf.placeholder_with_default(0., shape=None)
self.anneal_ph = tf.placeholder_with_default(1., shape=None)
self.mu_q = None
self.std_q = None
def build_graph(self,noise):
self._construct_weights()
saver, logits, KL = self.forward_pass(noise)
log_softmax_var = tf.nn.log_softmax(logits)
neg_ll = -tf.reduce_mean(tf.reduce_sum(
log_softmax_var * self.input_ph,
axis=-1))
# apply regularization to weights
reg = l2_regularizer(self.lam)
reg_var = apply_regularization(reg, self.weights_q + self.weights_p)
# tensorflow l2 regularization multiply 0.5 to the l2 norm
# multiply 2 so that it is back in the same scale
neg_ELBO = neg_ll + self.anneal_ph * KL + 2 * reg_var
train_op = tf.train.AdamOptimizer(self.lr).minimize(neg_ELBO)
# add summary statistics
tf.summary.scalar('negative_multi_ll', neg_ll)
tf.summary.scalar('KL', KL)
tf.summary.scalar('neg_ELBO_train', neg_ELBO)
merged = tf.summary.merge_all()
return saver, logits, neg_ELBO, train_op, merged
def q_graph(self):
mu_q, std_q, KL = None, None, None
h = tf.nn.l2_normalize(self.input_ph, 1)
h = tf.nn.dropout(h, self.keep_prob_ph)
for i, (w, b) in enumerate(zip(self.weights_q, self.biases_q)):
h = tf.matmul(h, w) + b
if i != len(self.weights_q) - 1:
h = tf.nn.tanh(h)
else:
mu_q = h[:, :self.q_dims[-1]]
logvar_q = h[:, self.q_dims[-1]:]
std_q = tf.exp(0.5 * logvar_q)
KL = tf.reduce_mean(tf.reduce_sum(
0.5 * (-logvar_q + tf.exp(logvar_q) + mu_q**2 - 1), axis=1))
self.mu_q = mu_q
self.std_q = std_q
return mu_q, std_q, KL
def p_graph(self, z):
h = z
for i, (w, b) in enumerate(zip(self.weights_p, self.biases_p)):
h = tf.matmul(h, w) + b
if i != len(self.weights_p) - 1:
h = tf.nn.tanh(h)
return h
def noise_selection(self,noise,std_q):
return {
1: tf.random_normal(tf.shape(std_q),stddev=0.5),
2: tf.random_normal(tf.shape(std_q)),
3: tf.random_normal(tf.shape(std_q),stddev=2),
4: tf.random_uniform(tf.shape(std_q))
}.get(noise, 0)
def forward_pass(self,noise):
# q-network
mu_q, std_q, KL = self.q_graph()
#epsilon = tf.random_normal(tf.shape(std_q))
epsilon = self.noise_selection(noise,std_q)
#sampled_z = mu_q + self.is_training_ph *\
# epsilon * std_q
sampled_z = mu_q + epsilon * std_q
# p-network
logits = self.p_graph(sampled_z)
return tf.train.Saver(), logits, KL
def _construct_weights(self):
self.weights_q, self.biases_q = [], []
for i, (d_in, d_out) in enumerate(zip(self.q_dims[:-1], self.q_dims[1:])):
if i == len(self.q_dims[:-1]) - 1:
# we need two sets of parameters for mean and variance,
# respectively
d_out *= 2
weight_key = "weight_q_{}to{}".format(i, i+1)
bias_key = "bias_q_{}".format(i+1)
self.weights_q.append(tf.get_variable(
name=weight_key, shape=[d_in, d_out],
initializer=tf.contrib.layers.xavier_initializer(
seed=self.random_seed)))
self.biases_q.append(tf.get_variable(
name=bias_key, shape=[d_out],
initializer=tf.truncated_normal_initializer(
stddev=0.001, seed=self.random_seed)))
# add summary stats
tf.summary.histogram(weight_key, self.weights_q[-1])
tf.summary.histogram(bias_key, self.biases_q[-1])
self.weights_p, self.biases_p = [], []
for i, (d_in, d_out) in enumerate(zip(self.p_dims[:-1], self.p_dims[1:])):
weight_key = "weight_p_{}to{}".format(i, i+1)
bias_key = "bias_p_{}".format(i+1)
self.weights_p.append(tf.get_variable(
name=weight_key, shape=[d_in, d_out],
initializer=tf.contrib.layers.xavier_initializer(
seed=self.random_seed)))
self.biases_p.append(tf.get_variable(
name=bias_key, shape=[d_out],
initializer=tf.truncated_normal_initializer(
stddev=0.001, seed=self.random_seed)))
# add summary stats
tf.summary.histogram(weight_key, self.weights_p[-1])
tf.summary.histogram(bias_key, self.biases_p[-1])