From 46ea03a3fadd4955716882e5d1025ab47b969630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 7 May 2025 20:04:34 +0200 Subject: [PATCH] feat: add main and vocabulary text --- main.py | 30 ++++++++++++++++++++++++++++++ nnetwork.ipynb | 12 +++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..3b5912f --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +import math +import random + +class Neuron: + def __init__(self, isize: int) -> None: + self.isize = isize + self.weight = [random.uniform(0, 1) for _ in range(self.isize)] + self.bias = random.uniform(0, 1) + + def forward(self, inputs: list) -> float: + assert len(inputs) == self.isize, "error: incorrect inputs number" + total = sum(self.weight[i] * inputs[i] for i in range(self.isize)) + self.bias + return self.sigmoid(total) + + def sigmoid(self, x: float) -> float: + return 1/(1 + math.exp(-x)) + + # target needs to be between 0 and 1 + def train(self, inputs: list, target: float, learning_rate: float = 0.1): + z = sum(self.weight[i] * inputs[i] for i in range(self.isize)) + self.bias + output = self.sigmoid(z) + + error = output - target + d_sigmoid = output * (1 - output) + dz = error * d_sigmoid + + for i in range(self.isize): + self.weight[i] -= learning_rate * dz * inputs[i] + + self.bias -= learning_rate * dz diff --git a/nnetwork.ipynb b/nnetwork.ipynb index af3040a..c915634 100644 --- a/nnetwork.ipynb +++ b/nnetwork.ipynb @@ -19,7 +19,16 @@ "\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 **multiple *inputs*** (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...)." + "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" ] }, { @@ -115,6 +124,7 @@ "import math\n", "import random\n", "\n", + "# Neuron 3\n", "class Neuron:\n", " def __init__(self, isize: int) -> None:\n", " self.isize = isize\n",