Building an Artificial Neural Network to make Predictions with Machine Learning.
Introduction Artificial Neural Networks (ANNs) are computational models inspired by the human brain's biological neural networks. They are designed to simulate the way humans learn and process information. By recognizing patterns, approximating functions, and optimizing tasks, ANNs serve as the backbone of many artificial intelligence (AI) applications. Biological Inspiration In the human brain, neurons are the fundamental units that transmit signals. Each neuron receives input, processes it, and passes the result to other neurons. ANNs replicate this behavior by using nodes (artificial neurons) organized in layers: Input Layer: Takes in raw data. Hidden Layers: Perform intermediate computations. Output Layer: Produces the final result. Each connection between neurons has an associated weight which is adjusted during training. Structure of an ANN An ANN typically consists of: Neurons: Basic processing units. Layers: Organized into input, hidden, and output. Weights and Biases: Parameters adjusted during training. Activation Function: Determines the output of a neuron. Common activation functions: Sigmoid: ( \sigma(x) = \frac{1}{1 + e^{-x}} ) ReLU (Rectified Linear Unit): ( f(x) = \max(0, x) ) Tanh: ( \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} ) Training an ANN Training involves adjusting the weights and biases to minimize error using a method called Backpropagation. The key steps are: Forward Propagation: Calculate outputs based on current weights. Loss Calculation: Compute the error (e.g., Mean Squared Error). Backpropagation: Adjust weights using gradients. Optimization: Apply an algorithm like Stochastic Gradient Descent (SGD) or Adam to update weights. Python Implementation of a Simple ANN (From Scratch) import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) def sigmoid_derivative(x): return x * (1 - x) # Input data (4 samples, 2 features) x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # Output data (4 samples, 1 output) y = np.array([[0], [1], [1], [0]]) # XOR problem # Seed random numbers for reproducibility np.random.seed(1) # Initialize weights randomly with mean 0 input_layer_neurons = 2 hidden_layer_neurons = 4 output_neurons = 1 weights_input_hidden = 2 * np.random.random((input_layer_neurons, hidden_layer_neurons)) - 1 weights_hidden_output = 2 * np.random.random((hidden_layer_neurons, output_neurons)) - 1 # Training process epochs = 10000 learning_rate = 0.1 for epoch in range(epochs): # Forward propagation hidden_input = np.dot(x, weights_input_hidden) hidden_output = sigmoid(hidden_input) final_input = np.dot(hidden_output, weights_hidden_output) predicted_output = sigmoid(final_input) # Backpropagation error = y - predicted_output d_predicted_output = error * sigmoid_derivative(predicted_output) error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T) d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_output) # Updating Weights weights_hidden_output += hidden_output.T.dot(d_predicted_output) * learning_rate weights_input_hidden += x.T.dot(d_hidden_layer) * learning_rate if epoch % 1000 == 0: loss = np.mean(np.square(error)) print(f"Epoch {epoch} Loss: {loss:.4f}") # Final output print("\nFinal Predicted Output:") print(predicted_output) Applications of ANN Computer Vision: Image classification, object detection Natural Language Processing (NLP): Language translation, sentiment analysis Healthcare: Disease prediction, medical image analysis Finance: Fraud detection, algorithmic trading Robotics: Autonomous navigation, control systems Summary Artificial Neural Networks are powerful tools capable of learning from data and making complex decisions. They consist of layers of interconnected neurons that adapt through training. As data-driven models, their effectiveness continues to grow with more data, better algorithms, and advanced hardware.

Introduction
Artificial Neural Networks (ANNs) are computational models inspired by the human brain's biological neural networks. They are designed to simulate the way humans learn and process information. By recognizing patterns, approximating functions, and optimizing tasks, ANNs serve as the backbone of many artificial intelligence (AI) applications.
Biological Inspiration
In the human brain, neurons are the fundamental units that transmit signals. Each neuron receives input, processes it, and passes the result to other neurons. ANNs replicate this behavior by using nodes (artificial neurons) organized in layers:
- Input Layer: Takes in raw data.
- Hidden Layers: Perform intermediate computations.
- Output Layer: Produces the final result.
Each connection between neurons has an associated weight which is adjusted during training.
Structure of an ANN
An ANN typically consists of:
- Neurons: Basic processing units.
- Layers: Organized into input, hidden, and output.
- Weights and Biases: Parameters adjusted during training.
- Activation Function: Determines the output of a neuron.
Common activation functions:
- Sigmoid: ( \sigma(x) = \frac{1}{1 + e^{-x}} )
- ReLU (Rectified Linear Unit): ( f(x) = \max(0, x) )
- Tanh: ( \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} )
Training an ANN
Training involves adjusting the weights and biases to minimize error using a method called Backpropagation. The key steps are:
- Forward Propagation: Calculate outputs based on current weights.
- Loss Calculation: Compute the error (e.g., Mean Squared Error).
- Backpropagation: Adjust weights using gradients.
- Optimization: Apply an algorithm like Stochastic Gradient Descent (SGD) or Adam to update weights.
Python Implementation of a Simple ANN (From Scratch)
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# Input data (4 samples, 2 features)
x = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
# Output data (4 samples, 1 output)
y = np.array([[0],
[1],
[1],
[0]]) # XOR problem
# Seed random numbers for reproducibility
np.random.seed(1)
# Initialize weights randomly with mean 0
input_layer_neurons = 2
hidden_layer_neurons = 4
output_neurons = 1
weights_input_hidden = 2 * np.random.random((input_layer_neurons, hidden_layer_neurons)) - 1
weights_hidden_output = 2 * np.random.random((hidden_layer_neurons, output_neurons)) - 1
# Training process
epochs = 10000
learning_rate = 0.1
for epoch in range(epochs):
# Forward propagation
hidden_input = np.dot(x, weights_input_hidden)
hidden_output = sigmoid(hidden_input)
final_input = np.dot(hidden_output, weights_hidden_output)
predicted_output = sigmoid(final_input)
# Backpropagation
error = y - predicted_output
d_predicted_output = error * sigmoid_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)
d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_output)
# Updating Weights
weights_hidden_output += hidden_output.T.dot(d_predicted_output) * learning_rate
weights_input_hidden += x.T.dot(d_hidden_layer) * learning_rate
if epoch % 1000 == 0:
loss = np.mean(np.square(error))
print(f"Epoch {epoch} Loss: {loss:.4f}")
# Final output
print("\nFinal Predicted Output:")
print(predicted_output)
Applications of ANN
- Computer Vision: Image classification, object detection
- Natural Language Processing (NLP): Language translation, sentiment analysis
- Healthcare: Disease prediction, medical image analysis
- Finance: Fraud detection, algorithmic trading
- Robotics: Autonomous navigation, control systems
Summary
Artificial Neural Networks are powerful tools capable of learning from data and making complex decisions. They consist of layers of interconnected neurons that adapt through training. As data-driven models, their effectiveness continues to grow with more data, better algorithms, and advanced hardware.