optuna/optuna

A hyperparameter optimization framework

13,997 stars Python 10 components

13 hidden assumptions · 6-stage pipeline · 10 components

Like any codebase, this ml training makes assumptions it never checks — most are routine. The ones worth your attention are below.

Suggests hyperparameters for machine learning models using Bayesian optimization and early stopping

Users define objective functions that take Trial objects and return scalar values to optimize. The Study orchestrates an optimization loop where samplers analyze past trial results to suggest new hyperparameter combinations, trials execute user code with those parameters while reporting intermediate values, pruners decide whether to terminate trials early, and storage backends persist all results. This creates a feedback cycle where each trial's outcome improves future parameter suggestions.

Under the hood, the system uses 4 feedback loops, 4 data pools, 6 control points to manage its runtime behavior.

A 10-component ml training. 380 files analyzed. Data flows through 6 distinct pipeline stages.

Hidden Assumptions

Most of what this code assumes is routine. These 3 are the ones most likely to cause trouble here. The rest are minor; they're under "Show everything".

Worth your attention first

Silent parameter bound violations where later suggest calls with different bounds are ignored, causing optimization to explore wrong parameter spaces without error messages

Worth your attention first

Incorrect pruning decisions where trials are terminated based on wrong median calculations, potentially killing promising trials or keeping poor ones

Worth your attention first

Lost trial results during network failures, or corrupted study state when multiple processes modify the same study simultaneously without coordination

Show everything (10 more)
Domain

Assumes objective function values follow a reasonable distribution where Tree-structured Parzen Estimators can model good vs bad parameter regions - breaks with pathological objectives like random noise, step functions, or objectives with extreme outliers spanning orders of magnitude

If this fails: TPE sampler suggests poor parameters indefinitely without convergence warning, wasting compute resources on optimization that cannot succeed

optuna/samplers/_tpe/sampler.py:TPESampler.sample
Resource

Assumes sufficient memory and CPU to fit Gaussian Process models that scale quadratically with trial history - no checks for memory usage as trial count grows into thousands

If this fails: Out-of-memory crashes or extreme slowdown when GP model fitting requires more resources than available, with no graceful degradation to simpler samplers

optuna/samplers/_gp/sampler.py:GPSampler model fitting
Contract

Assumes user's objective function always returns a single numeric value (or tuple for multi-objective) and that the return type matches the study's direction count - no validation that objective(trial) returns expected shape

If this fails: Type errors or wrong optimization behavior when objective returns None, strings, lists, or wrong number of values for multi-objective studies, potentially corrupting study results

optuna/study/_optimize.py:Study.optimize
Scale

Assumes study history fits in memory for visualization with reasonable rendering time - no pagination or sampling for studies with hundreds of thousands of trials

If this fails: Browser crashes or multi-minute rendering delays when visualizing large studies, making analysis impossible for long-running optimizations

optuna/visualization/plot_optimization_history.py and other plot functions
Domain

Assumes intermediate values represent monotonic progress metrics (like validation loss decreasing or accuracy increasing) where early values predict final performance - pruning logic breaks if metrics oscillate or improve late in training

If this fails: Premature termination of trials that would eventually converge, especially in deep learning where validation metrics often fluctuate before stabilizing

optuna/trial/_trial.py:Trial.report
Environment

Assumes database schema matches expected table structure and that migrations have been properly applied - no version checking between Optuna code and database schema

If this fails: Cryptic SQL errors or silent data corruption when connecting to databases created with different Optuna versions that have incompatible schemas

optuna/storages/_rdb/storage.py:RDBStorage initialization
Contract

Assumes that when should_prune() returns True, the user will immediately raise TrialPruned - if user ignores the pruning signal and continues execution, the trial's intermediate values and final result are still recorded normally

If this fails: Inconsistent pruning behavior where some trials marked for pruning complete anyway, skewing sampler's understanding of which parameter regions were actually explored

optuna/trial/_trial.py:Trial.should_prune
Resource

Assumes objective functions are thread-safe or process-safe when n_jobs > 1, and that shared resources like GPU memory or database connections won't cause conflicts between parallel trials

If this fails: Race conditions, deadlocks, or resource conflicts when multiple trials access shared resources simultaneously, leading to crashes or incorrect results

