fix: rework partial main.py (might change soon)

This commit is contained in:
2025-05-29 19:17:41 +02:00
parent ee9662f37f
commit ab2ee46422
2 changed files with 60 additions and 185 deletions

View File

@@ -1,160 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9b3f1635",
"metadata": {},
"source": [
"# Neural Network"
]
},
{
"cell_type": "markdown",
"id": "478651c8",
"metadata": {},
"source": [
"## What is a *Neuron* (artifical)\n",
"\n",
"First of all, **I'm not an Neurologist so i might say some nonsense, i only researched online**. \n",
"\n",
"An artifical *neuron* works similary to a biological *neuron* in the way it process information. In a brain, like yours, a *neuron* receives signals from other *neurons*, processes them and sends an *output*.\n",
"\n",
"An artifical *neuron* takes an **input** (such as numbers), applies updated values called **weights** to each *inputs*, adds a constant called **bias**, apply a specific function to normalize the value called **Activation function**, and then `returns` the *output* of the Activation function (such as: **sigmoid**, **ReLU**, etc...).\n",
"\n",
"## Vocabulary / key components\n",
"\n",
"1. **inputs**: inputs are usually a unique list of numbers, they are simply values sent to a neuron, which then process them.\n",
"\n",
"2. **weights**: weights are also a list of numbers that has the same size of inputs. The weight determines how important de the number of the input is. If it's high, the input matters. Else, if the weight is low, the number matters less.\n",
"\n",
"3. **bias**: the bias are constant that are added after all the inputs are multiplied by the weight. it helps shift the resultat up or down.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d9d6072",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"# Neuron 1\n",
"class Neuron:\n",
" def __init__(self, input_size: int) -> None:\n",
" self.input_size = input_size\n",
" self.weight = [random.uniform(0, 1) for _ in range(self.input_size)]\n",
" self.bias = random.uniform(0, 1)"
]
},
{
"cell_type": "markdown",
"id": "1aff9ee6",
"metadata": {},
"source": [
"# 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ca39a42",
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import random\n",
"\n",
"# Neuron 2\n",
"class Neuron:\n",
" def __init__(self, input_size: int) -> None:\n",
" self.input_size = input_size\n",
" self.weight = [random.uniform(0, 1) for _ in range(self.input_size)]\n",
" self.bias = random.uniform(0, 1)\n",
"\n",
" def sigmoid(x: float) -> float:\n",
" return 1/(1 + math.exp(-x))\n",
" \n",
" def forward(self, inputs: list) -> float:\n",
" assert len(inputs) == self.input_size, \"error: misnumber inputs number\"\n",
" total = sum(self.weight[i] * inputs[i] for i in range(self.isize)) + self.bias\n",
" return self.sigmoid(total)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6709c5c7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Neuron output : 0.9001175686881125\n"
]
}
],
"source": [
"# 8 for 8 bits (1 Byte)\n",
"nbits: int = 8\n",
"neuron = Neuron(nbits)\n",
"inputs: list = [1, 0, 1, 0, 0, 1, 1, 0] \n",
"\n",
"output = neuron.forward(inputs)\n",
"print(\"Neuron output :\", output)"
]
},
{
"cell_type": "markdown",
"id": "aa57ae8e",
"metadata": {},
"source": [
"# 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6de25ea",
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import random\n",
"\n",
"# Neuron 3\n",
"class Neuron:\n",
" def __init__(self, isize: int) -> None:\n",
" self.isize = isize\n",
" self.weight = [random.uniform(0, 1) for _ in range(self.isize)]\n",
" self.bias = random.uniform(0, 1)\n",
"\n",
" def forward(self, inputs: list) -> float:\n",
" assert len(inputs) == self.isize, \"error: incorrect inputs number\"\n",
" total = sum(self.weight[i] * inputs[i] for i in range(self.isize)) + self.bias\n",
" return self.sigmoid(total)\n",
" \n",
" def sigmoid(x: float) -> float:\n",
" return 1/(1 + math.exp(-x))\n",
"\n",
" # target needs to be between 0 and 1\n",
" def train(self, inputs: list, target: float, learning_rate: float = 0.1):\n",
" z = sum(self.weight[i] * inputs[i] for i in range(self.isize)) + self.bias\n",
" output = self.sigmoid(z)\n",
"\n",
" error = output - target\n",
" d_sigmoid = output * (1 - output)\n",
" dz = error * d_sigmoid\n",
"\n",
" for i in range(self.isize):\n",
" self.weight[i] -= learning_rate * dz * inputs[i]\n",
"\n",
" self.bias -= learning_rate * dz\n"
]
}
],
"cells": [],
"metadata": {
"kernelspec": {
"display_name": ".venv",