Actually what i'm doing is to implement convolutional autoencoder in theano, and I chose dA for the starting point. (code at
http://deeplearning.net/tutorial/code/dA.py)
I changed some methods but I don't think those are crucial. I attached my dA class.
class dA(object):
def __init__(self, numpy_rng, n_visible, n_hidden, theano_rng=None, input=None,
W=None, bhid=None, bvis=None):
self.n_visible = n_visible
self.n_hidden = n_hidden
# create a Theano random generator that gives symbolic random values
if not theano_rng:
theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
# note : W' was written as `W_prime` and b' as `b_prime`
if not W:
initial_W = numpy.asarray(numpy_rng.uniform(
low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
size=(n_visible, n_hidden)), dtype=theano.config.floatX)
W = theano.shared(value=initial_W, name='W', borrow=True)
if not bvis:
bvis = theano.shared(value=numpy.zeros(n_visible, dtype=theano.config.floatX), borrow=True)
if not bhid:
bhid = theano.shared(value=numpy.zeros(n_hidden, dtype=theano.config.floatX), name='b', borrow=True)
self.W = W
self.b = bhid
self.b_prime = bvis
self.W_prime = self.W.T
self.theano_rng = theano_rng
if input == None:
self.x = T.matrix(name='input')
else:
self.x = input
self.params = [self.W, self.b, self.b_prime]
def corruption(self, input, corruption_level):
return self.theano_rng.binomial(size=input.shape, n=1,
p=1 - corruption_level,
dtype=theano.config.floatX) * input
def encode(self, input):
return T.tanh(T.dot(input, self.W) + self.b)
#return T.nnet.sigmoid(T.dot(input, self.W) + self.b)
def decode(self, hidden):
return T.dot(hidden, self.W_prime) + self.b_prime
#return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
def reconstruct(self, input):
return self.decode(self.encode(input))
def get_cost_updates(self, corruption_level, learning_rate, L1):
tilde_x = self.corruption(self.x, corruption_level)
y = self.encode(tilde_x)
z = self.decode(y)
L = ((self.x - z) ** 2).sum(axis=1).mean()
reg_units = T.abs_(self.encode(self.x)).sum(axis=1).mean()
cost = L + reg_units * L1
gparams = T.grad(cost, self.params)
updates = []
for param, gparam in zip(self.params, gparams):
updates.append((param, param - learning_rate * gparam))
return (cost, updates)