What Is a Neural Network?
Layers of connected neurons that learn complex patterns from data.
A neural network is made of layers of neurons. Each connection has a weight; each neuron combines its inputs and applies an activation.
How it learns
Data flows forward to produce a prediction. The error is measured, then flows backward (backpropagation) to nudge every weight. Repeat millions of times and the network learns.
Example
# Conceptual: one neuron computes a weighted sum + activation
import numpy as np
inputs = np.array([0.5, 0.9, 0.2])
weights = np.array([0.4, 0.7, 0.1])
bias = 0.1
z = np.dot(inputs, weights) + bias
activation = max(0, z) # ReLU
print(round(activation, 3)) # 0.95When to use it
- A vision team trains a convolutional neural network on 50,000 labelled photos to classify whether an image contains a defective product.
- A speech-recognition company uses a recurrent neural network to transcribe spoken words to text by processing audio as a time sequence.
- A recommendation system uses a dense neural network to learn a latent representation of users and items to predict click probability.
More examples
Single neuron by hand
Implements a single neuron from scratch: computes the weighted sum of inputs plus bias and passes it through a sigmoid activation.
import numpy as np
def neuron(inputs, weights, bias):
z = np.dot(inputs, weights) + bias
return 1 / (1 + np.exp(-z)) # sigmoid activation
x = np.array([0.5, -1.2, 3.0])
w = np.array([0.4, 0.8, -0.3])
b = 0.1
print(neuron(x, w, b))Two-layer network forward pass
Manually implements a forward pass through a two-layer network using matrix multiplication and a ReLU activation, showing the core data flow.
import numpy as np
def relu(x):
return np.maximum(0, x)
X = np.random.randn(4, 3) # 4 samples, 3 features
W1 = np.random.randn(3, 5) # hidden layer weights
b1 = np.zeros(5)
W2 = np.random.randn(5, 2) # output layer weights
b2 = np.zeros(2)
hidden = relu(X @ W1 + b1)
output = hidden @ W2 + b2
print(output.shape)Build the same network in PyTorch
Recreates the same two-layer architecture using PyTorch's nn.Sequential, showing how a framework reduces the manual math to a few lines.
import torch
import torch.nn as nn
net = nn.Sequential(
nn.Linear(3, 5),
nn.ReLU(),
nn.Linear(5, 2)
)
x = torch.randn(4, 3)
out = net(x)
print(out.shape) # torch.Size([4, 2])
Discussion