Add learning rate parameter to Perceptron training functions
Co-authored-by: leestott <2511341+leestott@users.noreply.github.com>
This commit is contained in:
parent
edc99e61db
commit
d7ae722726
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue