Skip to content

Commit 53a3e68

Browse files
committed
Refactoring the propagation of data
1 parent 141eadc commit 53a3e68

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

code/backprop2.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
7474
print "Epoch %s: %s" % (
7575
j,
7676
sum(self.cost_cross_entropy(x, y)
77-
for (x, y) in training_data[:1000]))
77+
for (x, y) in training_data))
7878

7979
def backprop(self, training_data, T, eta, lmbda,
8080
cost, cost_derivative):
@@ -167,6 +167,30 @@ def evaluate_training_results(self, training_data):
167167
return sum(int(x == y)
168168
for x, y in zip(training_results, actual_training_results))
169169

170+
def initial_feedforward(self, input_data, j):
171+
"""
172+
Feedforward the elements ``x`` in the list ``input_data``
173+
through the network until the ``j``th layer. Return the list
174+
of activations from the ``j``th layer.
175+
"""
176+
for k in range(j):
177+
intermediate_data = [
178+
sigmoid_vec(np.dot(self.weights[k], x)+self.biases[k])
179+
for x in input_data]
180+
return intermediate_data
181+
182+
def final_feedforward(self, intermediate_data, j):
183+
"""
184+
Feedforward the elements ``x`` in the list
185+
``intermediate_data`` through the network to the output. The
186+
elements in ``intermediate_data`` are assumed to be inputs to
187+
the ``j``th layer."""
188+
for k in range(j, len(self.weights)):
189+
output_data = [
190+
sigmoid_vec(np.dot(self.weights[k], a)+self.biases[k])
191+
for a in intermediate_data]
192+
return output_data
193+
170194
#### Miscellaneous functions
171195
def minimal_cross_entropy(training_data):
172196
"""

code/blog/common_knowledge.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
import mnist_loader
1414

1515
# Third-party libraries
16+
import matplotlib
17+
import matplotlib.pyplot as plt
1618
import numpy as np
1719

1820

1921
#### Parameters
2022
# Size of the training sets. May range from 1000 to 12,500. Lower
2123
# will be faster, higher will give more accuracy.
2224
SIZE = 5000
23-
25+
# Number of hidden units in the autoencoder
26+
HIDDEN = 30
2427

2528
print "\nGenerating training data"
2629
training_data, _, _ = mnist_loader.load_data_nn()
@@ -30,11 +33,11 @@
3033
test = [x for x, _ in training_data[37500:37500+SIZE]]
3134

3235
print "\nFinding first autoencoder"
33-
ae_1 = Network([784, 30, 784])
36+
ae_1 = Network([784, HIDDEN, 784])
3437
ae_1.SGD(td_1, 4, 10, 0.01, 0.05)
3538

3639
print "\nFinding second autoencoder"
37-
ae_2 = Network([784, 30, 784])
40+
ae_2 = Network([784, HIDDEN, 784])
3841
ae_2.SGD(td_1, 4, 10, 0.01, 0.05)
3942

4043
print "\nGenerating encoded training data"
@@ -45,7 +48,7 @@
4548
encoded_training_data = zip(encoded_td_1, encoded_td_2)
4649

4750
print "\nFinding mapping between theories"
48-
net = Network([30, 60, 30])
51+
net = Network([HIDDEN, HIDDEN])
4952
net.SGD(encoded_training_data, 6, 10, 0.01, 0.05)
5053

5154
print """\nBaseline for comparison: decompress with the first autoencoder"""
@@ -55,7 +58,7 @@
5558
encoded_test_2 = [sigmoid_vec(np.dot(ae_2.weights[0], x)+ae_2.biases[0])
5659
for x in test]
5760
test_data = zip(encoded_test_1, encoded_test_2)
58-
net_baseline = Network([30, 784, 30])
61+
net_baseline = Network([HIDDEN, 784, HIDDEN])
5962
net_baseline.biases[0] = ae_1.biases[1]
6063
net_baseline.weights[0] = ae_1.weights[1]
6164
net_baseline.biases[1] = ae_2.biases[0]
@@ -70,3 +73,18 @@
7073
error = sum(np.linalg.norm(net.feedforward(x)-y, 1) for (x, y) in test_data)
7174
print "Average l1 error per training image: %s" % (error / SIZE,)
7275

76+
print "\nComputing fiducial image inputs"
77+
fiducial_images_1 = [
78+
ae_1.weights[0][j,:].reshape(28,28)/np.linalg.norm(net.weights[0][j,:])
79+
for j in range(HIDDEN)]
80+
fiducial_images_2 = [
81+
ae_2.weights[0][j,:].reshape(28,28)/np.linalg.norm(net.weights[0][j,:])
82+
for j in range(HIDDEN)]
83+
image = np.concatenate([np.concatenate(fiducial_images_1, axis=1),
84+
np.concatenate(fiducial_images_2, axis=1)])
85+
fig = plt.figure()
86+
ax = fig.add_subplot(111)
87+
ax.matshow(image, cmap = matplotlib.cm.binary)
88+
plt.xticks(np.array([]))
89+
plt.yticks(np.array([]))
90+
plt.show()

code/deep_autoencoder.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,6 @@ def train_nested_autoencoder_repl(
103103
j, double(self.initial_feedforward(training_data, j)),
104104
epochs, mini_batch_size, eta, lmbda)
105105

106-
def initial_feedforward(self, training_data, j):
107-
"""
108-
Feedforward the elements ``x`` in ``training_data`` through
109-
the network until the ``j``th layer. Return the list of
110-
activations.
111-
"""
112-
for k in range(j):
113-
training_data = [
114-
sigmoid_vec(np.dot(self.weights[k], x)+self.biases[k])
115-
for x in training_data]
116-
return training_data
117-
118-
def final_feedforward(self, data, j):
119-
"""
120-
Feedforward the elements ``x`` in ``data`` through the network
121-
to the output. The elements in ``data`` are assumed to be
122-
inputs to the ``j``th layer."""
123-
for k in range(j, len(self.weights)):
124-
data = [
125-
sigmoid_vec(np.dot(self.weights[k], a)+self.biases[k])
126-
for a in data]
127-
return data
128-
129106
def feature(self, j, k):
130107
"""
131108
Return the output if neuron number ``k`` in layer ``j`` is

