#169Hyperparameter grid-search sizeEasy

Hyperparameter grid-search size

Background

Because hyperparameters aren't learned, you find good ones by searching. A grid search tries every combination of candidate values. The number of runs is the product of the number of choices per hyperparameter:

configs=hchoicesh\text{configs} = \prod_{h} |\text{choices}_h|

This product explodes fast — the "curse of dimensionality" that motivates random and Bayesian search.

Problem statement

Implement grid_size(grid) returning the number of configurations in a hyperparameter grid.

Input

  • grid — a dict mapping each hyperparameter name to a list of candidate values.

Output

Returns an int: the product of the lengths of all the candidate lists.

Examples

Example 1

Input:  grid = {"lr": [0.1, 0.01], "batch": [16, 32, 64]}
Output: 6

Explanation: 2×3=62 \times 3 = 6 combinations.

Example 2 — empty grid

Input:  grid = {}
Output: 1

Explanation: the empty product is 1 (one trivial configuration).

Constraints

  • Multiply the lengths of every value list.
  • An empty grid has 1 configuration (empty product).
  • Return a plain int.

Notes

  • Adding one more hyperparameter with kk choices multiplies the cost by kk — which is why exhaustive grid search becomes infeasible quickly.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • example: two by three
  • reference: empty grid is the empty product
  • sample: three hyperparameters