From 20d6e0963d92929a7594254e8b09ec9fe794f203 Mon Sep 17 00:00:00 2001 From: FederCO Date: Wed, 23 Oct 2024 23:41:45 -0300 Subject: [PATCH] update code to support Python 3 to folow http://neuralnetworksanddeeplearning.com/chap5.html --- src/mnist_loader.py | 14 +++++++------- src/network2.py | 18 ++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/mnist_loader.py b/src/mnist_loader.py index d17498c89..40999cd14 100644 --- a/src/mnist_loader.py +++ b/src/mnist_loader.py @@ -10,7 +10,7 @@ #### Libraries # Standard library -import cPickle +import pickle import gzip # Third-party libraries @@ -39,11 +39,11 @@ def load_data(): That's done in the wrapper function ``load_data_wrapper()``, see below. """ - f = gzip.open('../data/mnist.pkl.gz', 'rb') - training_data, validation_data, test_data = cPickle.load(f) - f.close() + with gzip.open('../data/mnist.pkl.gz', 'rb') as f: + training_data, validation_data, test_data = pickle.load(f, encoding='latin1') return (training_data, validation_data, test_data) + def load_data_wrapper(): """Return a tuple containing ``(training_data, validation_data, test_data)``. Based on ``load_data``, but the format is more @@ -68,11 +68,11 @@ def load_data_wrapper(): tr_d, va_d, te_d = load_data() training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]] training_results = [vectorized_result(y) for y in tr_d[1]] - training_data = zip(training_inputs, training_results) + training_data = list(zip(training_inputs, training_results)) validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]] - validation_data = zip(validation_inputs, va_d[1]) + validation_data = list(zip(validation_inputs, va_d[1])) test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]] - test_data = zip(test_inputs, te_d[1]) + test_data = list(zip(test_inputs, te_d[1])) return (training_data, validation_data, test_data) def vectorized_result(j): diff --git a/src/network2.py b/src/network2.py index e29e80c48..73788cfca 100644 --- a/src/network2.py +++ b/src/network2.py @@ -156,33 +156,31 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, n = len(training_data) evaluation_cost, evaluation_accuracy = [], [] training_cost, training_accuracy = [], [] - for j in xrange(epochs): + for j in range(epochs): random.shuffle(training_data) mini_batches = [ training_data[k:k+mini_batch_size] - for k in xrange(0, n, mini_batch_size)] + for k in range(0, n, mini_batch_size)] for mini_batch in mini_batches: self.update_mini_batch( mini_batch, eta, lmbda, len(training_data)) - print "Epoch %s training complete" % j + print("Epoch %s training complete" % j) if monitor_training_cost: cost = self.total_cost(training_data, lmbda) training_cost.append(cost) - print "Cost on training data: {}".format(cost) + print("Cost on training data: {}".format(cost)) if monitor_training_accuracy: accuracy = self.accuracy(training_data, convert=True) training_accuracy.append(accuracy) - print "Accuracy on training data: {} / {}".format( - accuracy, n) + print("Accuracy on training data: {} / {}".format(accuracy, n)) if monitor_evaluation_cost: cost = self.total_cost(evaluation_data, lmbda, convert=True) evaluation_cost.append(cost) - print "Cost on evaluation data: {}".format(cost) + print("Cost on evaluation data: {}".format(cost)) if monitor_evaluation_accuracy: accuracy = self.accuracy(evaluation_data) evaluation_accuracy.append(accuracy) - print "Accuracy on evaluation data: {} / {}".format( - self.accuracy(evaluation_data), n_data) + print("Accuracy on evaluation data: {} / {}".format(self.accuracy(evaluation_data), n_data)) print return evaluation_cost, evaluation_accuracy, \ training_cost, training_accuracy @@ -232,7 +230,7 @@ def backprop(self, x, y): # second-last layer, and so on. It's a renumbering of the # scheme in the book, used here to take advantage of the fact # that Python can use negative indices in lists. - for l in xrange(2, self.num_layers): + for l in range(2, self.num_layers): z = zs[-l] sp = sigmoid_prime(z) delta = np.dot(self.weights[-l+1].transpose(), delta) * sp