1. Home
  2. Explore
  3. Computer Science
  4. Machine Learning Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Machine Learning Overview

Search as Reasoning

Introduction to Search

Search is a form of reasoning used when the world is deterministic and problems require planning, problem-solving, or thinking — not just reflexive mapping from percepts to actions. Examples include solving a Rubik's cube or finding the shortest path from point A to point B.

The Bitter Lesson (Rich Sutton) argues that general methods that leverage computation are ultimately the most effective. Search and learning are identified as the two methods that scale arbitrarily with compute. While 1950s symbolic AI relied solely on search and failed, modern AI combines learned models with search to tackle challenging problems.

Formalizing Search Problems

A search problem is an abstraction that formally defines a problem using the following components:

  • Start state: The initial state from which search begins.

  • Successors function: Given a state, returns a list of possible steps. Each step consists of:

    • Action (a label)

    • Cost (e.g., time)

    • Next state (the state reached after taking the action)

  • Is end function: Given a state, returns whether it is a goal state.

The objective is to find a solution: a sequence of actions that minimizes total cost from the start state to any end state.

Example: Travel Search Problem

A street with blocks numbered 1 through nnn. From block iii, you can:

  • Walk: to block i+1i+1i+1, cost 1 minute (if i<ni < ni<n)

  • Take a magic tram: to block 2i2i2i, cost 2 minutes (if 2i≤n2i \leq n2i≤n)

Search problem definition:

  • Start state: block 1

  • Successors at state iii: walk (cost 1, next i+1i+1i+1) and tram (cost 2, next 2i2i2i), with bounds checks

  • Is end: return true if state i=ni = ni=n

State Design: Incorporating Constraints

State must contain all information needed to evaluate future actions, costs, and successors. Keeping the state small is important for computational efficiency.

Constraint: Limited Tram Tickets

If only a fixed number of tickets are available, the state must include both location and remaining tickets.

  • Start state: TravelState(location=1, tickets=initial_tickets)

  • Successors for TravelState(loc, t):

    • Walk: cost 1, next state TravelState(loc+1, t) (always allowed)

    • Tram: cost 2, next state TravelState(2*loc, t-1) (only if t>0t > 0t>0)

  • Is end: check if location equals destination (ignores tickets — multiple end states possible)

Constraint: No Tram Twice in a Row

The state must also record the last action taken (e.g., boolean last_action_was_tram). Then in successors, the tram action is only allowed if the last action was not tram.

Exhaustive Search and Future Cost

The Future Cost Recurrence

Define future cost f(s)f(s)f(s) as the minimum cost from state sss to any end state. The recurrence is:

