This tutorial aims to introduce auditory modeling and optimization in PyTorch, a machine learning framework for the Python programming language. Some reasons to consider using PyTorch include:
- High-performance computing: computational models can be efficiently run at scale on (multiple) CPUs or GPUs with minimal code modification.
- Neural network compatibility: models can be integrated with artificial neural networks, which are readily implemented in PyTorch.
- Automatic differentiation: modern machine learning relies heavily on gradient descent algorithms, in which model parameters are optimized to minimize a loss function. This requires iteratively adjusting parameters according to the gradient of the loss function with respect to said parameters. Machine learning frameworks like PyTorch have automatic differentiation, meaning they automatically calculate and keep track of these gradients (greatly simplifying the code needed to train a model).
To illustrate some of these advantages for hearing science applications, the DEMO notebook walks through:
- A simple, differentiable auditory nerve model implemented in PyTorch
- Crudely simulating peripheral effects of hearing loss
- Defining a model-based loss function for hearing aid optimization
- Optimizing hearing aid parameters via gradient descent
The DEMO notebook was designed be run in Google Colab, which requires a Google account. The notebook can also be run locally with minimal Python 3.9+ requirements, though access to a CUDA-enabled GPU is highly recommended. The free-tier GPU available through Google Colab is sufficient.
auditory_machine_learning
|__ DEMO.ipynb <-- Main demo notebook (start here)
|__ example_datasets.py <-- PyTorch dataset classes to generate or load sound waveforms
|__ example_plots.py <-- Functions to generate visualizations in the demo notebook
|__ filters.py <-- Python filter implementations
|__ modules.py <-- PyTorch modules for auditory modeling
|__ utils.py <-- Helper functions for plotting and audio manipulation
|__ requirements.txt <-- Python packages needed to run this code
|__ data <-- Small dataset of 100 brief speech clips
|__ 000.wav
|__ 001.wav
...
|__ 099.wav
The code provided here has been adapted from my own research:
- Hearing loss simulation code
- Saddler, Dau, & McDermott (2025). Towards individualized models of hearing-impaired speech perception. Proc. ISCA Clarity.
- Simple auditory nerve model code
- Saddler & McDermott (2024). Models optimized for real-world tasks reveal the task-dependent necessity of precise temporal coding in hearing. Nat. Commun.
- Auditory model-based loss function optimization code
- Saddler & Francl et al. (2021). Speech denoising with auditory models. Proc. Interspeech.
Mark R. Saddler (marksa@dtu.dk)
