This is the code repository for Time Series with PyTorch, First Edition, published by Packt.
Graeme Davidson, Lei Ma
Neural networks are powerful tools for time-series forecasting, but applying them effectively requires both practical experience and a clear understanding of architectures, training strategies, and evaluation methods. This book brings these ideas together in a structured and practical way. Starting with PyTorch fundamentals, you will build neural networks from scratch and progress through recurrent networks, attention mechanisms, and transformers before exploring forecasting architectures such as N-BEATS, N-HiTS, and the Temporal Fusion Transformer. Along the way, you will learn robust hyperparameter tuning, conformal prediction for uncertainty estimation, and reliable evaluation practices. Unlike most forecasting books, this text also explores topics often overlooked or treated separately, including transfer learning across collections of series, synthetic data generation with diffusion models, and self-supervised representation learning. Beyond forecasting, later chapters cover classification, clustering, anomaly detection, and embeddings for large-scale time-series modeling. Throughout, the focus is pragmatic: theory is reinforced through experimentation and implementation so you can apply these methods confidently to real-world time-series problems.
- Build, train, and evaluate neural networks for time series using PyTorch and PyTorch Lightning. Tune models with Bayesian optimisation and validate them with suitable metrics and strategies.
- Progress from feedforward and recurrent networks to transformers and models such as N-BEATS, N-HiTS, and TFT.
- Learn how global models use cross- and transfer learning across many series.
- Generate synthetic series and representations with diffusion and self-supervised methods.
- Apply modern approaches to classification, clustering, and anomaly detection.
- Time Series for Everyone
- The Challenge of Time Series
- Evaluating Time-Series Models
- PyTorch Fundamentals
- Simple Neural Architecture
- Optimization
- Conformal Prediction
- Recurrent Neural Networks
- Transformers
- Other Neural Structures
- Transfer Learning and Global Modelling
- Synthetic Time Series Data
- Diffusion Models
- Time Series Classification
- Time Series Clustering
- Embeddings for Time Series
- Supervised and Unsupervised Anomaly Detection
- Self-Supervised Learning for Time Series
To follow the code examples in this book, you will need a working Python 3.10+ environment with PyTorch installed. We recommend using a virtual environment manager such as Poetry, uv or conda. A CUDA-capable GPU is beneficial for the later chapters but is not strictly required — most examples can be run on CPU, though training times will be longer.
The book assumes you are comfortable reading and writing Python, including working with pandas DataFrames and NumPy arrays. Some familiarity with basic statistics (means, standard deviations, hypothesis testing) and machine learning concepts (overfitting, cross-validation, loss functions) will help, though we revisit these where relevant. You do not need prior experience with PyTorch or deep learning — Chapter 4 covers the fundamentals.
Key libraries used throughout include PyTorch, Nixtla’s statsforecast, NeuralForecast, and MLForecast, scikit-learn, aeon, stumpy, and matplotlib. Installation instructions and version requirements are provided at the start of each chapter.
- Python>=3.11
- uv
- Install the required dependencies using uv:
uv sync --all-groups - Select the virtual environment created by uv in your IDE or terminal or jupyter.
Graeme Davidson is a Lead Data Scientist at Retail Express, where he redesigned the company's demand forecasting framework in line with contemporary statistical learning practices. His background spans cognitive neuroscience, researching implicit reward processing and human decision-making, through advertising analytics to research-focused demand forecasting. He is an active contributor to several data science Slack and Discord communities, an occasional competitor in forecasting competitions, and was approached by Packt in late 2022 to write the book he wished had existed when he first fell down an ARIMA rabbit hole chasing answers about how supermarkets actually forecast demand, and how a quantitative researcher models financial markets.
Lei Ma is a physicist-turned data scientist specializing in time series forecasting. He is theorist but has tackled real-world forecasting challenges across a variety of industries like housing, logistics, ecommerce, and manufacturing. Lei has led and delivered numerous forecasting projects where he combines deep expertise in building advanced time series models with a strategic approach to delivering holistic business insights. Lei creates time series forecasting tutorials online and joined the venture when Graeme approached him to collaborate on this book.
