PyTorch Hyperparameter Tuning - A Tutorial for spotPython

Thomas Bartz-BeielsteinarXiv 2023

Abstract

The goal of hyperparameter tuning (or hyperparameter optimization) is to optimize the hyperparameters to improve the performance of the machine or deep learning model. spotPython ("Sequential Parameter Optimization Toolbox in Python") is the Python version of the well-known hyperparameter tuner SPOT, which has been developed in the R programming environment for statistical analysis for over a decade. PyTorch is an optimized tensor library for deep learning using GPUs and CPUs. This document shows how to integrate the spotPython hyperparameter tuner into the PyTorch training workflow. As an example, the results of the CIFAR10 image classifier are used. In addition to an introduction to spotPython, this tutorial also includes a brief comparison with Ray Tune, a Python library for running experiments and tuning hyperparameters. This comparison is based on the PyTorch hyperparameter tuning tutorial. The advantages and disadvantages of both approaches are discussed. We show that spotPython achieves similar or even better results while being more flexible and transparent than Ray Tune.

Introduction

Hyperparameter optimization (HPO) is often the most computationally demanding phase of the deep learning pipeline. While industry-standard tools like Ray Tune provide powerful automation, they frequently rely on random search or multi-armed bandit strategies (such as Hyperband) that can function as "black boxes" for researchers. This lack of transparency can make it difficult to understand why specific configurations succeed or how hyperparameters interact.

This paper introduces spotPython, a Python-based evolution of the Sequential Parameter Optimization Toolbox (SPOT). By utilizing surrogate-model-based optimization (SMBO), spotPython offers a statistically grounded alternative that emphasizes explainability and efficiency, particularly in environments where hardware resources are limited or data privacy requires local processing.

Methodology and Technical Features

The core of spotPython is its use of Surrogate Model-Based Optimization (SMBO). Rather than sampling the hyperparameter space randomly, the tool constructs a mathematical surrogate model to approximate the relationship between hyperparameters and the target loss function. This model then guides the search toward the most promising regions of the parameter space.

Key Technical Capabilities

  • Surrogate Diversity: The framework supports a variety of models to approximate the cost function, including Kriging (Gaussian Process models), Random Forests, and any model compatible with scikit-learn.
  • Flexible Parameter Handling: spotPython natively manages numerical, boolean, and categorical (factor) hyperparameters, allowing for complex search spaces without manual encoding.
  • JSON-Based Configuration: To ensure reproducibility, hyperparameter spaces are defined in human-readable JSON files. This allows researchers to modify experimental designs without altering the underlying code.
  • Explainability Tools: The package includes built-in functions for sensitivity analysis and the generation of 3D contour plots, enabling researchers to visualize the "fitness landscape" and identify critical hyperparameter interactions.

Experimental Setup: CIFAR10

To demonstrate the integration of spotPython into a PyTorch workflow, we utilized the CIFAR10 dataset—a standard computer vision benchmark consisting of 60,000 32x32 color images across 10 classes. The model under optimization was a configurable Convolutional Neural Network (CNN). The tuning process involved selecting optimal values for layer sizes, learning rates, and optimizer types.

Results and Comparison

We conducted a head-to-head comparison between spotPython and the official PyTorch Ray Tune tutorial. The results highlighted several advantages of the SMBO approach:

  • Accuracy: spotPython achieved a validation accuracy of 0.5896, slightly outperforming the Ray Tune result of 0.5836.
  • Search Efficiency: Progress plots indicated that the surrogate model significantly refined results after the initial design phase, identifying lower-loss regions more rapidly than pure random sampling.
  • Architectural Flexibility: spotPython demonstrated superior flexibility by allowing the user to switch between 10 different PyTorch optimizers (e.g., AdamW, SGD, RAdam) within a single tuning run with minimal boilerplate code.

Discussion and Limitations

While spotPython provides high transparency and statistical depth, it is currently optimized for different use cases than Ray Tune.

  1. Parallelism: spotPython currently supports rudimentary parallel and distributed training. Ray Tune remains the preferred choice for large-scale industrial clusters requiring massive horizontal scaling.
  2. Human-in-the-Loop: spotPython encourages an iterative approach. By providing tools like gen_design_table, it allows users to inspect and validate the experimental design before committing significant GPU resources.

Conclusion

spotPython represents a powerful tool for researchers who require more than just automated tuning. By providing a suite of visualization and statistical modeling tools, it transforms hyperparameter optimization from a black-box task into an interpretable part of the research process. Future developments will focus on enhancing distributed capabilities and expanding the library of example implementations for diverse deep learning architectures.

Summary updated

BibTeX
@misc{bartzbeielstein2023pytorch,
  author = {Thomas Bartz-Beielstein},
  title = {{PyTorch Hyperparameter Tuning - A Tutorial for spotPython}},
  year = {2023},
  eprint = {2305.11930},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.11930},
}
Figure