diff --git a/README.md b/README.md index e31a3f1..c9aaf10 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ -# NeuralNetwork -A simple python neural network. +

Game of Life

+ +

+ Learn how to build and train a neural network in Python from scratch.
+ This project goes further by teaching how a network can convert any 8-bit binary number into its corresponding decimal value. +

+ +

+ + Python + + + GitHub Repo + +

+ +## Objective + +The goal of this project is to: +- Understand how artificial neurons, layers, and neural networks work. +- Implement all components of a neural network (no external libraries). +- Train the network to map 8-bit binary inputs to decimal values (from 0 to 255). + +## Prerequis + +## Links +- [Sigmoid derivation function](res/sigmoid.png) +- [Wikipedia](https://en.wikipedia.org/wiki/Activation_function) \ No newline at end of file diff --git a/nnetwork.ipynb b/nnetwork.ipynb index 932e1e9..5963811 100644 --- a/nnetwork.ipynb +++ b/nnetwork.ipynb @@ -1,5 +1,160 @@ { - "cells": [], + "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" + ] + } + ], "metadata": { "kernelspec": { "display_name": ".venv", @@ -22,3 +177,4 @@ "nbformat": 4, "nbformat_minor": 5 } + diff --git a/res/sigmoid.png b/res/sigmoid.png new file mode 100644 index 0000000..642fd19 Binary files /dev/null and b/res/sigmoid.png differ