Build It With Python
Now let's build a very small neural network with Python. We will use NumPy to see the basic idea behind inputs, weights, predictions, loss, and learning without hiding everything behind a Deep Learning framework.
The simple idea
We will build a tiny neural network that takes two inputs and learns to predict whether their combined value belongs to one class or another. The goal is not to build a production model, but to understand what happens inside a neural network.
What Will We Build?
Our small model will receive two input values and produce one output.
We will train the model using simple examples so that it can learn a relationship between the inputs and the expected output.
Step 1 — Install NumPy
NumPy provides arrays and mathematical operations that make it easier to work with numerical data.
pip install numpy
If NumPy is already installed, you can skip this step.
Step 2 — Import NumPy
First, import NumPy into the Python program.
import numpy as np
We use np as a short name for NumPy.
Step 3 — Create Training Data
We need examples that the neural network can learn from.
import numpy as np
X = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
y = np.array([
[0],
[1],
[1],
[1]
])
Here, X contains our inputs and
y contains the correct answers.
The model should learn that the output should be 1 when at least one input is 1.
Step 4 — Create the Weights and Bias
A neural network needs parameters that can change during training. Two important parameters are weights and bias.
weights = np.array([
[0.0],
[0.0]
])
bias = 0.0
We start with simple values. The model will adjust them while learning.
Step 5 — Add an Activation Function
We will use the sigmoid function to convert the neuron's value into a number between 0 and 1.
def sigmoid(x):
return 1 / (1 + np.exp(-x))
For example, the sigmoid function produces values close to 0 for strongly negative inputs and values close to 1 for strongly positive inputs.
Step 6 — Make a Prediction
The model calculates a weighted sum of the inputs and then passes that value through the sigmoid function.
def predict(X):
z = np.dot(X, weights) + bias
return sigmoid(z)
The important calculation is:
z = X × weights + bias
Then:
prediction = sigmoid(z)
In simple words
The model takes the inputs, gives each input a weight, adds the bias, and converts the result into a prediction.
Step 7 — Measure the Error
The model needs to know how wrong its predictions are. We can calculate a simple loss value.
def loss(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
This uses Mean Squared Error for simplicity.
Step 8 — Train the Model
Now we need to adjust the weights so that the model's predictions become better.
For this small example, we will use gradient descent.
learning_rate = 0.1
for epoch in range(1000):
predictions = predict(X)
error = predictions - y
gradient_weights = np.dot(
X.T,
error
) / len(X)
gradient_bias = np.mean(error)
weights -= learning_rate * gradient_weights
bias -= learning_rate * gradient_bias
This loop repeatedly makes predictions, calculates the error, calculates gradients, and updates the weights and bias.
Step 9 — Complete Python Code
Here is the complete example in one place.
import numpy as np
# Training data
X = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
y = np.array([
[0],
[1],
[1],
[1]
])
# Model parameters
weights = np.array([
[0.0],
[0.0]
])
bias = 0.0
# Activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Prediction function
def predict(X):
z = np.dot(X, weights) + bias
return sigmoid(z)
# Training
learning_rate = 0.1
for epoch in range(1000):
predictions = predict(X)
error = predictions - y
gradient_weights = np.dot(
X.T,
error
) / len(X)
gradient_bias = np.mean(error)
weights -= learning_rate * gradient_weights
bias -= learning_rate * gradient_bias
# Test the model
predictions = predict(X)
print("Predictions:")
print(predictions)
print("Weights:")
print(weights)
print("Bias:")
print(bias)
What Happens When We Run the Code?
The model starts with weights of zero. Its first predictions are not useful.
Then the training loop repeatedly changes the weights. As training continues, the predictions move closer to the expected outputs.
Understanding the Predictions
The sigmoid function produces values between 0 and 1. For example, a prediction might look like:
0.02
0.98
0.97
0.99
We can interpret values close to 0 as one class and values close to 1 as the other class.
The exact threshold depends on the problem. A common starting point for binary classification is 0.5.
What Did We Actually Build?
We built a very small neural network with:
During training, gradient descent changes the weights and bias so that the predictions become better.
Important
This is intentionally a tiny educational neural network. Real Deep Learning models can contain many layers, millions of parameters, specialized loss functions, optimizers, and much larger datasets.
Why Are We Using NumPy Instead of TensorFlow?
NumPy lets us see the basic mathematics directly. Nothing is hidden behind a high-level Deep Learning framework.
Later in this course, we will use TensorFlow and Keras to build much larger and more practical neural networks.
Check Your Understanding
What are the inputs?
Two numerical input values stored in the
X array.
What do the weights do?
They determine how strongly each input affects
the neuron's calculation.
Why do we use a sigmoid function?
It converts the neuron's value into a number
between 0 and 1.
How does the model improve?
Gradient descent updates the weights and bias
based on the error.