Neural networks are the building blocks of modern AI and power applications like self-driving cars, chatbots, and image recognition. Unlike traditional programs, which follow predefined rules, neural networks learn from data by recognizing patterns.
A neural network is a system of artificial neurons that work together to process information. It is inspired by how the human brain works.
Imagine a self-driving car trying to recognize a stop sign :

A neural network is made up of three layers:

Example: Recognizing handwritten digits (0-9) using a neural network.
Think of a neural network like a chef preparing a meal:
Neural networks come in different types, depending on what they are used for.
Think of different neural networks like different types of athletes:
Neural networks learn by adjusting their internal connections (weights and biases).
Example: Teaching a child to recognize apples
Neural networks learn in a similar way by:

Activation functions decide whether a neuron should “fire” (activate) based on its input.
Think of activation functions like a decision filter:
Think of backpropagation like a coach helping an athlete:
Problem: Traditional OCR (Optical Character Recognition) struggles with handwriting variations. Solution: A Convolutional Neural Network (CNN) is trained to recognize handwritten text.
Result: AI-powered OCR improves accuracy in reading handwritten text, reducing manual data entry.
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Define input, weights, and bias
inputs = np.array([1, 0]) # Binary input
weights = np.array([0.5, -0.5]) # Weight coefficients
bias = 0.1 # Bias term
# Compute output
output = sigmoid(np.dot(inputs, weights) + bias)
print("Output:", output)Output: 0.6456563062257954This code simulates a simple artificial neuron:
[1, 0] (binary values).[0.5, -0.5] (determines feature importance).0.1 (shifts activation threshold).import torch
import torch.nn as nn
# Define an MLP model
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = nn.Linear(2, 4) # Input layer to hidden layer
self.relu = nn.ReLU() # Activation function
self.fc2 = nn.Linear(4, 1) # Hidden layer to output layer
self.sigmoid = nn.Sigmoid() # Output activation
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.sigmoid(self.fc2(x))
return x
# Instantiate model
model = MLP()
print(model)MLP(
(fc1): Linear(in_features=2, out_features=4, bias=True)
(relu): ReLU()
(fc2): Linear(in_features=4, out_features=1, bias=True)
(sigmoid): Sigmoid()
)Importing Necessary Libraries:
torch: The main library for working with deep learning in Python.torch.nn: Contains tools to build neural networks.Defining the Neural Network (MLP Model):
MLP class is created to define the neural network.nn.Module, which is required for PyTorch models.__init__ method sets up two layers:fc1: Connects 2 input numbers to 4 neurons in a hidden layer.fc2: Connects 4 hidden neurons to 1 output neuron.0 and 1 (useful for yes/no decisions).How the Model Processes Data (forward Method):
fc1, then the ReLU activation is applied.fc2, followed by sigmoid activation.Creating and Printing the Model:
model = MLP() Creates the neural network print(model) Displays the structure of the model# Define loss function and optimizer
criterion = nn.BCELoss() # Binary Cross-Entropy Loss
optimizer = optim.Adam(model.parameters(), lr=0.01)
# Sample training data
X_train = torch.tensor([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
y_train = torch.tensor([[0.0], [1.0], [1.0], [0.0]]) # XOR dataset labels
# Training loop
epochs = 1000
for epoch in range(epochs):
optimizer.zero_grad()
outputs = model(X_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
if epoch % 200 == 0:
print(f"Epoch {epoch}, Loss: {loss.item()}")
# Final output after training
print("Final model output:", model(X_train))Epoch 0, Loss: 0.6961403489112854
Epoch 200, Loss: 0.22329354286193848
Epoch 400, Loss: 0.04864644259214401
Epoch 600, Loss: 0.019203947857022285
Epoch 800, Loss: 0.010208996012806892
Final model output: tensor([[0.0055],
[0.9953],
[0.9897],
[0.0047]], grad_fn=<SigmoidBackward0>)This code trains a neural network to learn the XOR function, a basic logic operation where:
0 XOR 0 = 00 XOR 1 = 11 XOR 0 = 11 XOR 1 = 0Step-by-Step Breakdown:
1. Define Loss Function & Optimizer
2. Define the Training Data (XOR Dataset)
X_train consists of 4 possible binary combinations (0s and 1s).y_train follows the XOR truth table.3. Training the Model (1000 Epochs)
4. Model Evaluation
X_train, and the results are displayed.The model prints loss values and final predictions. Let’s analyze them:
1. Loss at Different Epochs

2. Final Model Predictions
tensor([[0.0055],
[0.9953],
[0.9897],
[0.0047]])
These values represent the model’s predicted probabilities (between 0 and 1) after applying the Sigmoid activation function.

The model has successfully learned the XOR function!