Intro to Neural Networks
Neural networks are the cornerstone of modern AI. This tutorial will guide you through the basic concepts, key components, and a simple implementation in Python.
What is a Neural Network?
A neural network is a computational model inspired by the human brain. It consists of layers of interconnected neurons that transform input data into meaningful output.
- Input Layer: Receives raw data.
- Hidden Layers: Perform feature extraction and transformation.
- Output Layer: Produces the final prediction.
How It Works
Each neuron computes a weighted sum of its inputs, adds a bias, and passes the result through an activation function:
output = activation( Σ (weight_i * input_i) + bias )
Training a network means adjusting the weights and biases to minimize the error between predictions and true values using backpropagation and gradient descent.
Simple Python Example
Below is a minimal implementation of a single‑layer perceptron that learns the logical AND function.
import numpy as np
# Training data for AND
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([0,0,0,1])
# Initialize weights and bias
w = np.random.rand(2)
b = np.random.rand(1)
def sigmoid(z):
return 1/(1+np.exp(-z))
def predict(x):
return sigmoid(np.dot(x,w)+b)
# Training
lr = 0.1
for epoch in range(1000):
for xi, yi in zip(X,y):
y_pred = predict(xi)
error = yi - y_pred
w += lr * error * xi
b += lr * error
print("Trained weights:", w)
print("Trained bias:", b)
print("Predictions:", [predict(xi) for xi in X])
Try It Yourself
What activation function is most commonly used in deep neural networks?