PythiloLesson 1 of 15 · progress kept in this browser

Vectors and norms

To a neural network, everything is a vector: a list of numbers. An image is its pixels, a word is its embedding, and a layer's output is one number per unit. NumPy stores a vector as a one-dimensional array, and arithmetic works element by element.

The length of a vector is its norm. The usual one, the L2 norm, is the square root of the sum of squares, Pythagoras in any number of dimensions. The L1 norm adds absolute values instead. The distance between two vectors is the norm of their difference.

import numpy as np

v = np.array([3.0, 4.0])
print(np.sqrt(np.sum(v ** 2)))  # 5.0
print(np.sum(np.abs(v)))        # 7.0
print(np.linalg.norm(v))        # 5.0
print(v / np.linalg.norm(v))    # [0.6 0.8]

Dividing a vector by its norm gives a unit vector: same direction, length 1. Models normalise vectors this way before comparing directions. The zero vector has no direction, so it cannot be normalised.

Your task

Write l2_norm(v) returning the L2 norm of a list or array as a float, without np.linalg.norm. Write normalize(v) returning a NumPy array of length 1 in the same direction, raising ValueError for the zero vector, and distance(a, b) returning the L2 distance between two vectors.

The first run loads NumPy into this browser.