feat: add main and vocabulary text

This commit is contained in:
2025-05-07 20:04:34 +02:00
parent cb9630b78a
commit 46ea03a3fa
2 changed files with 41 additions and 1 deletions

30
main.py Normal file
View File

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

View File

@@ -19,7 +19,16 @@
"\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", "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", "\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 math\n",
"import random\n", "import random\n",
"\n", "\n",
"# Neuron 3\n",
"class Neuron:\n", "class Neuron:\n",
" def __init__(self, isize: int) -> None:\n", " def __init__(self, isize: int) -> None:\n",
" self.isize = isize\n", " self.isize = isize\n",