AI Foundations for Developers COURSE·LESSON 04 OF 410 completed

Vectors, Matrices, and Tensors

Numbers with structure

A scalar is one number. A vector is an ordered list of numbers. A matrix has rows and columns. A tensor generalizes arrays to any number of dimensions. The word tensor does not imply special meaning in every application: a batch of RGB images can have shape [batch, height, width, channels].

For a ticket, a hand-built feature vector might be [word_count, has_invoice, days_since_purchase]. The order and units matter. Multiplying this vector by learned weights combines its features into a score.

Dot products and shapes

For x = [2, 3] and w = [4, -1], the dot product is 2×4 + 3×(-1) = 5. Adding bias b = 2 gives 7. A matrix applies several weighted combinations at once.

If a batch X has shape [8, 3] and weights W have shape [3, 5], XW has shape [8, 5]: eight examples, each mapped from three features to five outputs. The middle dimensions must match. This is a useful way to diagnose neural network errors before looking at values.

python
def dot(a, b):
    if len(a) != len(b):
        raise ValueError("Dimensions must match")
    return sum(x * y for x, y in zip(a, b))

x = [2, 3]
w = [4, -1]
print(dot(x, w) + 2)  # 7

Run this in the Python compiler. Change one weight and predict the result first.

Norms, distance, and scaling

The Euclidean norm of [3,4] is sqrt(3²+4²)=5. A unit vector divides a nonzero vector by its norm. Distance measures how far vectors are apart; a dot product also depends on their lengths. Later we will use cosine similarity to compare directions.

Features measured on very different scales can dominate distance or optimization. A purchase price around 1,000 may overwhelm a boolean represented by 0 or 1. Standardization often subtracts a training-set mean and divides by a training-set standard deviation. Save those statistics and reuse them at inference; recomputing them from test data leaks information.

Checkpoint with answers

What is the shape of [20, 4] × [4, 2]? [20, 2].

Does a 768-dimensional embedding contain 768 human-readable attributes? Usually no. Learned dimensions need not correspond to separate named concepts.

Exercise: Find the dot product of [1,0,-2] and [3,5,4]. Answer: 3 + 0 - 8 = -5. A negative score is valid; its interpretation depends on the model.

Learn at your own pace0 / 41 lessons complete