1. Foundations of Neural Networks
From Biological Neurons to the Perceptron
The artificial neuron, or Perceptron, is a mathematical abstraction inspired by the biological neuron's structure of dendrites receiving inputs, a cell body processing them, and an axon firing signals. In the artificial model, inputs are weighted (representing synaptic strength), summed, and passed through an activation function.
Formally, a Perceptron performs a weighted sum of inputs plus a bias term, denoted as $z = w^T x + b$. This single-layer architecture was the first attempt at binary classification, though it remains restricted to linearly separable problems, as it lacks the complexity to represent non-linear decision boundaries.
import torch # Simple Perceptron implementation input_data = torch.tensor([0.5, -0.2, 0.1]) weights = torch.tensor([0.8, 0.3, -0.5]) bias = 0.1 # Linear transformation z = torch.dot(input_data, weights) + bias # Heaviside step function: output 1 if z > 0 else 0 output = 1.0 if z > 0 else 0.0 print(f'Perceptron Output: {output}')
Mathematical Intuition of Activation Functions
Activation functions serve as the critical decision-making nodes, introducing non-linear transformations into the network. Without these, even deep networks would collapse into a single linear transformation, losing the ability to learn complex patterns. Sigmoid maps inputs to (0, 1) but suffers from vanishing gradients, whereas ReLU (Rectified Linear Unit) has become the standard due to its computational efficiency and sparsity.
Tanh rescales outputs to (-1, 1), centering data around zero, which often facilitates faster convergence during training. Softmax, typically used in the final layer, normalizes a vector of raw scores into a probability distribution, ensuring all outputs are positive and sum to exactly one.
import torch.nn.functional as F logits = torch.tensor([2.0, 1.0, 0.1]) # ReLU activation: keeps positive values, zeros out negative ones relu_out = F.relu(logits) # Softmax activation: maps logits to probability space probs = F.softmax(logits, dim=0) print(f'ReLU: {relu_out}, Softmax: {probs}')
MLP Architecture and Layer Dimensions
The Multi-Layer Perceptron (MLP) extends the single-layer perceptron by adding one or more 'hidden' layers. Each layer consists of a weight matrix of shape (input_features, output_features) and a bias vector. The architecture is defined by the transformation: $h = f(W x + b)$.
To design an MLP, one must ensure dimensional compatibility: the number of neurons in layer L must match the input dimension of the weight matrix in layer L+1. This chain-like propagation allows for hierarchical feature extraction, where lower layers learn edges or basic shapes, and deeper layers represent high-level semantic concepts.
import torch.nn as nn # Defining a simple 3-layer MLP model = nn.Sequential( nn.Linear(in_features=10, out_features=20), nn.ReLU(), nn.Linear(in_features=20, out_features=5) ) # Layer 1: 10 input neurons -> 20 neurons, Layer 2: 20 -> 5 output neurons print(model)
Knowledge Check
Ready to test your understanding of 1. Foundations of Neural Networks?