Book Image

The Deep Learning with PyTorch Workshop

By : Hyatt Saleh
Book Image

The Deep Learning with PyTorch Workshop

By: Hyatt Saleh

Overview of this book

Want to get to grips with one of the most popular machine learning libraries for deep learning? The Deep Learning with PyTorch Workshop will help you do just that, jumpstarting your knowledge of using PyTorch for deep learning even if you’re starting from scratch. It’s no surprise that deep learning’s popularity has risen steeply in the past few years, thanks to intelligent applications such as self-driving vehicles, chatbots, and voice-activated assistants that are making our lives easier. This book will take you inside the world of deep learning, where you’ll use PyTorch to understand the complexity of neural network architectures. The Deep Learning with PyTorch Workshop starts with an introduction to deep learning and its applications. You’ll explore the syntax of PyTorch and learn how to define a network architecture and train a model. Next, you’ll learn about three main neural network architectures - convolutional, artificial, and recurrent - and even solve real-world data problems using these networks. Later chapters will show you how to create a style transfer model to develop a new image from two images, before finally taking you through how RNNs store memory to solve key data issues. By the end of this book, you’ll have mastered the essential concepts, tools, and libraries of PyTorch to develop your own deep neural networks and intelligent apps.
Table of Contents (8 chapters)

6. Analyzing the Sequence of Data with RNNs

Activity 6.01: Using a Simple RNN for a Time Series Prediction

Solution

  1. Import the required libraries, as follows:
    import pandas as pd
    import matplotlib.pyplot as plt
    import torch
    from torch import nn, optim
  2. Load the dataset and then slice it so that it contains all the rows but only the columns from index 1 to 52:
    data = pd.read_csv("Sales_Transactions_Dataset_Weekly.csv")
    data = data.iloc[:,1:53]
    data.head()

    The output is as follows:

    Figure 6.26: Displaying dataset for columns from index 1 to 52

  3. Plot the weekly sales transactions of five randomly chosen products from the entire dataset. Use a random seed of 0 when performing random sampling in order to achieve the same results as in the current activity:
    plot_data = data.sample(5, random_state=0)
    x = range(1,53)
    plt.figure(figsize=(10,5))
    for i,row in plot_data.iterrows():
        plt.plot(x,row)
    plt.legend(plot_data.index)
    plt.xlabel("Weeks...