diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..625b81c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "jupyter.jupyterServerType": "local" +} \ No newline at end of file diff --git "a/M\303\251todo SGD.py" "b/M\303\251todo SGD.py" new file mode 100644 index 0000000..9c99d9b --- /dev/null +++ "b/M\303\251todo SGD.py" @@ -0,0 +1,51 @@ + +import random + +def SGD(self, training_data, epochs, mini_batch_size, eta, + test_data=None): + + if test_data: + test_data = list(test_data) + n_test = len(test_data) + + for j in range(epochs): + random.shuffle(training_data) + mini_batches = [ training_data[k:k+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) + + if test_data: + print("Epoch {} : {} / {}".format(j,self.evaluate(test_data),n_test)) + else: + print("Epoch {} complete".format(j)) + +def update_mini_batch(self, mini_batch, eta): + + nabla_b = [np.zeros(b.shape) for b in self.biases] + nabla_w = [np.zeros(w.shape) for w in self.weights] + + for x, y in mini_batch: + delta_nabla_b, delta_nabla_w = self.backprop(x, y) + nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)] + nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)] + + self.weights = [w-(eta/len(mini_batch))*nw + for w, nw in zip(self.weights, nabla_w)] + + self.biases = [b-(eta/len(mini_batch))*nb + for b, nb in zip(self.biases, nabla_b)] + +def update_mini_batch(self, mini_batch, eta): + + nabla_b = [np.zeros(b.shape) for b in self.biases] + nabla_w = [np.zeros(w.shape) for w in self.weights] + for x, y in mini_batch: + delta_nabla_b, delta_nabla_w = self.backprop(x, y) + nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)] + nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)] + self.weights = [w-(eta/len(mini_batch))*nw + for w, nw in zip(self.weights, nabla_w)] + self.biases = [b-(eta/len(mini_batch))*nb + for b, nb in zip(self.biases, nabla_b)] \ No newline at end of file diff --git a/network.py b/network.py index 78629cf..eeac370 100644 --- a/network.py +++ b/network.py @@ -147,3 +147,4 @@ def sigmoid(z): def sigmoid_prime(z): """Derivative of the sigmoid function.""" return sigmoid(z)*(1-sigmoid(z)) +