def classifier(hidden_units, n_unlabeled_inputs, n_labeled_inputs):
    """
    Train a semi-supervised classifier.  We begin with pretraining,
    creating an autoencoder which uses ``n_unlabeled_inputs`` from the
    MNIST training data.  This is then converted into a classifier
    which is fine-tuned using the ``n_labeled_inputs``.

    For comparison a classifier is also created which does not make
    use of the unlabeled data.
    """
    training_data, test_inputs, actual_test_results = \
        mnist_loader.load_data_nn()
    print "\nUsing pretraining and %s items of unlabeled data" %\
        n_unlabeled_inputs
    net_ae = train_autoencoder(hidden_units,
                               training_data[:n_unlabeled_inputs])
    net_c = Network([784, hidden_units, 10])
    net_c.biases = net_ae.biases[:1] + [np.random.randn(10, 1) / np.sqrt(10)]
    net_c.weights = net_ae.weights[:1]+\
        [np.random.randn(10, hidden_units)/np.sqrt(10)]
    net_c.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
    print "Result on test data: %s / %s" % (net_c.evaluate(
        test_inputs, actual_test_results), len(test_inputs))
    print "Training a network with %s items of training data" % n_labeled_inputs
    net = Network([784, hidden_units, 10])
    net.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
    print "Result on test data: %s / %s" % (net.evaluate(
        test_inputs, actual_test_results), len(test_inputs))
    return net_c
def classifier(hidden_units, n_unlabeled_inputs, n_labeled_inputs):
    """
    Train a semi-supervised classifier.  We begin with pretraining,
    creating an autoencoder which uses ``n_unlabeled_inputs`` from the
    MNIST training data.  This is then converted into a classifier
    which is fine-tuned using the ``n_labeled_inputs``.

    For comparison a classifier is also created which does not make
    use of the unlabeled data.
    """
    training_data, test_inputs, actual_test_results = \
        mnist_loader.load_data_nn()
    print "\nUsing pretraining and %s items of unlabeled data" %\
        n_unlabeled_inputs
    net_ae = train_autoencoder(hidden_units, training_data[:n_unlabeled_inputs])
    net_c = Network([784, hidden_units, 10])
    net_c.biases = net_ae.biases[:1]+[np.random.randn(10, 1)/np.sqrt(10)]
    net_c.weights = net_ae.weights[:1]+\
        [np.random.randn(10, hidden_units)/np.sqrt(10)]
    net_c.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
    print "Result on test data: %s / %s" % (
        net_c.evaluate(test_inputs, actual_test_results), len(test_inputs))
    print "Training a network with %s items of training data" % n_labeled_inputs
    net = Network([784, hidden_units, 10])
    net.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
    print "Result on test data: %s / %s" % (
        net.evaluate(test_inputs, actual_test_results), len(test_inputs))
    return net_c
def autoencoder_results(hidden_units):
    """
    Train an autoencoder using the MNIST training data and plot the
    results when the first ten MNIST test images are passed through
    the autoencoder.
    """
    training_data, test_inputs, actual_test_results = \
        mnist_loader.load_data_nn()
    net = train_autoencoder(hidden_units, training_data)
    plot_test_results(net, test_inputs)
def autoencoder_results(hidden_units):
    """
    Train an autoencoder using the MNIST training data and plot the
    results when the first ten MNIST test images are passed through
    the autoencoder.
    """
    training_data, test_inputs, actual_test_results = \
        mnist_loader.load_data_nn()
    net = train_autoencoder(hidden_units, training_data)
    plot_test_results(net, test_inputs)
