Module 20 · Deep learning and neural networks · Lesson 64 of 66
How a neural network works
Neurons, weights, activations and layers, built by hand with NumPy.
In this lesson · about 20 minutes
- Explain what a neuron computes
- Write the sigmoid and ReLU activation functions
- Run a forward pass through one layer with matrix math
Deep learning means machine learning with neural networks that have several layers. They power image recognition, speech, translation and chatbots.
The building block is a neuron. It takes some numbers in, multiplies each by a weight, adds them up with a bias, and passes the total through an activation function:
output = activation(x₁·w₁ + x₂·w₂ + … + b)
The activation adds a bend, so that stacked neurons can learn curved patterns, not only straight lines. Two common ones:
- Sigmoid:
1 / (1 + e−z)squeezes any number into 0 to 1, handy for probabilities. - ReLU:
max(0, z)keeps positive numbers and turns negatives into 0. It's simple and fast, and most hidden layers use it.
The two activations
Exercise
Write the activations
Write two functions that work on whole NumPy arrays: sigmoid(z) returns 1 / (1 + e−z), and relu(z) returns z where it's positive and 0 elsewhere.
Layers are matrix math
A layer is many neurons reading the same inputs. Put each neuron's weights in one column of a matrix W, and the whole layer for a whole batch of rows is one line: relu(X @ W + b). A network is layers chained together: an input layer (the features), one or more hidden layers, and an output layer with one neuron per answer.
Training starts with random weights. The network predicts, a loss function measures how wrong it was, and gradient descent nudges every weight a little in the direction that lowers the loss. One pass over the training data is an epoch; training runs many.
Exercise
A forward pass
Four rows of two features are in X, a layer of three neurons has weights W (2 × 3) and biases b, and relu is defined. Compute the layer's output for every row as hidden, and end with it. Its shape should be (4, 3).
Code that runs before yours
import numpy as np X = np.array([[1.0, 2.0], [0.5, -1.0], [-3.0, 0.0], [2.0, 2.0]]) W = np.array([[0.2, -0.5, 1.0], [0.4, 0.3, -0.6]]) b = np.array([0.1, 0.0, -0.2]) relu = lambda z: np.maximum(0, z)
Exercises solved: 0 of 2
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.