Example #1
0
 def train(self, training_data, epochs, mini_batch_size, eta, lmbda):
     """
     Train the DeepAutoencoder.  The ``training_data`` is a list of
     training inputs, ``x``, ``mini_batch_size`` is a single
     positive integer, and ``epochs``, ``eta``, ``lmbda`` are lists
     of parameters, with the different list members corresponding
     to the different stages of training.  For example, ``eta[0]``
     is the learning rate used for the first nested autoencoder,
     ``eta[1]`` is the learning rate for the second nested
     autoencoder, and so on.  ``eta[-1]`` is the learning rate used
     for the final stage of fine-tuning.
     """
     print "\nTraining a %s deep autoencoder" % ("-".join(
         [str(j) for j in self.sizes]), )
     training_data = double(training_data)
     cur_training_data = training_data[::]
     for j in range(len(self.layers) - 1):
         print "\nTraining the %s-%s-%s nested autoencoder" % (
             self.layers[j], self.layers[j + 1], self.layers[j])
         print "%s epochs, mini-batch size %s, eta = %s, lambda = %s" % (
             epochs[j], mini_batch_size, eta[j], lmbda[j])
         self.train_nested_autoencoder(j, cur_training_data, epochs[j],
                                       mini_batch_size, eta[j], lmbda[j])
         cur_training_data = [
             (sigmoid_vec(np.dot(net.weights[0], x) + net.biases[0]), ) * 2
             for (x, _) in cur_training_data
         ]
     print "\nFine-tuning network weights with backpropagation"
     print "%s epochs, mini-batch size %s, eta = %s, lambda = %s" % (
         epochs[-1], mini_batch_size, eta[-1], lmbda[-1])
     self.SGD(training_data, epochs[-1], mini_batch_size, eta[-1],
              lmbda[-1])
Example #2
0
 def train(self, training_data, epochs, mini_batch_size, eta,
           lmbda):
     """
     Train the DeepAutoencoder.  The ``training_data`` is a list of
     training inputs, ``x``, ``mini_batch_size`` is a single
     positive integer, and ``epochs``, ``eta``, ``lmbda`` are lists
     of parameters, with the different list members corresponding
     to the different stages of training.  For example, ``eta[0]``
     is the learning rate used for the first nested autoencoder,
     ``eta[1]`` is the learning rate for the second nested
     autoencoder, and so on.  ``eta[-1]`` is the learning rate used
     for the final stage of fine-tuning.
     """
     print "\nTraining a %s deep autoencoder" % (
         "-".join([str(j) for j in self.sizes]),)
     training_data = double(training_data)
     cur_training_data = training_data[::]
     for j in range(len(self.layers)-1):
         print "\nTraining the %s-%s-%s nested autoencoder" % (
             self.layers[j], self.layers[j+1], self.layers[j])
         print "%s epochs, mini-batch size %s, eta = %s, lambda = %s" % (
             epochs[j], mini_batch_size, eta[j], lmbda[j])
         self.train_nested_autoencoder(
             j, cur_training_data, epochs[j], mini_batch_size, eta[j],
             lmbda[j])
         cur_training_data = [
             (sigmoid_vec(np.dot(net.weights[0], x)+net.biases[0]),)*2
             for (x, _) in cur_training_data]
     print "\nFine-tuning network weights with backpropagation"
     print "%s epochs, mini-batch size %s, eta = %s, lambda = %s" % (
             epochs[-1], mini_batch_size, eta[-1], lmbda[-1])
     self.SGD(training_data, epochs[-1], mini_batch_size, eta[-1],
              lmbda[-1])
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]) for x in td_3
]
encoded_td_2 = [
    sigmoid_vec(np.dot(ae_2.weights[0], x) + ae_2.biases[0]) for x in td_3
]
encoded_training_data = zip(encoded_td_1, encoded_td_2)
print("\nFinding mapping between theories")
net = Network([HIDDEN, HIDDEN])
net.SGD(encoded_training_data, 6, 10, 0.01, 0.05)

print("""\nBaseline for comparison: decompress with the first autoencoder""")
print("""and compress with the second autoencoder""")
encoded_test_1 = [
    sigmoid_vec(np.dot(ae_1.weights[0], x) + ae_1.biases[0]) for x in test
]
encoded_test_2 = [
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])
                for x in td_3]
encoded_td_2 = [sigmoid_vec(np.dot(ae_2.weights[0], x)+ae_2.biases[0])
                for x in td_3]
encoded_training_data = zip(encoded_td_1, encoded_td_2)

print "\nFinding mapping between theories"
net = Network([HIDDEN, HIDDEN])
net.SGD(encoded_training_data, 6, 10, 0.01, 0.05)

print """\nBaseline for comparison: decompress with the first autoencoder"""
print """and compress with the second autoencoder"""
encoded_test_1 = [sigmoid_vec(np.dot(ae_1.weights[0], x)+ae_1.biases[0])
                  for x in test]
encoded_test_2 = [sigmoid_vec(np.dot(ae_2.weights[0], x)+ae_2.biases[0])
                  for x in test]