feat: add test backward neuron

This commit is contained in:
2025-06-07 09:47:05 +02:00
parent 39a88325c5
commit b2465410bf
2 changed files with 39 additions and 7 deletions

View File

@@ -32,7 +32,7 @@ def main():
network = NeuralNetwork([8, 16, 1])
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...")
while True:

View File

@@ -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,