Example of an LSTM network with Keras

In this example, we want to test the ability of an LSTM network to learn long-term dependencies. For this reason, we employ a dataset called Zuerich Monthly Sunspots (freely provided by Andrews and Herzberg in 1985) containing the numbers observed in all the months starting from 1749 to 1983 (please read the information box for how to download the dataset). As we are not interested in the dates, we need to parse the file in order to extract only the values needed for the time series (which contains 2,820 steps):

import numpy as npdataset_filename = '<YOUR_PATH>\dataset.csv'n_samples = 2820data = np.zeros(shape=(n_samples, ), dtype=np.float32)with open(dataset_filename, 'r') as f: lines = f.readlines() ...

Get Mastering Machine Learning Algorithms 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.