optuna/study/_optimize.py:parallel trial execution with n_jobs
Temporal

Assumes that random sampling during warmup period provides representative coverage of the parameter space - if search space has rare but important regions, random sampling might miss them entirely

If this fails: TPE model trained on unrepresentative data leads to poor suggestions that avoid optimal parameter regions, reducing overall optimization effectiveness

optuna/samplers/_tpe/sampler.py:n_startup_trials warmup
Domain

Assumes parameter bounds make mathematical sense (low < high for numeric types, non-empty choices for categorical) and that log=True parameters have positive bounds - no validation of distribution consistency

If this fails: Infinite loops or NaN values when samplers try to generate parameters from invalid distributions, causing optimization to hang or crash

optuna/distributions/_base.py:BaseDistribution constraints

Open the standalone hidden-assumptions report for optuna →

How Data Flows Through the System

Users define objective functions that take Trial objects and return scalar values to optimize. The Study orchestrates an optimization loop where samplers analyze past trial results to suggest new hyperparameter combinations, trials execute user code with those parameters while reporting intermediate values, pruners decide whether to terminate trials early, and storage backends persist all results. This creates a feedback cycle where each trial's outcome improves future parameter suggestions.

  1. Create study with optimization configuration — create_study() instantiates a Study object with specified sampler (e.g. TPESampler), pruner (e.g. MedianPruner), storage backend (e.g. RDBStorage), and optimization direction (minimize/maximize)
  2. Generate trial with parameter suggestions — Study.ask() creates a new Trial object and invokes the sampler's sample() method, which analyzes past FrozenTrial results to suggest promising parameter values based on Bayesian optimization [FrozenTrial → Trial]
  3. Execute objective function with suggested parameters — User's objective function receives the Trial, calls trial.suggest_float/suggest_int/suggest_categorical to define the search space and get parameter values, then runs training/evaluation code with those hyperparameters [Trial → objective value]
  4. Report intermediate results for pruning decisions — During execution, objective function calls trial.report(value, step) to log intermediate metrics like validation loss, which pruners use to decide if the trial should be terminated early via trial.should_prune() [intermediate values → pruning decision]
  5. Complete trial and store results — Study.tell() marks the trial as complete with its final objective value, creates a FrozenTrial record, and persists all trial data (parameters, result, intermediate values) to the storage backend [objective value → FrozenTrial]
  6. Analyze results for visualization and next suggestions — Visualization functions like plot_optimization_history query completed FrozenTrial records from storage to generate plots, while samplers analyze the same data to improve future parameter suggestions [FrozenTrial → visualizations]

Data Models

The data structures that flow between stages — the contracts that hold the system together.

Trial optuna/trial/_trial.py
Object with trial_id: int, study_id: int, number: int, state: TrialState, params: dict[str, Any], distributions: dict[str, BaseDistribution], user_attrs: dict[str, Any], intermediate_values: dict[int, float], datetime_start: datetime, datetime_complete: datetime
Created by Study, populated with suggested parameters from Sampler, executed by user's objective function, and stored in Storage backend
Study optuna/study/_study.py
Object with study_id: int, study_name: str, direction: StudyDirection, user_attrs: dict[str, Any], system_attrs: dict[str, Any], sampler: BaseSampler, pruner: BasePruner, storage: BaseStorage
Created via create_study(), manages the optimization loop by coordinating trials, samplers, and pruners until termination criteria are met
BaseDistribution optuna/distributions/_base.py
Abstract base with subclasses like FloatDistribution(low: float, high: float, log: bool), IntDistribution(low: int, high: int, step: int), CategoricalDistribution(choices: Sequence[Any])
Defined when trial suggests parameters, used by samplers to understand parameter bounds and types, stored with trial results
FrozenTrial optuna/trial/_frozen.py
Immutable version of Trial with same fields but read-only, used for analysis and visualization
Created from completed Trial objects, passed to samplers for suggesting new parameters and to visualization functions for analysis
StudySummary optuna/study/_study_summary.py
Object with study_name: str, direction: StudyDirection, best_trial: FrozenTrial, user_attrs: dict[str, Any], system_attrs: dict[str, Any], n_trials: int, datetime_start: datetime
Generated by storage backends when querying study metadata, used for study selection and management operations

