From d7ae7227262c184833946aeab3d0055d5c74d319 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 16:21:23 +0000 Subject: [PATCH] Add learning rate parameter to Perceptron training functions Co-authored-by: leestott <2511341+leestott@users.noreply.github.com> --- .../03-Perceptron/Perceptron.ipynb | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb b/lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb index 3d3f57ac..7a82a008 100644 --- a/lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb +++ b/lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb @@ -232,7 +232,7 @@ }, "outputs": [], "source": [ - "def train(positive_examples, negative_examples, num_iterations = 100):\n", + "def train(positive_examples, negative_examples, num_iterations = 100, learning_rate = 0.01):\n", " num_dims = positive_examples.shape[1]\n", " \n", " # Initialize weights. \n", @@ -251,11 +251,11 @@ "\n", " z = np.dot(pos, weights) \n", " if z < 0: # positive example was classified as negative\n", - " weights = weights + pos.reshape(weights.shape)\n", + " weights = weights + learning_rate * pos.reshape(weights.shape)\n", "\n", " z = np.dot(neg, weights)\n", " if z >= 0: # negative example was classified as positive\n", - " weights = weights - neg.reshape(weights.shape)\n", + " weights = weights - learning_rate * neg.reshape(weights.shape)\n", " \n", " # Periodically, print out the current accuracy on all examples \n", " if i % report_frequency == 0: \n", @@ -268,6 +268,19 @@ " return weights" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Note on Learning Rate**: The `learning_rate` parameter (default `0.01`) controls how much we adjust the weights during each training step. This implements the gradient descent update formula:\n", + "\n", + "$$\\mathbf{w}^{\\tau + 1}=\\mathbf{w}^{\\tau} + \\eta \\mathbf{x}_{n} t_{n}$$\n", + "\n", + "- A larger learning rate (e.g., `1.0`) makes the perceptron learn faster but may overshoot the optimal solution\n", + "- A smaller learning rate (e.g., `0.001`) learns more slowly but may converge more precisely\n", + "- You can experiment by calling: `train(pos_examples, neg_examples, learning_rate=0.1)`\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -435,7 +448,7 @@ }, "outputs": [], "source": [ - "def train_graph(positive_examples, negative_examples, num_iterations = 100):\n", + "def train_graph(positive_examples, negative_examples, num_iterations = 100, learning_rate = 0.01):\n", " num_dims = positive_examples.shape[1]\n", " weights = np.zeros((num_dims,1)) # initialize weights\n", " \n", @@ -451,11 +464,11 @@ "\n", " z = np.dot(pos, weights) \n", " if z < 0:\n", - " weights = weights + pos.reshape(weights.shape)\n", + " weights = weights + learning_rate * pos.reshape(weights.shape)\n", "\n", " z = np.dot(neg, weights)\n", " if z >= 0:\n", - " weights = weights - neg.reshape(weights.shape)\n", + " weights = weights - learning_rate * neg.reshape(weights.shape)\n", " \n", " if i % report_frequency == 0: \n", " pos_out = np.dot(positive_examples, weights)\n", @@ -1077,4 +1090,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file