optimizer = tf.keras.optimizers.SGD(lr=5e-5, momentum=0.9) model.compile(loss=tf.keras.losses.Huber(), optimizer=optimizer, metrics=["mae"]) history = model.fit(dataset, epochs=400) forecast = [] for time in range(len(series) - window_size): forecast.append(model.predict(series[time:time + window_size][np.newaxis])) forecast = forecast[split_time - window_size:] results = np.array(forecast)[:, 0, 0] plt.figure(figsize=(10, 6)) plot_series(time_valid, x_valid) plot_series(time_valid, results) plt.show() print(tf.keras.metrics.mean_absolute_error(x_valid, results).numpy()) import matplotlib.image as mpimg import matplotlib.pyplot as plt #----------------------------------------------------------- # Retrieve a list of list results on training and test data # sets for each training epoch #----------------------------------------------------------- mae = history.history['mae'] loss = history.history['loss']
time_valid = time[split_time:] x_valid = series[split_time:] from sklearn.preprocessing import StandardScaler, RobustScaler, MinMaxScaler, Normalizer scaler = StandardScaler() scaler = RobustScaler() scaler = MinMaxScaler() scaler = Normalizer() print(x_train) print(x_valid) x_train = scaler.fit_transform([x_train])[0] x_valid = scaler.fit_transform([x_valid])[0] plot_series(time_train, x_train) plot_series(time_valid, x_valid) print(x_train) print(x_valid) # Hyper parameters window_size = 24 batch_size = 32 shuffle_buffer_size = 500 def windowed_dataset(series, window_size, batch_size, shuffle_buffer): dataset = tf.data.Dataset.from_tensor_slices(series) dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True) dataset = dataset.flat_map(lambda window: window.batch(window_size + 1)) dataset = dataset.shuffle(shuffle_buffer).map(lambda window: (window[:-1], window[-1:]))
amplitude = 40 slope = 0.05 noise_level = 5 series = baseline + trend(time, slope) + seasonality( time, period=365, amplitude=amplitude) series += noise(time, noise_level, seed=42) split_time = 1000 time_train = time[:split_time] x_train = series[:split_time] time_valid = time[split_time:] x_valid = series[split_time:] plt.figure(figsize=(10, 6)) plot_series(time_train, x_train) #plt.show() plt.figure(figsize=(10, 6)) plot_series(time_valid, x_valid) # Naive Forecast: naive_forecast = series[split_time - 1:-1] plt.figure(figsize=(10, 6)) plot_series(time_valid, x_valid) plot_series(time_valid, naive_forecast) plt.figure(figsize=(10, 6)) plot_series(time_valid, x_valid, start=0, end=150) plot_series(time_valid, naive_forecast, start=1, end=151)
import os os.environ['CUDA_VISIBLE_DEVICES'] = '-1' from lib.utils import plot_series, trend, seasonality, noise time = np.arange(4 * 365 + 1, dtype="float32") baseline = 10 amplitude = 40 slope = 0.05 noise_level = 5 series = baseline + trend(time, slope) + seasonality(time, period=365, amplitude=amplitude) series += noise(time, noise_level, seed=42) plot_series(time, series) split_time = 1000 x_train = series[:split_time] x_valid = series[split_time:] time_train = time[:split_time] time_valid = time[split_time:] ws = 20 bs = 20 sb = 1000 def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
import pandas as pd import tensorflow as tf from lib.utils import plot_series import numpy as np import matplotlib.pyplot as plt path = "https://raw.githubusercontent.com/shravanc/datasets/master/nov_time_series.csv" df = pd.read_csv(path) #, names=["series"]) series = df['series'].values time = list(range(0, len(series))) values = df['series'].values dataset = tf.data.Dataset.from_tensor_slices(values) dataset = dataset.window(25, shift=1, drop_remainder=True) dataset = dataset.flat_map(lambda window: window.batch(25)) dataset = dataset.map(lambda window: (window[:-1], window[-1: ])) for x, y in dataset: print(x.numpy(), y.numpy()) break plt.figure(figsize=(15, 10)) plot_series(time, series) plt.show()