System Behavior

How the system operates at runtime — where data accumulates, what loops, what waits, and what controls what.

Data Pools

Trial History (database)
Persistent store of all completed trials including parameters, objective values, intermediate results, and metadata, used by samplers for Bayesian optimization
Study Registry (database)
Registry of active and completed optimization studies with their configuration, best results, and progress statistics
Intermediate Values Buffer (in-memory)
Temporary storage of step-by-step metrics during trial execution, used by pruners for early stopping decisions
Parameter Distribution Cache (in-memory)
Cached search space definitions to avoid recomputing distribution parameters across multiple sampling requests

Feedback Loops

Delays

Control Points

Technology Stack

SQLAlchemy (database)
Database ORM for persisting studies and trials across SQL databases with schema migration support
Alembic (database)
Database migration tool for evolving Optuna's storage schema across versions
NumPy (compute)
Numerical computing foundation for samplers and acquisition functions in Bayesian optimization
Plotly (library)
Interactive visualization backend for optimization history, parameter relationships, and Pareto fronts
Matplotlib (library)
Static plotting backend providing alternative visualizations for research and publication use
CMA-ES (library)
Evolution strategy optimization algorithm implemented as pluggable sampler for continuous parameter spaces
scikit-learn (library)
Feature importance calculation for hyperparameter analysis and visualization components
PyTorch/SciPy (compute)
Numerical backends for Gaussian Process sampler fitting and acquisition function optimization
YAML (serialization)
Configuration file parsing for CLI-based study execution and parameter specification
tqdm (library)
Progress bar display during long optimization runs with trial completion tracking

Key Components

Explore the interactive analysis

See the full architecture map, data flow, and code patterns visualization.

Analyze on CodeSea

Related Ml Training Repositories

Frequently Asked Questions

What is optuna used for?

Suggests hyperparameters for machine learning models using Bayesian optimization and early stopping optuna/optuna is a 10-component ml training written in Python. Data flows through 6 distinct pipeline stages. The codebase contains 380 files.

How is optuna architected?

optuna is organized into 5 architecture layers: Study Management, Optimization Algorithms, Early Stopping, Storage & Persistence, and 1 more. Data flows through 6 distinct pipeline stages. This layered structure keeps concerns separated and modules independent.

How does data flow through optuna?

Data moves through 6 stages: Create study with optimization configuration → Generate trial with parameter suggestions → Execute objective function with suggested parameters → Report intermediate results for pruning decisions → Complete trial and store results → .... Users define objective functions that take Trial objects and return scalar values to optimize. The Study orchestrates an optimization loop where samplers analyze past trial results to suggest new hyperparameter combinations, trials execute user code with those parameters while reporting intermediate values, pruners decide whether to terminate trials early, and storage backends persist all results. This creates a feedback cycle where each trial's outcome improves future parameter suggestions. This pipeline design reflects a complex multi-stage processing system.

What technologies does optuna use?

The core stack includes SQLAlchemy (Database ORM for persisting studies and trials across SQL databases with schema migration support), Alembic (Database migration tool for evolving Optuna's storage schema across versions), NumPy (Numerical computing foundation for samplers and acquisition functions in Bayesian optimization), Plotly (Interactive visualization backend for optimization history, parameter relationships, and Pareto fronts), Matplotlib (Static plotting backend providing alternative visualizations for research and publication use), CMA-ES (Evolution strategy optimization algorithm implemented as pluggable sampler for continuous parameter spaces), and 4 more. This broad technology surface reflects a mature project with many integration points.

What system dynamics does optuna have?

optuna exhibits 4 data pools (Trial History, Study Registry), 4 feedback loops, 6 control points, 4 delays. The feedback loops handle training-loop and convergence. These runtime behaviors shape how the system responds to load, failures, and configuration changes.

What design patterns does optuna use?

6 design patterns detected: Strategy Pattern for Optimization Algorithms, Observer Pattern for Trial Lifecycle Events, Template Method for Pruning Logic, Factory Pattern for Study Creation, Command Pattern for Trial Operations, and 1 more.

Analyzed on April 20, 2026 by CodeSea. Written by .