Use PCA to reconstruct some of the MNIST test digits.
"""

# My libraries
import mnist_loader

# Third-party libraries
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import RandomizedPCA


# Training
training_data, test_inputs, actual_test_results = mnist_loader.load_data_nn()
pca = RandomizedPCA(n_components=30)
nn_images = [x for (x, y) in training_data]
pca_images = np.concatenate(nn_images, axis=1).transpose()
pca_r = pca.fit(pca_images)

# Try PCA on first ten test images
test_images = np.array(test_inputs[:10]).reshape((10,784))
test_outputs = pca_r.inverse_transform(pca_r.transform(test_images))

# Plot the first ten test images and the corresponding outputs
fig = plt.figure()
ax = fig.add_subplot(111)
images_in = [test_inputs[j].reshape(-1, 28) for j in range(10)]
images_out = [test_outputs[j].reshape(-1, 28) for j in range(10)]
image_in = np.concatenate(images_in, axis=1)
import mnist_loader

# Third-party libraries
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

#### Parameters
# Size of the training sets.  May range from 1000 to 12,500.  Lower
# will be faster, higher will give more accuracy.
SIZE = 5000
# Number of hidden units in the autoencoder
HIDDEN = 30

print("\nGenerating training data")
training_data, _, _ = mnist_loader.load_data_nn()
td_1 = [(x, x) for x, _ in training_data[0:SIZE]]
td_2 = [(x, x) for x, _ in training_data[12500:12500 + SIZE]]
td_3 = [x for x, _ in training_data[25000:25000 + SIZE]]
test = [x for x, _ in training_data[37500:37500 + SIZE]]

print("\nFinding first autoencoder")
ae_1 = Network([784, HIDDEN, 784])
ae_1.SGD(td_1, 4, 10, 0.01, 0.05)

print("\nFinding second autoencoder")
ae_2 = Network([784, HIDDEN, 784])
ae_2.SGD(td_1, 4, 10, 0.01, 0.05)

print("\nGenerating encoded training data")
encoded_td_1 = [
# Third-party libraries
import matplotlib
import matplotlib.pyplot as plt
import numpy as np


#### Parameters
# Size of the training sets.  May range from 1000 to 12,500.  Lower
# will be faster, higher will give more accuracy.
SIZE = 5000 
# Number of hidden units in the autoencoder
HIDDEN = 30

print "\nGenerating training data"
training_data, _, _ = mnist_loader.load_data_nn()
td_1 = [(x, x) for x, _ in training_data[0:SIZE]]
td_2 = [(x, x) for x, _ in training_data[12500:12500+SIZE]]
td_3 = [x for x, _ in training_data[25000:25000+SIZE]]
test = [x for x, _ in training_data[37500:37500+SIZE]]

print "\nFinding first autoencoder"
ae_1 = Network([784, HIDDEN, 784])
ae_1.SGD(td_1, 4, 10, 0.01, 0.05)

print "\nFinding second autoencoder"
ae_2 = Network([784, HIDDEN, 784])
ae_2.SGD(td_1, 4, 10, 0.01, 0.05)

print "\nGenerating encoded training data"
encoded_td_1 = [sigmoid_vec(np.dot(ae_1.weights[0], x)+ae_1.biases[0])
Esempio n. 8
0
~~~~~~~~~

Use PCA to reconstruct some of the MNIST test digits.
"""

# My libraries
import mnist_loader

# Third-party libraries
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import RandomizedPCA

# Training
training_data, test_inputs, actual_test_results = mnist_loader.load_data_nn()
pca = RandomizedPCA(n_components=30)
nn_images = [x for (x, y) in training_data]
pca_images = np.concatenate(nn_images, axis=1).transpose()
pca_r = pca.fit(pca_images)

# Try PCA on first ten test images
test_images = np.array(test_inputs[:10]).reshape((10, 784))
test_outputs = pca_r.inverse_transform(pca_r.transform(test_images))

# Plot the first ten test images and the corresponding outputs
fig = plt.figure()
ax = fig.add_subplot(111)
images_in = [test_inputs[j].reshape(-1, 28) for j in range(10)]
images_out = [test_outputs[j].reshape(-1, 28) for j in range(10)]
image_in = np.concatenate(images_in, axis=1)