C4W2: Predicting time series
C4W2: Predicting time series#
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from dataclasses import dataclass
from tensorflow.keras import layers
def plot_series(time, series, format="-", start=0, end=None):
plt.plot(time[start:end], series[start:end], format)
plt.xlabel("Time")
plt.ylabel("Value")
plt.grid(False)
def trend(time, slope=0):
return slope * time
def seasonal_pattern(season_time):
"""Just an arbitrary pattern, you can change it if you wish"""
return np.where(season_time < 0.1,
np.cos(season_time * 6 * np.pi),
2 / np.exp(9 * season_time))
def seasonality(time, period, amplitude=1, phase=0):
"""Repeats the same pattern at each period"""
season_time = ((time + phase) % period) / period
return amplitude * seasonal_pattern(season_time)
def noise(time, noise_level=1, seed=None):
rnd = np.random.RandomState(seed)
return rnd.randn(len(time)) * noise_level
def generate_time_series():
# The time dimension or the x-coordinate of the time series
time = np.arange(4 * 365 + 1, dtype="float32")
# Initial series is just a straight line with a y-intercept
y_intercept = 10
slope = 0.005
series = trend(time, slope) + y_intercept
# Adding seasonality
amplitude = 50
series += seasonality(time, period=365, amplitude=amplitude)
# Adding some noise
noise_level = 3
series += noise(time, noise_level, seed=51)
return time, series
# Save all "global" variables within the G class (G stands for global)
@dataclass
class G:
TIME, SERIES = generate_time_series()
SPLIT_TIME = 1100
WINDOW_SIZE = 20
BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000
# Plot the generated series
plt.figure(figsize=(10, 6))
plot_series(G.TIME, G.SERIES)
plt.show()
def train_val_split(time, series, time_step=G.SPLIT_TIME):
time_train = time[:time_step]
series_train = series[:time_step]
time_valid = time[time_step:]
series_valid = series[time_step:]
return time_train, series_train, time_valid, series_valid
# Split the dataset
time_train, series_train, time_valid, series_valid = train_val_split(G.TIME, G.SERIES)
def windowed_dataset(series, window_size=G.WINDOW_SIZE, batch_size=G.BATCH_SIZE, shuffle_buffer=G.SHUFFLE_BUFFER_SIZE):
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)
dataset = dataset.map(lambda window: (window[:-1], window[-1]))
dataset = dataset.batch(batch_size).prefetch(tf.data.AUTOTUNE)
return dataset
def create_model(window_size=G.WINDOW_SIZE):
model = tf.keras.Sequential([
layers.Dense(10, activation='relu', input_shape=[window_size]),
layers.Dense(10, activation='relu'),
layers.Dense(1)
])
model.compile(optimizer='adam',
loss='mse')
return model
dataset = windowed_dataset(series_train)
model = create_model()
model.fit(dataset, epochs=100)
Epoch 1/100
34/34 [==============================] - 2s 40ms/step - loss: 1340.9701
Epoch 2/100
34/34 [==============================] - 0s 6ms/step - loss: 172.2769
Epoch 3/100
34/34 [==============================] - 0s 5ms/step - loss: 83.9721
Epoch 4/100
34/34 [==============================] - 0s 5ms/step - loss: 78.8805
Epoch 5/100
34/34 [==============================] - 0s 5ms/step - loss: 76.0876
Epoch 6/100
34/34 [==============================] - 0s 5ms/step - loss: 73.4062
Epoch 7/100
34/34 [==============================] - 0s 5ms/step - loss: 70.7829
Epoch 8/100
34/34 [==============================] - 0s 5ms/step - loss: 68.2268
Epoch 9/100
34/34 [==============================] - 0s 5ms/step - loss: 65.7392
Epoch 10/100
34/34 [==============================] - 0s 5ms/step - loss: 63.3203
Epoch 11/100
34/34 [==============================] - 0s 5ms/step - loss: 61.0110
Epoch 12/100
34/34 [==============================] - 0s 6ms/step - loss: 58.8323
Epoch 13/100
34/34 [==============================] - 0s 5ms/step - loss: 56.7796
Epoch 14/100
34/34 [==============================] - 0s 5ms/step - loss: 54.8793
Epoch 15/100
34/34 [==============================] - 0s 5ms/step - loss: 53.1298
Epoch 16/100
34/34 [==============================] - 0s 5ms/step - loss: 51.5033
Epoch 17/100
34/34 [==============================] - 0s 5ms/step - loss: 50.0040
Epoch 18/100
34/34 [==============================] - 0s 5ms/step - loss: 48.6180
Epoch 19/100
34/34 [==============================] - 0s 5ms/step - loss: 47.3641
Epoch 20/100
34/34 [==============================] - 0s 5ms/step - loss: 46.1812
Epoch 21/100
34/34 [==============================] - 0s 5ms/step - loss: 45.0712
Epoch 22/100
34/34 [==============================] - 0s 5ms/step - loss: 44.0572
Epoch 23/100
34/34 [==============================] - 0s 5ms/step - loss: 43.1306
Epoch 24/100
34/34 [==============================] - 0s 5ms/step - loss: 42.2878
Epoch 25/100
34/34 [==============================] - 0s 5ms/step - loss: 41.5175
Epoch 26/100
34/34 [==============================] - 0s 5ms/step - loss: 40.8108
Epoch 27/100
34/34 [==============================] - 0s 5ms/step - loss: 40.1514
Epoch 28/100
34/34 [==============================] - 0s 5ms/step - loss: 39.5212
Epoch 29/100
34/34 [==============================] - 0s 5ms/step - loss: 38.9626
Epoch 30/100
34/34 [==============================] - 0s 5ms/step - loss: 38.4412
Epoch 31/100
34/34 [==============================] - 0s 5ms/step - loss: 37.9826
Epoch 32/100
34/34 [==============================] - 0s 5ms/step - loss: 37.5472
Epoch 33/100
34/34 [==============================] - 0s 5ms/step - loss: 37.1381
Epoch 34/100
34/34 [==============================] - 0s 5ms/step - loss: 36.7650
Epoch 35/100
34/34 [==============================] - 0s 5ms/step - loss: 36.3947
Epoch 36/100
34/34 [==============================] - 0s 5ms/step - loss: 36.0554
Epoch 37/100
34/34 [==============================] - 0s 5ms/step - loss: 35.7352
Epoch 38/100
34/34 [==============================] - 0s 5ms/step - loss: 35.4257
Epoch 39/100
34/34 [==============================] - 0s 5ms/step - loss: 35.1450
Epoch 40/100
34/34 [==============================] - 0s 5ms/step - loss: 34.8787
Epoch 41/100
34/34 [==============================] - 0s 5ms/step - loss: 34.6231
Epoch 42/100
34/34 [==============================] - 0s 5ms/step - loss: 34.3476
Epoch 43/100
34/34 [==============================] - 0s 5ms/step - loss: 34.0856
Epoch 44/100
34/34 [==============================] - 0s 5ms/step - loss: 33.8270
Epoch 45/100
34/34 [==============================] - 0s 5ms/step - loss: 33.5679
Epoch 46/100
34/34 [==============================] - 0s 6ms/step - loss: 33.3399
Epoch 47/100
34/34 [==============================] - 0s 5ms/step - loss: 33.0958
Epoch 48/100
34/34 [==============================] - 0s 5ms/step - loss: 32.8676
Epoch 49/100
34/34 [==============================] - 0s 5ms/step - loss: 32.6548
Epoch 50/100
34/34 [==============================] - 0s 5ms/step - loss: 32.4386
Epoch 51/100
34/34 [==============================] - 0s 5ms/step - loss: 32.2392
Epoch 52/100
34/34 [==============================] - 0s 5ms/step - loss: 32.0592
Epoch 53/100
34/34 [==============================] - 0s 5ms/step - loss: 31.8723
Epoch 54/100
34/34 [==============================] - 0s 5ms/step - loss: 31.6965
Epoch 55/100
34/34 [==============================] - 0s 5ms/step - loss: 31.5315
Epoch 56/100
34/34 [==============================] - 0s 6ms/step - loss: 31.3622
Epoch 57/100
34/34 [==============================] - 0s 5ms/step - loss: 31.2047
Epoch 58/100
34/34 [==============================] - 0s 6ms/step - loss: 31.0424
Epoch 59/100
34/34 [==============================] - 0s 6ms/step - loss: 30.9039
Epoch 60/100
34/34 [==============================] - 0s 5ms/step - loss: 30.7382
Epoch 61/100
34/34 [==============================] - 0s 5ms/step - loss: 30.6125
Epoch 62/100
34/34 [==============================] - 0s 5ms/step - loss: 30.4566
Epoch 63/100
34/34 [==============================] - 0s 7ms/step - loss: 30.3344
Epoch 64/100
34/34 [==============================] - 0s 5ms/step - loss: 30.1896
Epoch 65/100
34/34 [==============================] - 0s 5ms/step - loss: 30.0766
Epoch 66/100
34/34 [==============================] - 0s 5ms/step - loss: 29.9383
Epoch 67/100
34/34 [==============================] - 0s 5ms/step - loss: 29.8252
Epoch 68/100
34/34 [==============================] - 0s 5ms/step - loss: 29.6932
Epoch 69/100
34/34 [==============================] - 0s 5ms/step - loss: 29.5865
Epoch 70/100
34/34 [==============================] - 0s 5ms/step - loss: 29.4582
Epoch 71/100
34/34 [==============================] - 0s 5ms/step - loss: 29.3593
Epoch 72/100
34/34 [==============================] - 0s 5ms/step - loss: 29.2246
Epoch 73/100
34/34 [==============================] - 0s 5ms/step - loss: 29.1516
Epoch 74/100
34/34 [==============================] - 0s 5ms/step - loss: 28.9600
Epoch 75/100
34/34 [==============================] - 0s 5ms/step - loss: 28.9945
Epoch 76/100
34/34 [==============================] - 0s 5ms/step - loss: 28.7264
Epoch 77/100
34/34 [==============================] - 0s 5ms/step - loss: 29.3997
Epoch 78/100
34/34 [==============================] - 0s 5ms/step - loss: 29.6907
Epoch 79/100
34/34 [==============================] - 0s 5ms/step - loss: 35.6477
Epoch 80/100
34/34 [==============================] - 0s 5ms/step - loss: 39.5197
Epoch 81/100
34/34 [==============================] - 0s 5ms/step - loss: 39.4927
Epoch 82/100
34/34 [==============================] - 0s 5ms/step - loss: 29.4130
Epoch 83/100
34/34 [==============================] - 0s 5ms/step - loss: 28.5821
Epoch 84/100
34/34 [==============================] - 0s 5ms/step - loss: 28.0186
Epoch 85/100
34/34 [==============================] - 0s 6ms/step - loss: 27.9871
Epoch 86/100
34/34 [==============================] - 0s 6ms/step - loss: 27.8451
Epoch 87/100
34/34 [==============================] - 0s 5ms/step - loss: 27.7732
Epoch 88/100
34/34 [==============================] - 0s 5ms/step - loss: 27.6577
Epoch 89/100
34/34 [==============================] - 0s 5ms/step - loss: 27.5650
Epoch 90/100
34/34 [==============================] - 0s 5ms/step - loss: 27.4675
Epoch 91/100
34/34 [==============================] - 0s 5ms/step - loss: 27.3737
Epoch 92/100
34/34 [==============================] - 0s 5ms/step - loss: 27.2743
Epoch 93/100
34/34 [==============================] - 0s 5ms/step - loss: 27.2039
Epoch 94/100
34/34 [==============================] - 0s 5ms/step - loss: 27.0864
Epoch 95/100
34/34 [==============================] - 0s 6ms/step - loss: 27.0292
Epoch 96/100
34/34 [==============================] - 0s 5ms/step - loss: 26.8743
Epoch 97/100
34/34 [==============================] - 0s 7ms/step - loss: 26.8698
Epoch 98/100
34/34 [==============================] - 0s 7ms/step - loss: 26.6904
Epoch 99/100
34/34 [==============================] - 0s 6ms/step - loss: 26.8957
Epoch 100/100
34/34 [==============================] - 0s 6ms/step - loss: 26.7227
<keras.callbacks.History at 0x1ca26f07310>
def compute_metrics(true_series, forecast):
mse = tf.keras.metrics.mean_squared_error(true_series, forecast).numpy()
mae = tf.keras.metrics.mean_absolute_error(true_series, forecast).numpy()
return mse, mae
def generate_forecast(series=G.SERIES, split_time=G.SPLIT_TIME, window_size=G.WINDOW_SIZE):
forecast = []
forecast_series = series[split_time - window_size:]
for time in range(len(forecast_series) - window_size):
forecast.append(model.predict(forecast_series[time:time + window_size][np.newaxis]))
results = np.array(forecast).squeeze()
return results
# Save the forecast
dnn_forecast = generate_forecast()
# Plot it
plt.figure(figsize=(10, 6))
plot_series(time_valid, series_valid)
plot_series(time_valid, dnn_forecast)
1/1 [==============================] - 0s 134ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 53ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 19ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 36ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 36ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 38ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 19ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 21ms/step
mse, mae = compute_metrics(series_valid, dnn_forecast)
print(f"mse: {mse:.2f}, mae: {mae:.2f} for forecast")
mse: 27.87, mae: 3.50 for forecast
# model.save('my_model.h5')