import sac
from utils import ASSERT_SIZE, ASSERT_NO_NAN

MAX_PATCHES = 60000
images = utils.load_images("../data/train-images-idx3-ubyte")
labels_ = utils.load_labels("../data/train-labels-idx1-ubyte")
patches = images[:, 0:MAX_PATCHES]
labels = labels_[0:MAX_PATCHES]

# Note, this is the output from running mnist_train.py in the top level.
print "Reading edge detector."
fname = "data/numeral_sac.pickle"
f = open(fname, "r")
edge_detector_solution = pickle.load(f)

options = sac.SparseAutoEncoderOptions(28 * 28, 196, output_dir="output")

edge_detector = sac.SparseAutoEncoder(options, patches)
print "Computing edges."
edges, identity = edge_detector.feed_forward(images[:, 0:MAX_PATCHES],
                                             edge_detector_solution.W1,
                                             edge_detector_solution.W2,
                                             edge_detector_solution.b1,
                                             edge_detector_solution.b2)

# Note, this is the output of running digit_classifier.py in this directory.
fname = "recognizer_network.pickle"
f = open(fname, "r")
digit_classifier_solution = pickle.load(f)

print digit_classifier_solution.W1.shape
#!/usr/bin/python

import utils
import numpy as np
import scipy.optimize
import struct
import sac
import pickle

images = utils.load_images("data/train-images-idx3-ubyte")
labels = utils.load_labels("data/train-labels-idx1-ubyte")
utils.save_as_figure(images[:, 0:100], "output/input.png")

patches = images[:, 0:10000]
visible_size = 28 * 28
hidden_size = 196

options = sac.SparseAutoEncoderOptions(visible_size,
                                       hidden_size,
                                       output_dir="output",
                                       max_iterations=400)
network = sac.SparseAutoEncoder(options, patches)
answer = network.learn()

output = open("network.pickle", "w")
output.write(pickle.dumps(answer))
output.close()
Ejemplo n.º 3
0
    return numgrad


patchsize = 8
num_patches = 10000
visible_size = patchsize * patchsize
hidden_size = 25
target_activation = 0.01
lamb = 0.0001
beta = 3

patches = sampleIMAGES(patchsize, num_patches)
options = sac.SparseAutoEncoderOptions(visible_size,
                                       hidden_size,
                                       learning_rate=lamb,
                                       beta=beta,
                                       sparsity=target_activation,
                                       output_dir="ex4_output")
network = sac.SparseAutoEncoder(options, patches)

# def sal(theta):
#   return network.sparse_autoencoder(theta)
#
# theta = network.initialize_parameters()

#numgrad = compute_numerical_gradient(lambda x: sal(x)[0], theta)

# Eyeball the gradients
#print np.hstack([numgrad, grad])

#diff = linalg.norm(numgrad-grad) / linalg.norm(numgrad+grad)