|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import sys |
| 5 | +import numpy |
| 6 | +from utils import * |
| 7 | + |
| 8 | + |
| 9 | +class dA(object): |
| 10 | + def __init__(self, input=None, n_visible=2, n_hidden=3, \ |
| 11 | + W=None, hbias=None, vbias=None, numpy_rng=None): |
| 12 | + |
| 13 | + self.n_visible = n_visible # num of units in visible (input) layer |
| 14 | + self.n_hidden = n_hidden # num of units in hidden layer |
| 15 | + |
| 16 | + if numpy_rng is None: |
| 17 | + numpy_rng = numpy.random.RandomState(1234) |
| 18 | + |
| 19 | + if W is None: |
| 20 | + a = 1. / n_visible |
| 21 | + initial_W = numpy.array(numpy_rng.uniform( # initialize W uniformly |
| 22 | + low=-a, |
| 23 | + high=a, |
| 24 | + size=(n_visible, n_hidden))) |
| 25 | + |
| 26 | + W = initial_W |
| 27 | + |
| 28 | + if hbias is None: |
| 29 | + hbias = numpy.zeros(n_hidden) # initialize h bias 0 |
| 30 | + |
| 31 | + if vbias is None: |
| 32 | + vbias = numpy.zeros(n_visible) # initialize v bias 0 |
| 33 | + |
| 34 | + self.numpy_rng = numpy_rng |
| 35 | + self.x = input |
| 36 | + self.W = W |
| 37 | + self.W_prime = self.W.T |
| 38 | + self.hbias = hbias |
| 39 | + self.vbias = vbias |
| 40 | + |
| 41 | + # self.params = [self.W, self.hbias, self.vbias] |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + def get_corrupted_input(self, input, corruption_level): |
| 46 | + assert corruption_level < 1 |
| 47 | + |
| 48 | + return self.numpy_rng.binomial(size=input.shape, |
| 49 | + n=1, |
| 50 | + p=1-corruption_level) * input |
| 51 | + |
| 52 | + # Encode |
| 53 | + def get_hidden_values(self, input): |
| 54 | + return sigmoid(numpy.dot(input, self.W) + self.hbias) |
| 55 | + |
| 56 | + # Decode |
| 57 | + def get_reconstructed_input(self, hidden): |
| 58 | + return sigmoid(numpy.dot(hidden, self.W_prime) + self.vbias) |
| 59 | + |
| 60 | + |
| 61 | + def train(self, lr=0.1, corruption_level=0.3, input=None): |
| 62 | + if input is not None: |
| 63 | + self.x = input |
| 64 | + |
| 65 | + x = self.x |
| 66 | + tilde_x = self.get_corrupted_input(x, corruption_level) |
| 67 | + y = self.get_hidden_values(tilde_x) |
| 68 | + z = self.get_reconstructed_input(y) |
| 69 | + |
| 70 | + L_h2 = x - z |
| 71 | + L_h1 = numpy.dot(L_h2, self.W) * y * (1 - y) |
| 72 | + |
| 73 | + L_vbias = L_h2 |
| 74 | + L_hbias = L_h1 |
| 75 | + L_W = numpy.dot(tilde_x.T, L_h1) + numpy.dot(L_h2.T, y) |
| 76 | + |
| 77 | + |
| 78 | + self.W += lr * L_W |
| 79 | + self.hbias += lr * numpy.mean(L_hbias, axis=0) |
| 80 | + self.vbias += lr * numpy.mean(L_vbias, axis=0) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + def negative_log_likelihood(self, corruption_level=0.3): |
| 85 | + tilde_x = self.get_corrupted_input(self.x, corruption_level) |
| 86 | + y = self.get_hidden_values(tilde_x) |
| 87 | + z = self.get_reconstructed_input(y) |
| 88 | + |
| 89 | + cross_entropy = - numpy.mean( |
| 90 | + numpy.sum(self.x * numpy.log(z) + |
| 91 | + (1 - self.x) * numpy.log(1 - z), |
| 92 | + axis=1)) |
| 93 | + |
| 94 | + return cross_entropy |
| 95 | + |
| 96 | + |
| 97 | + def reconstruct(self, x): |
| 98 | + y = self.get_hidden_values(x) |
| 99 | + z = self.get_reconstructed_input(y) |
| 100 | + return z |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +def test_dA(learning_rate=0.1, corruption_level=0.3, training_epochs=50): |
| 105 | + data = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 106 | + [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 107 | + [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 108 | + [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 109 | + [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 110 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 111 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1], |
| 112 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1], |
| 113 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1], |
| 114 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0]]) |
| 115 | + |
| 116 | + rng = numpy.random.RandomState(123) |
| 117 | + |
| 118 | + # construct dA |
| 119 | + da = dA(input=data, n_visible=20, n_hidden=5, numpy_rng=rng) |
| 120 | + |
| 121 | + # train |
| 122 | + for epoch in xrange(training_epochs): |
| 123 | + da.train(lr=learning_rate, corruption_level=corruption_level) |
| 124 | + # cost = da.negative_log_likelihood(corruption_level=corruption_level) |
| 125 | + # print >> sys.stderr, 'Training epoch %d, cost is ' % epoch, cost |
| 126 | + # learning_rate *= 0.95 |
| 127 | + |
| 128 | + |
| 129 | + # test |
| 130 | + x = numpy.array([[1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1], |
| 131 | + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0]]) |
| 132 | + |
| 133 | + print da.reconstruct(x) |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + test_dA() |
0 commit comments