Reader small image

You're reading from  Deep Learning with PyTorch Lightning

Product typeBook
Published inApr 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800561618
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Kunal Sawarkar
Kunal Sawarkar
author image
Kunal Sawarkar

Kunal Sawarkar is a chief data scientist and AI thought leader. He leads the worldwide partner ecosystem in building innovative AI products. He also serves as an advisory board member and an angel investor. He holds a master's degree from Harvard University with major coursework in applied statistics. He has been applying machine learning to solve previously unsolved problems in industry and society, with a special focus on deep learning and self-supervised learning. Kunal has led various AI product R&D labs and has 20+ patents and papers published in this field. When not diving into data, he loves doing rock climbing and learning to fly aircraft, in addition to an insatiable curiosity for astronomy and wildlife.
Read more about Kunal Sawarkar

Right arrow

Chapter 5: Time Series Models

There are many datasets that are generated naturally in a sequence that is separated by a quantum of time, such as ocean waves that come to the shore every few minutes or transactions in the stock market that happen every few microseconds. Models that forecast when the next wave will hit the shore or what the price of the next stock transactions could be, by analyzing the history of previous occurrences, are a type of data science algorithm known as Time Series models. While traditional time series methods have long been used for forecasting, using Deep Learning, we can use advanced approaches for better results. In this chapter, we will focus on how to build commonly used Deep Learning-based time series models such as Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM), using PyTorch Lightning to perform time series forecasting.

In this chapter, we will start with a brief introduction to time series problems and then see a use case...

Technical requirements

The code for this chapter has been developed and tested on a macOS with Anaconda or in Google Colab with Python 3.6. If you are using another environment, please make the appropriate changes to your environment variables.

In this chapter, we will primarily be using the following Python modules, mentioned with their versions:

  • PyTorch Lightning (version 1.5.2)
  • Seaborn (version 0.11.2)
  • NumPy (version 1.21.5)
  • Torch (version 1.10.0)
  • pandas (version 1.3.5)

Working examples for this chapter can be found at this GitHub link: https://github.com/PacktPublishing/Deep-Learning-with-PyTorch-Lightning/tree/main/Chapter05.

n order to make sure that these modules work together and not go out of sync, we have used the specific version of torch, torchvision, torchtext, torchaudio with PyTorch Lightning 1.5.2. You can also use the latest version of PyTorch Lightning and torch compatible with each other. More details can be found on the GitHub...

Introduction to time series

In a typical machine learning use case, a dataset is a collection of features (x) and target variables (y). The model uses features to learn and predict the target variable.

Take the following example. To predict house prices, the features could be the number of bedrooms, the number of baths, and square footage, and the target variable is the price of the house. Here, the goal can be to use all the features (x) to train the model and predict the price (y) of the house. One thing we observe in such a use case is that all the records in the dataset are treated equally when predicting target variables, which is the price of the house in our example, and the order of the data doesn't matter much. The outcome (y) depends only on the values of x.

On the other hand, in time series prediction, the order of the data plays an important role in capturing some of the features, such as trends and seasons. Time series datasets are typically datasets where...

Getting started with time series models

In the next sections of this chapter, we will go through real-world examples of time series forecasting, explained in detail. Every time series model typically follows the following structure:

  1. Load and apply feature engineering on the dataset.
  2. Build the model.
  3. Train the model.
  4. Perform time series forecasting.

Let's get started in the following section.

Traffic volume forecasting using the LSTM time series model

Time series models also have various other business applications, such as predicting stock prices, forecasting the demand for products, or predicting the number of passengers per hour at an airport.

One of the most commonly used applications of such a model (which you must have used unknowingly while driving) is traffic predictions. In this section, we will try to predict the traffic volume for Interstate 94, which can be used by ride share companies such as Uber, Lyft, and/or Google Maps for predicting traffic and the time required to reach a destination for both the drivers and ride share customers. The traffic volume varies notoriously by hour (depending on the time of day, office hours, commute hours, and so on), and a time series model is helpful in making such predictions.

In this use case, we will use the Metro Interstate Traffic Volume dataset to build a multi-layer stacked LSTM model and forecast the traffic...

Summary

Time series modeling is one of the oldest and most ubiquitous applications of machine learning in industry and academia alike. In this chapter, we saw how PyTorch Lightning facilitates time series modeling using Deep Learning algorithms such as RNN and LSTM. This also provides data scientists with various tools to define RNN and LSTM configurations, with capabilities for easily monitoring learning and loss rates. We learned how we can quickly build complex Deep Learning models for time series forecasting using PyTorch Lightning.

We will continue our journey with PyTorch Lightning in the next chapter with one of the most fascinating and newest algorithms in machine learning – Generative Adversarial Networks (GANs). It can generate realistic faces of humans and objects that do not exist, and they look so real that no one can say for certain that they are artificial entities. Using PyTorch Lightning, you will see that it is much easier to carry out Generative Modeling...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Deep Learning with PyTorch Lightning
Published in: Apr 2022Publisher: PacktISBN-13: 9781800561618
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Kunal Sawarkar

Kunal Sawarkar is a chief data scientist and AI thought leader. He leads the worldwide partner ecosystem in building innovative AI products. He also serves as an advisory board member and an angel investor. He holds a master's degree from Harvard University with major coursework in applied statistics. He has been applying machine learning to solve previously unsolved problems in industry and society, with a special focus on deep learning and self-supervised learning. Kunal has led various AI product R&D labs and has 20+ patents and papers published in this field. When not diving into data, he loves doing rock climbing and learning to fly aircraft, in addition to an insatiable curiosity for astronomy and wildlife.
Read more about Kunal Sawarkar