f(s)={0if s is an end statemin⁡(action,cost,s′)∈successors(s)(cost+f(s′))otherwisef(s) = \begin{cases} 0 & \text{if } s \text{ is an end state} \\ \min_{(action, cost, s') \in \text{successors}(s)} \big( cost + f(s') \big) & \text{otherwise} \end{cases}f(s)={0min(action,cost,s′)∈successors(s)​(cost+f(s′))​if s is an end stateotherwise​

The solution to the original problem is f(start)f(\text{start})f(start), along with the sequence of actions that achieves it.

Exhaustive Search Algorithm

Exhaustive search implements the recurrence directly via recursion, trying all possible sequences of actions. It returns a solution (cost + action sequence).

Pseudo-idea:

  • If state is end, return solution with cost 0 and empty steps.

  • Otherwise, for each successor, recursively compute the future solution from the next state.

  • Choose the successor with minimum total cost (cost+future_costcost + future\_costcost+future_cost).

Time complexity: Exponential in the number of states (worst case). Memory complexity: Linear in solution length (the recursion stack).

Problem: Cycles

If the search graph contains cycles, exhaustive search may not terminate (infinite recursion). Workaround: incorporate a step counter into the state (so each state is unique) and define a maximum step threshold. Any state exceeding the threshold is given infinite cost.

Dynamic Programming (Memoization)

Dynamic programming (DP) is exhaustive search plus caching. Before computing f(s)f(s)f(s), check a cache (dictionary) to see if f(s)f(s)f(s) was already computed. If so, return the cached value. After computing, store it in the cache.

Three changes to exhaustive search:

  1. Add a cache (e.g., dictionary mapping states to solutions).

  2. At the start of the future-solution function, if state is in cache, return cached solution.

  3. After computing the best solution, store it in cache.

Benefits

  • Time complexity: Reduces exponential to polynomial (usually number of states) — states are never explored more than once.

  • Memory complexity: Requires storing solutions for all states (linear in number of states).

When to Use DP

  • Useful when many paths converge to the same state (e.g., lattice structures). If every path is unique (like tree), DP gives no speedup.

  • Only practical when the number of states fits in memory. For very large state spaces (e.g., 101210^{12}1012), DP is infeasible.

Approximate Search

When exact search is intractable (e.g., state space too large), heuristic methods are used. Two common algorithms are best-of-n and beam search.

Best-of-N

Idea: Generate nnn random solutions (rollouts) and return the best one.

A policy is a function mapping a state to an action (deterministic or random). A rollout applies the policy from the start state until an end state (or a step limit) is reached.

  • Policy example: Uniform random – at each state, choose uniformly among available actions.

  • Rollout: execute policy, accumulate actions and cost.

Best-of-n algorithm:

  • Perform nnn rollouts (independent, can be parallelized).

  • Return the solution with minimum total cost.

Properties:

  • As n→∞n \to \inftyn→∞, converges to the optimal solution if the policy has positive probability on all actions (but may require exponentially many rollouts).

  • Embarrassingly parallel — no communication between workers.

  • Often used with an informed policy (e.g., a language model) to make it practical.

Beam Search

Idea: Maintain a set of up to kkk (beam width) partial solutions at each step. At each iteration, extend all partial solutions by one action, then keep only the kkk best (by total cost).

  • Beam width k=1k=1k=1 is greedy search.

  • As k→∞k \to \inftyk→∞, beam search becomes exhaustive search (bad idea – memory also exponential).

  • Deterministic (unlike best-of-n's stochastic sampling).

Algorithm outline:

  • Initialize candidates = [start state (empty solution)].

  • For step in 1..max_steps:

    • For each candidate, if it reached an end state, keep it; otherwise, for each successor, create a new candidate by appending the step.

    • Sort all new candidates by total cost.

    • Prune to keep only the top kkk (lowest cost).

  • Return the best candidate that reached an end state.

Properties:

  • Provides not only the best approximate solution but also alternatives (top kkk).

  • Memory usage is O(k⋅max steps)O(k \cdot \text{max steps})O(k⋅max steps) – controlled by beam width.

  • Useful when there is no good policy for sampling.

Test-Time Compute in Language Models

Language models produce a probability distribution over next tokens given a prefix. Combining search with learned costs (negative log probabilities) enables finding better responses through computation at test time.

Search problem formulation for language model generation:

  • State: a string consisting of the prompt plus the generated prefix.

  • Actions: choose the next token.

  • Cost: −log⁡P(next token∣current state)-\log P(\text{next token} \mid \text{current state})−logP(next token∣current state) — minimizing cost maximizes probability.

  • Is end: when a termination condition is met (e.g., end-of-sequence token or incomplete expression). Optionally, a verifier assigns a large negative cost (bonus) if the response is correct.

Best-of-N with Language Model Policy

A policy derived from the language model samples tokens according to the predicted distribution.

  • Rollout: starting from the prompt, repeatedly sample the next token using the policy until stopping condition.

  • Collect nnn rollouts; return the one with lowest total cost (highest probability, possibly plus verifier bonus).

This is a simple yet effective method: best-of-n sampling with a small model can compete with a much larger model.

Synergy of Search and Learning

  • Costs are learned from data (e.g., language model probabilities learned via supervised learning).

  • Search finds the best sequence given those costs.

  • Combining learned models with search is a powerful recipe for building robust AI systems.


This lecture covered the formalization of search problems, exact algorithms (exhaustive search, dynamic programming), and approximate algorithms (best-of-n, beam search). The key takeaway is that search remains relevant today, especially when coupled with learning.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent