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:
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: 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
1configuration (empty product). - Return a plain
int.
Notes
- Adding one more hyperparameter with choices multiplies the cost by — which is why exhaustive grid search becomes infeasible quickly.
▶ 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