How to do it...

Execute the following steps to train an RNN for a time series prediction problem.

  1. Import the libraries:
import yfinance as yfimport numpy as npimport torchimport torch.optim as optimimport torch.nn as nnfrom torch.utils.data import (Dataset, TensorDataset,                              DataLoader, Subset)from chapter_10_utils import create_input_data, custom_set_seedfrom sklearn.metrics import mean_squared_errorfrom sklearn.preprocessing import MinMaxScalerdevice = 'cuda' if torch.cuda.is_available() else 'cpu'
  1. Define the parameters:
# dataTICKER = 'INTL'START_DATE = '2010-01-02'END_DATE = '2019-12-31'VALID_START = '2019-07-01'N_LAGS = 12# neural network BATCH_SIZE = 16N_EPOCHS = 100
  1. Download and prepare the data:
df = yf.download(TICKER,  start=START_DATE, ...

Get Python for Finance Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.