Intro to Array Computing

You might have heard (or experienced) that Python is slow. So how can Python be the language behind basically all of the recent advances in AI, which all require huge amounts of computing? The secret is array computing. The Python code is orchestrating operations that happen on powerful “accelerator” hardware like GPUs and TPUs. Those operations typically involve repeatedly applying an operation to a big (usually rectangular) arrays of numbers, hence, array computing.

For those used to writing loops, this sort of coding can take some getting used to. Here are two exercises that previous students have found very helpful in getting their mind around how arrays work in PyTorch. (The concepts are basically identical in other libraries like TensorFlow, NumPy, and JAX.)

Objectives

Notebooks

The reference below is an AI-generated summary of the material in the notebook.

Dot Products

A dot product is a fundamental operation in neural networks, particularly in linear (Dense) layers. Key concepts:

Intuitions

Mathematical Form

Basic form: y = w1*x1 + w2*x2 + ... + wN*xN + b

Implementation Methods

  1. Using PyTorch’s built-in operations:
    • torch.dot(w, x) or w @ x
  2. Using elementwise operations:
    • Multiply corresponding elements: w * x
    • Sum the results: (w * x).sum()

Linear Transformations

A linear transformation is the basic building block of neural networks:

PyTorch Operations

Elementwise Operations

Reduction Operations

Common reduction methods:

Can be called as methods (x.sum()) or functions (torch.sum(x))

Mean Squared Error (MSE)

Common error metric for regression tasks.

Formula: MSE = (1/n)Σ(y_true - y_pred)²

Implementation steps:

  1. Compute residuals: y_true - y_pred
  2. Square residuals: (y_true - y_pred)**2
  3. Take mean: ((y_true - y_pred)**2).mean()

PyTorch provides built-in implementations:

Multidimensional Arrays

Key Concepts

Reduction Operations on Multiple Dimensions

Tensor Products

Linear Regression the Hard Way