Skip to content
← All cheat sheets

Cheat sheet · Area 00

Foundations

What AI, ML, deep learning and generative AI are

AI is the big goal of making computers do things that seem smart; machine learning is the main way we get there today, by learning from examples instead of following hand-written rules; deep learning and generative AI are smaller circles inside it.

  • AI ⊃ machine learning ⊃ deep learning ⊃ generative AI: each is a smaller circle inside the last.
  • Rules: a person writes the logic. Machine learning: the computer finds the logic from examples.
  • A model is the learned thing you keep after training; you send it inputs and it returns outputs.
  • Generative AI makes new content (text, images, code); an LLM is a generative model for text.

How a computer learns from examples

A computer learns by looking at many examples where the answer is known, adjusting itself until its guesses match those answers, and then using what it learned to answer new questions.

  • Example = features (what you know) + label (the answer you want predicted).
  • Training: show labelled examples, adjust the model until its guesses are close. Slow, done once or rarely.
  • Inference: use the trained model on new inputs. Fast, done millions of times. Calling an LLM API is inference.
  • Supervised = examples have labels. Unsupervised = no labels; find groups or structure.
  • Garbage in, garbage out: a model can only be as good as its examples.

Neural networks, starting from one neuron

A neural network is many tiny calculators (neurons) stacked in layers; each one multiplies its inputs by weights, adds them up and passes the result on, and together they can learn very complicated patterns.

  • Neuron: output = activation(inputs · weights + bias).
  • Weights say how much each input matters; the bias shifts how easily the neuron fires.
  • Activation (sigmoid, ReLU) adds a bend, so stacked layers can learn curves, not just straight lines.
  • Layers: input → hidden layers → output. 'Deep' learning = many hidden layers.
  • Parameters = all the weights and biases. GPT-class models have billions.

Weights, loss and gradient descent

Training means finding the weights that make the loss (how wrong the model is) as small as possible, and gradient descent does it by repeatedly taking a small step in whichever direction makes the loss go down.

  • Loss = one number saying how wrong the model is on the examples (e.g. mean squared error).
  • Gradient = the slope: which way, and how steeply, the loss goes up if you change each weight.
  • Gradient descent: weight = weight − learning_rate × gradient. Repeat.
  • Learning rate too big → bounces around or blows up; too small → painfully slow.
  • Backpropagation computes the gradient for every weight in a deep network efficiently.

Overfitting and train, validation and test splits

Overfitting is when a model memorises its training examples instead of learning the general pattern, so it looks perfect in training and fails on new data; you catch it by keeping some data aside that the model never learns from.

  • Overfitting = great on training data, bad on new data (memorised). Underfitting = bad on both (too simple).
  • Train set: learn from it. Validation set: tune choices on it. Test set: look once, at the end.
  • Typical split: 70/15/15 or 80/10/10. Split randomly, or by time for time-based data.
  • If test data leaks into training, your scores lie.
  • Same idea for LLM apps: keep an eval set you never tune your prompt on.

Vectors and matrices, just enough for AI

A vector is a list of numbers that can describe anything (a word, a photo, a customer), a matrix is a table of numbers, and almost all the maths inside AI is multiplying them together.

  • Vector = list of numbers, e.g. [0.9, 0.8, 0.0]. Its length is its dimension.
  • Dot product: multiply matching positions, add up. Big = pointing the same way = similar.
  • Cosine similarity = dot product of directions only, from −1 (opposite) to 1 (same direction).
  • Matrix @ vector = many dot products at once. A neural network layer is exactly this (plus an activation).
  • Shapes must line up: (2×3) @ (3,) → (2,).

Probability, just enough for AI

An LLM doesn't pick the next word for certain; it gives every possible word a probability, softmax turns its raw scores into those probabilities, and sampling picks one at random following them, which is why the same prompt can give different answers.

  • Probability: a number from 0 (never) to 1 (certain). A distribution's probabilities add up to 1.
  • Logits = raw scores from the model, any size. Softmax turns them into probabilities.
  • Sampling = random pick that follows the probabilities: 0.6 → picked about 60% of the time.
  • Temperature divides the logits first: low T = sharper, more predictable; high T = flatter, more varied.
  • Same prompt, different answers = sampling, not a bug.

Classic machine learning in one lesson

Before LLMs, most machine learning was (and much still is) three jobs, predicting a number (regression), predicting a category (classification) and finding groups (clustering), done with small, fast models like linear regression and decision trees.

  • Regression → a number (price). Classification → a category (spam/not). Clustering → groups, no labels.
  • Linear regression: best straight line. Decision tree: a learned flowchart of yes/no questions.
  • scikit-learn pattern: model = Model(); model.fit(X_train, y_train); model.predict(X_new); model.score(X_test, y_test).
  • Classic ML is cheap, fast and explainable; often better than an LLM for tables of numbers.

Python for AI engineers

Almost all AI work happens in Python, so you need a clean setup (a virtual environment per project, installed with uv or pip), notebooks for experiments, and two libraries, numpy for fast maths and pandas for tables.

  • One virtual environment per project: uv venv (or python -m venv .venv), then activate it.
  • Install: uv pip install pkg (fast) or pip install pkg. Record versions in pyproject.toml or requirements.txt.
  • Notebooks (Jupyter/VS Code) for exploring; .py files for anything you'll run again or deploy.
  • numpy = fast maths on arrays. pandas = tables (DataFrames): read_csv, head, groupby, agg.
  • Never commit API keys: read them from environment variables.

Calling an LLM API for the first time

Calling an LLM is just sending an HTTPS request with your API key, a model name and your messages to a provider like OpenAI or Anthropic, and reading the text that comes back, or doing the same for free against a model on your own laptop with Ollama.

  • An LLM API call = API key + model ID + messages → response text + token usage.
  • OpenAI: client.responses.create(model=..., input=...) → response.output_text.
  • Anthropic: client.messages.create(model=..., max_tokens=..., messages=[...]) → response.content[0].text.
  • Keys live in environment variables (OPENAI_API_KEY, ANTHROPIC_API_KEY); SDKs read them automatically.
  • Free and offline: Ollama runs open models locally (ollama pull llama3.2, then ollama.chat(...)).
  • You pay per token, input and output, so log usage from day one.

Where models come from, and what an AI engineer does

A few labs (OpenAI, Anthropic, Google, Meta, Mistral and others) train the big models and offer them either through paid APIs or as open weights you can download from hubs like Hugging Face, and an AI engineer's day is choosing between them and building reliable, safe, affordable products on top.

  • Closed (API-only): OpenAI GPT, Anthropic Claude, Google Gemini. Open weights: Meta Llama/Muse, Mistral, Qwen, Gemma, DeepSeek.
  • Hugging Face = the GitHub of models and datasets: download weights, read model cards, host demos.
  • Pick a model by quality on YOUR task, price, speed, context size, privacy needs and licence.
  • AI engineer's loop: prompt/context → build → evaluate → ship → monitor → improve.