Example #1
0
def main(testing=True):
  images = mnist.load_images('../data/train-images-idx3-ubyte')  # 784 x 60000
  labels = mnist.load_labels('../data/train-labels-idx1-ubyte')  # 60000 x 1
  util.display_network(images[:,0:100])  # Show the first 100 images

  visible_size = 28*28
  hidden_size = 196
  sparsity_param = 0.1
  lamb = 3e-3
  beta = 3
  patches = images[:,0:10000]
  theta = autoencoder.initialize_parameters(hidden_size, visible_size)
  def sal(theta):
    return autoencoder.sparse_autoencoder_loss(theta, visible_size, hidden_size, lamb,
                                               sparsity_param, beta, patches)
  x, f, d = scipy.optimize.fmin_l_bfgs_b(sal, theta, maxfun=400, iprint=1, m=20)
  W1, W2, b1, b2 = autoencoder.unflatten(x, visible_size, hidden_size)
  util.display_network(W1.T)
Example #2
0
# - `hidden_size_l2` - layer 2 hidden size
# - `sparsity_param` - desired average activation of the hidden units
#   (\\(\\rho\\) in the lecture notes)
# - `lamb` - weight decay parameter
# - `beta` - weight of sparsity penalty term
input_size = 28 * 28
num_classes = 10
hidden_size_l1 = 200
hidden_size_l2 = 200
sparsity_param = 0.1
lamb = 3e-3
beta = 3

# === Step 1: Load data from the MNIST database ===
train_data = mnist.load_images('../data/train-images-idx3-ubyte')
train_labels = mnist.load_labels('../data/train-labels-idx1-ubyte')

# For debugging purposes, reduce the size of the input data in order
# to speed up gradient checking.  Here, we consider only the eight
# most-varying pixels of the images, and only the first 100 images.

if DEBUG:
    input_size = 64
    # only 100 datapoints
    train_data = train_data[:, :100]
    train_labels = train_labels[:100]
    # only top input_size most-varying input elements (pixels)
    indices = train_data.var(1).argsort()[-input_size:]
    train_data = np.asfortranarray(train_data[indices, :])

# === Step 2: Train the first sparse autoencoder ===
#  Here we define and initialise some constants which allow the code
#  to be used more generally on any arbitrary input.  We also
#  initialise some parameters used for tuning the model.

input_size = 28 * 28
num_classes = 10
lamb = 1e-4

# === Step 1: Load data ===
#
#  In this section, we load the input and output data.  For softmax
#  regression on MNIST pixels, the input data is the images, and the
#  output data is the labels.

images = mnist.load_images('../data/train-images-idx3-ubyte')
labels = mnist.load_labels('../data/train-labels-idx1-ubyte')
input_data = images

# For debugging purposes, reduce the size of the input data in order
# to speed up gradient checking.  Here, we consider only the eight
# most-varying pixels of the images, and only the first 100 images.

DEBUG = False
if DEBUG:
  input_size = 8
  # only 100 datapoints
  input_data = input_data[:, :100]
  labels = labels[:100]
  # only top input_size most-varying input elements (pixels)
  indices = input_data.var(1).argsort()[-input_size:]
  input_data = np.asfortranarray(input_data[indices, :])
# - `hidden_size_l2` - layer 2 hidden size
# - `sparsity_param` - desired average activation of the hidden units
#   (\\(\\rho\\) in the lecture notes)
# - `lamb` - weight decay parameter
# - `beta` - weight of sparsity penalty term
input_size = 28 * 28
num_classes = 10
hidden_size_l1 = 200
hidden_size_l2 = 200
sparsity_param = 0.1
lamb = 3e-3
beta = 3

# === Step 1: Load data from the MNIST database ===
train_data = mnist.load_images("../data/train-images-idx3-ubyte")
train_labels = mnist.load_labels("../data/train-labels-idx1-ubyte")

# For debugging purposes, reduce the size of the input data in order
# to speed up gradient checking.  Here, we consider only the eight
# most-varying pixels of the images, and only the first 100 images.

if DEBUG:
    input_size = 64
    # only 100 datapoints
    train_data = train_data[:, :100]
    train_labels = train_labels[:100]
    # only top input_size most-varying input elements (pixels)
    indices = train_data.var(1).argsort()[-input_size:]
    train_data = np.asfortranarray(train_data[indices, :])

# === Step 2: Train the first sparse autoencoder ===