Mercurial > pylearn
view sandbox/rbm/model.py @ 398:6e55ccb7e2bf
Better output
author | Joseph Turian <turian@gmail.com> |
---|---|
date | Tue, 08 Jul 2008 20:48:56 -0400 |
parents | e0c9357456e0 |
children | 8796b91a9f09 |
line wrap: on
line source
""" The model for an autoassociator for sparse inputs, using Ronan Collobert + Jason Weston's sampling trick (2008). """ import parameters import globals from globals import LR import numpy from numpy import dot import random random.seed(globals.SEED) import pylearn.nnet_ops def sigmoid(v): # if x < -30.0: return 0.0 # if x > 30.0: return 1.0 return 1.0 / (1.0 + numpy.exp(-v)) def sample(v): assert len(v.shape) == 2 x = numpy.zeros(v.shape) for j in range(v.shape[0]): for i in range(v.shape[1]): assert v[j][i] >= 0 and v[j][i] <= 1 if random.random() < v[j][i]: x[j][i] = 1 else: x[j][i] = 0 return x def crossentropy(output, target): """ Compute the crossentropy of binary output wrt binary target. @note: We do not sum, crossentropy is computed by component. @todo: Rewrite as a scalar, and then broadcast to tensor. """ return -(target * numpy.log(output) + (1 - target) * numpy.log(1 - output)) class Model: def __init__(self): self.parameters = parameters.Parameters(randomly_initialize=True) def update(self, instance): """ Update the L{Model} using one training instance. @param instance: A dict from feature index to (non-zero) value. @todo: Should assert that nonzero_indices and zero_indices are correct (i.e. are truly nonzero/zero). """ v0 = numpy.zeros((1, globals.INPUT_DIMENSION)) for idx in instance.keys(): v0[0][idx] = instance[idx] q0 = sigmoid(self.parameters.b + dot(v0, self.parameters.w)) h0 = sample(q0) p0 = sigmoid(self.parameters.c + dot(h0, self.parameters.w.T)) v1 = sample(p0) q1 = sigmoid(self.parameters.b + dot(v1, self.parameters.w)) print print "v[0]:", v0 print "Q(h[0][i] = 1 | v[0]):", q0 print "h[0]:", h0 print "P(v[1][j] = 1 | h[0]):", p0 print "XENT(P(v[1][j] = 1 | h[0]) | v0):", numpy.sum(crossentropy(p0, v0)) print "v[1]:", v1 print "Q(h[1][i] = 1 | v[1]):", q1 self.parameters.w += LR * (dot(v0.T, h0) - dot(v1.T, q1)) self.parameters.b += LR * (h0 - q1) self.parameters.c += LR * (v0 - v1) # print self.parameters