view simple_autoassociator.py/model.py @ 386:a474341861fa

Added a simple AA
author Joseph Turian <turian@gmail.com>
date Tue, 08 Jul 2008 02:27:00 -0400
parents sparse_random_autoassociator/model.py@42cc94cf6c12
children 98ca97cc9910
line wrap: on
line source

"""
The model for an autoassociator for sparse inputs, using Ronan Collobert + Jason
Weston's sampling trick (2008).
"""

from graph import trainfn
import parameters

import globals
from globals import LR

import numpy
import random
random.seed(globals.SEED)

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).
        """
        x = numpy.zeros(globals.INPUT_DIMENSION)
        for idx in instance.keys():
            x[idx] = instance[idx]

        (y, loss, gw1, gb1, gw2, gb2) = trainfn(x, self.parameters.w1, self.parameters.b1, self.parameters.w2, self.parameters.b2)
        print
        print "instance:", instance
        print "OLD y:", y
        print "OLD total loss:", loss

        # SGD update
        self.parameters.w1  -= LR * gw1
        self.parameters.b1  -= LR * gb1
        self.parameters.w2  -= LR * gw2
        self.parameters.b2  -= LR * gb2

        # Recompute the loss, to make sure it's descreasing
        (y, loss, gw1, gb1, gw2, gb2) = trainfn(x, self.parameters.w1, self.parameters.b1, self.parameters.w2, self.parameters.b2)
        print "NEW y:", y
        print "NEW total loss:", loss