-
Notifications
You must be signed in to change notification settings - Fork 14
/
darn.py
188 lines (141 loc) · 5.87 KB
/
darn.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
#!/usr/bin/env python
from __future__ import division
import logging
import numpy as np
import theano
import theano.tensor as T
from theano.printing import Print
from learning.model import default_weights
from learning.models.rws import TopModule, Module, theano_rng
from learning.utils.unrolled_scan import unrolled_scan
_logger = logging.getLogger(__name__)
floatX = theano.config.floatX
class DARNTop(TopModule):
def __init__(self, **hyper_params):
super(DARNTop, self).__init__()
# Hyper parameters
self.register_hyper_param('n_X', help='no. binary variables')
self.register_hyper_param('unroll_scan', default=1)
# Model parameters
self.register_model_param('b', help='sigmoid(b)-bias ', default=lambda: np.zeros(self.n_X))
self.register_model_param('W', help='weights (triangular)', default=lambda: 0.5*default_weights(self.n_X, self.n_X) )
self.set_hyper_params(hyper_params)
def log_prob(self, X):
""" Evaluate the log-probability for the given samples.
Parameters
----------
X: T.tensor
samples from X
Returns
-------
log_p: T.tensor
log-probabilities for the samples in X
"""
n_X, = self.get_hyper_params(['n_X'])
b, W = self.get_model_params(['b', 'W'])
W = T.tril(W, k=-1)
prob_X = self.sigmoid(T.dot(X, W) + b)
log_prob = X*T.log(prob_X) + (1-X)*T.log(1-prob_X)
log_prob = T.sum(log_prob, axis=1)
return log_prob
def sample(self, n_samples):
""" Sample from this toplevel module and return X ~ P(X), log(P(X))
Parameters
----------
n_samples:
number of samples to drawn
Returns
-------
X: T.tensor
samples from this module
log_p: T.tensor
log-probabilities for the samples returned in X
"""
n_X, = self.get_hyper_params(['n_X'])
b, W = self.get_model_params(['b', 'W'])
#------------------------------------------------------------------
a_init = T.zeros([n_samples, n_X]) + T.shape_padleft(b)
post_init = T.zeros([n_samples], dtype=floatX)
x_init = T.zeros([n_samples], dtype=floatX)
rand = theano_rng.uniform((n_X, n_samples), nstreams=512)
def one_iter(i, Wi, rand_i, a, X, post):
pi = self.sigmoid(a[:,i])
xi = T.cast(rand_i <= pi, floatX)
post = post + T.log(pi*xi + (1-pi)*(1-xi))
a = a + T.outer(xi, Wi)
return a, xi, post
[a, X, post], updates = unrolled_scan(
fn=one_iter,
sequences=[T.arange(n_X), W, rand],
outputs_info=[a_init, x_init, post_init],
unroll=self.unroll_scan
)
assert len(updates) == 0
return X.T, post[-1,:]
class DARN(Module):
def __init__(self, **hyper_params):
super(DARN, self).__init__()
# Hyper parameters
self.register_hyper_param('n_X', help='no. binary variables')
self.register_hyper_param('n_Y', help='no. conditioning binary variables')
self.register_hyper_param('unroll_scan', default=1)
# Model parameters
self.register_model_param('b', help='sigmoid(b)-bias ', default=lambda: np.zeros(self.n_X))
self.register_model_param('W', help='weights (triangular)', default=lambda: default_weights(self.n_X, self.n_X) )
self.register_model_param('U', help='cond. weights U', default=lambda: default_weights(self.n_Y, self.n_X) )
self.set_hyper_params(hyper_params)
def log_prob(self, X, Y):
""" Evaluate the log-probability for the given samples.
Parameters
----------
Y: T.tensor
samples from the upper layer
X: T.tensor
samples from the lower layer
Returns
-------
log_p: T.tensor
log-probabilities for the samples in X and Y
"""
n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
b, W, U = self.get_model_params(['b', 'W', 'U'])
W = T.tril(W, k=-1)
prob_X = self.sigmoid(T.dot(X, W) + T.dot(Y, U) + T.shape_padleft(b))
log_prob = X*T.log(prob_X) + (1-X)*T.log(1-prob_X)
log_prob = T.sum(log_prob, axis=1)
return log_prob
def sample(self, Y):
""" Evaluate the log-probability for the given samples.
Parameters
----------
Y: T.tensor
samples from the upper layer
Returns
-------
X: T.tensor
samples from the lower layer
log_p: T.tensor
log-probabilities for the samples in X and Y
"""
n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
b, W, U = self.get_model_params(['b', 'W', 'U'])
batch_size = Y.shape[0]
#------------------------------------------------------------------
a_init = T.dot(Y, U) + T.shape_padleft(b) # shape (batch, n_vis)
post_init = T.zeros([batch_size], dtype=floatX)
x_init = T.zeros([batch_size], dtype=floatX)
rand = theano_rng.uniform((n_X, batch_size), nstreams=512)
def one_iter(i, Wi, rand_i, a, X, post):
pi = self.sigmoid(a[:,i])
xi = T.cast(rand_i <= pi, floatX)
post = post + T.log(pi*xi + (1-pi)*(1-xi))
a = a + T.outer(xi, Wi)
return a, xi, post
[a, X, post], updates = unrolled_scan(
fn=one_iter,
sequences=[T.arange(n_X), W, rand],
outputs_info=[a_init, x_init, post_init],
unroll=self.unroll_scan
)
assert len(updates) == 0
return X.T, post[-1,:]