NanoBrainAI is a from-scratch neural network project developed in Python to explore the mathematical foundations of artificial intelligence through Linear Algebra, Differential Calculus, and Gradient-Based Optimization.
Created by Abdelkabir Ouadoukou, this repository presents a complete implementation of a basic artificial neuron without relying on machine learning frameworks. It focuses on clarity, mathematical rigor, and clean modular engineering to bridge the gap between theoretical mathematics and practical machine learning.
This project implements the core mechanics of a neuron:
- weighted sum
- sigmoid activation
- mean squared error loss
- gradient descent
- backpropagation
- JSON model persistence
The goal is not only to make the code work, but also to show the mathematics behind learning.
A neuron computes:
z = sum_{i=1..n} w_i x_i + b
Then applies a nonlinear activation:
a = Ο(z) = 1 / (1 + e^{-z})
The neuron learns by comparing its prediction (a) to the target (y), measuring the error, and updating its parameters to reduce that error.
The activation function is:
Ο(z) = 1 / (1 + e^{-z})
Its limits are: as z β +β, Ο(z) β 1; as z β -β, Ο(z) β 0.
So the output stays between 0 and 1, which makes it useful for a neuron that behaves like a smooth switch.
Starting from Ο(z) = (1 + e^{-z})^{-1} and differentiating gives:
Ο'(z) = e^{-z} / (1 + e^{-z})^2
A useful equivalent form is:
Ο'(z) = Ο(z) * (1 - Ο(z))
This identity is especially important in backpropagation.
For a single prediction:
L = (y - y_hat)^2
where y is the target and y_hat is the prediction.
The derivative with respect to the prediction is:
dL/d(y_hat) = 2 * (y_hat - y)
This tells the model how to correct itself.
For one neuron:
z = sum_i w_i x_i + b a = Ο(z) L = (a - y)^2
Using the chain rule:
dL/dw_i = dL/da * da/dz * dz/dw_i
with dL/da = 2 * (a - y) and da/dz = a * (1 - a) and dz/dw_i = x_i
Therefore:
dL/dw_i = 2 * (a - y) * a * (1 - a) * x_i
For the bias:
dL/db = 2 * (a - y) * a * (1 - a)
Each parameter is updated by:
w_i <- w_i - Ξ· * dL/dw_i
b <- b - Ξ· * dL/db
where Ξ· is the learning rate.
This is the mechanism that makes the neuron learn.
The neuron is trained on the AND gate:
[ [0,0] \mapsto 0,\quad [0,1] \mapsto 0,\quad [1,0] \mapsto 0,\quad [1,1] \mapsto 1 ]
This is a classic test because it shows whether the model can learn a simple logical function using gradients.
This project demonstrates that a neural network is not magic.
It is built from:
- vectors and weights
- affine transformations
- nonlinear activation
- error measurement
- derivatives
- iterative optimization
In other words, it is a mathematical machine that learns by reducing loss.
Possible next steps:
- multiple neurons per layer
- matrix-based implementation
- ReLU and softmax
- cross-entropy loss
- mini-batch training
- multi-layer perceptron
- visualization of loss curves
Built by Abdelkabir Ouadoukou as a scientific and educational AI project.
MIT License. See the LICENSE file for details.
This project is inspired by classical mathematics, neural network theory, and the idea that AI should be understandable from first principles.