mirror of
https://github.com/guezoloic/neural-network.git
synced 2026-01-25 09:34:23 +00:00
feat: add test backward neuron
This commit is contained in:
2
main.py
2
main.py
@@ -32,7 +32,7 @@ def main():
|
|||||||
network = NeuralNetwork([8, 16, 1])
|
network = NeuralNetwork([8, 16, 1])
|
||||||
|
|
||||||
print("Start training...")
|
print("Start training...")
|
||||||
train_network(network, verbose=True, size_data=size, epochs=5_000)
|
train_network(network, verbose=True, size_data=size, epochs=5_000, max_val=max_val)
|
||||||
print("End training...")
|
print("End training...")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@@ -90,7 +90,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 17,
|
"execution_count": 3,
|
||||||
"id": "ee0fdb39",
|
"id": "ee0fdb39",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -224,7 +224,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"id": "f6de25ea",
|
"id": "f6de25ea",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -272,7 +272,7 @@
|
|||||||
" # dz/dw = x\n",
|
" # dz/dw = x\n",
|
||||||
" dz_dw = x\n",
|
" dz_dw = x\n",
|
||||||
"\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",
|
"\n",
|
||||||
" # dz/db = 1\n",
|
" # dz/db = 1\n",
|
||||||
" dz_db = 1\n",
|
" dz_db = 1\n",
|
||||||
@@ -293,15 +293,47 @@
|
|||||||
"id": "0c9baabf",
|
"id": "0c9baabf",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
"\n",
|
||||||
"1. **derivates sigmoid, inputs, and lineear combination**:\n",
|
"1. **derivates sigmoid, inputs, and lineear combination**:\n",
|
||||||
|
" \n",
|
||||||
" \n",
|
" \n",
|
||||||
"2. **adjust each input weight**:\n",
|
"2. **adjust each input weight**:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"3. **adjust neuron bias**:\n",
|
"3. **adjust neuron bias**:\n",
|
||||||
"\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",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.13.3"
|
"version": "3.13.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user