code/mnist_autoencoder.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def train_autoencoder(hidden_units, training_data):
3333
"Return a trained autoencoder."
3434
autoencoder_training_data = [(x, x) for x, _ in training_data]
3535
net = Network([784, hidden_units, 784])
36-
net.SGD(autoencoder_training_data, 6, 10, 0.007, 0.05)
36+
net.SGD(autoencoder_training_data, 6, 10, 0.01, 0.05)
3737
return net
3838

3939
def plot_test_results(net, test_inputs, actual_test_results):
@@ -53,18 +53,31 @@ def plot_test_results(net, test_inputs, actual_test_results):
5353
plt.yticks(np.array([]))
5454
plt.show()
5555

56-
def classifier(hidden_units):
56+
def classifier(hidden_units, n_unlabeled_inputs, n_labeled_inputs):
5757
"""
58-
Train an autoencoder using the MNIST training data, and then use
59-
the autoencoder to create a classifier with a single hidden layer.
58+
Train a semi-supervised classifier. We begin with pretraining,
59+
creating an autoencoder which uses ``n_unlabeled_inputs`` from the
60+
MNIST training data. This is then converted into a classifier
61+
which is fine-tuned using the ``n_labeled_inputs``.
62+
63+
For comparison a classifier is also created which does not make
64+
use of the unlabeled data.
6065
"""
6166
training_data, test_inputs, actual_test_results = \
6267
mnist_loader.load_data_nn()
63-
net_ae = train_autoencoder(hidden_units, training_data)
68+
print "\nUsing pretraining and %s items of unlabeled data" %\
69+
n_unlabeled_inputs
70+
net_ae = train_autoencoder(hidden_units, training_data[:n_unlabeled_inputs])
6471
net_c = Network([784, hidden_units, 10])
6572
net_c.biases = net_ae.biases[:1]+[np.random.randn(10, 1)/np.sqrt(10)]
6673
net_c.weights = net_ae.weights[:1]+\
6774
[np.random.randn(10, hidden_units)/np.sqrt(10)]
68-
net_c.SGD(training_data, 6, 10, 0.007, 0.05)
69-
print net_c.evaluate(test_inputs, actual_test_results)
75+
net_c.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
76+
print "Result on test data: %s / %s" % (
77+
net_c.evaluate(test_inputs, actual_test_results), len(test_inputs))
78+
print "Training a network with %s items of training data" % n_labeled_inputs
79+
net = Network([784, hidden_units, 10])
80+
net.SGD(training_data[-n_labeled_inputs:], 300, 10, 0.01, 0.05)
81+
print "Result on test data: %s / %s" % (
82+
net.evaluate(test_inputs, actual_test_results), len(test_inputs))
7083
return net_c

0 commit comments

Comments
 (0)