mirror of
https://github.com/guezoloic/neural-network.git
synced 2026-01-25 03:34:21 +00:00
feat: add test backward neuron
This commit is contained in:
@@ -90,7 +90,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 3,
|
||||
"id": "ee0fdb39",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -224,7 +224,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "f6de25ea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -272,7 +272,7 @@
|
||||
" # dz/dw = x\n",
|
||||
" dz_dw = x\n",
|
||||
"\n",
|
||||
" assert len(dz_dw) >= self.isize, \"too many value for input size\"\n",
|
||||
" assert len(dz_dw) >= self.isize, \"too many values for input size\"\n",
|
||||
"\n",
|
||||
" # dz/db = 1\n",
|
||||
" dz_db = 1\n",
|
||||
@@ -293,15 +293,47 @@
|
||||
"id": "0c9baabf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The `backward()` method train the neuron by adjusting its weights and bias using **the gradient descent**. This is based on erros and gradient of the activation function:\n",
|
||||
"The `backward()` method train the neuron by adjusting its weights and bias using **the gradient descent**. This is based on erros between the neuron's prediction and the expected output, and gradient of the activation function:\n",
|
||||
"\n",
|
||||
"1. **derivates sigmoid, inputs, and lineear combination**:\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"2. **adjust each input weight**:\n",
|
||||
"\n",
|
||||
"3. **adjust neuron bias**:\n",
|
||||
"\n",
|
||||
"4. **return gradient vector**:"
|
||||
"4. **return gradient vector**:\n",
|
||||
"\n",
|
||||
"#### Test - Backward Pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"id": "e07b7881",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"5.279659636289641\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"target = [0, 0, 1, 0, 1] # 5 \n",
|
||||
"target_normalize = 5/31\n",
|
||||
"epoch = 200\n",
|
||||
"\n",
|
||||
"neuron = Neuron(len(target))\n",
|
||||
"\n",
|
||||
"for i in range(epoch):\n",
|
||||
" output = neuron.forward(target)\n",
|
||||
" error = 2 * (output - target_normalize)\n",
|
||||
" neuron.backward(target, error, 0.1)\n",
|
||||
"\n",
|
||||
"print(neuron.forward(target)*31)"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -321,7 +353,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.3"
|
||||
"version": "3.13.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user