<jupyter_start><jupyter_text>Load packages and data<jupyter_code>%load_ext autoreload
%autoreload 2
# Reload all modules (except those excluded by %aimport) every time before executing the Python code typed. https://ipython.org/ipython-doc/3/config/extensions/autoreload.html
import os
import glob
import joblib
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt

from pathlib import Path
from tensorflow.keras import Sequential
from tensorflow.keras import layers

# extra functions
from utility import generate_dataset, mean_absolute_error, mean_absolute_percentage_error, plot_result, report_results, normalise_data, vec2mat

path = Path("../Data/raw/AEMO/train/")
all_files = glob.glob(os.path.join(path, "*.csv"))
df_csv = pd.concat((pd.read_csv(f) for f in all_files))<jupyter_output><empty_output><jupyter_text>A. Short Term Prediction (2 hours)<jupyter_code>validation_ratio = 0.25
n_dim_in = 12
n_dim_out = 4
X_train, y_train, X_val, y_val, scaler_demand = generate_dataset(
    df_csv, n_dim_in, n_dim_out, validation_ratio)
print(f'Training samples: input {X_train.shape}, target {y_train.shape}')
print(f'Validation samples: input {X_val.shape}, target {y_val.shape}')
model = Sequential(name = 'MLP_Demand_Prediction_Model')
model.add(layers.Dense(100, input_shape = (n_dim_in,), activation='relu', name = 'Hidden_Layer'))
model.add(layers.Dense(n_dim_out, name ='Output_Layer'))
model.summary()
# Init a model
model = Sequential(name = 'MLP_Demand_Prediction_Model_2hs')
model.add(layers.Dense(100, input_shape = (n_dim_in,), activation='relu', name = 'Hidden_Layer'))
model.add(layers.Dense(n_dim_out, name ='Output_Layer'))
model.summary()

# Stop training when a monitored metric has stopped improving.
earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)

# Callback to save the Keras model or model weights at some frequency.
checkpoint_filepath =  './models/checkpoints/weights.best.hdf5'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True, 
    monitor='val_loss',
    mode='min',
    save_best_only=True,
    verbose = 0)

model.compile(loss='mae', optimizer='Adam', metrics=['mae', 'mape', 'mse'])
 
print('# Fit model on training data with validtion on validation data')
# Model (weights) are saved at the end of every epoch, if it's the best seen so far.
history = model.fit(X_train, y_train,
                    batch_size=1024,
                    epochs = 400,
                    # We pass some validation for
                    # monitoring validation loss and metrics
                    # at the end of each epoch
                    validation_data=(X_val, y_val),
                    verbose=0,
                    callbacks=[model_checkpoint_callback, earlystop_callback])

print('The model(that are considered the best) are loaded back into the model.')
model.load_weights(checkpoint_filepath)
print('# Done')
def plot_history(history):
    # summarize history for loss
    # Loss/MAE
    plt.figure(figsize=(15,5))
    plt.subplot(1, 3, 1)
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss: mae')
    plt.ylabel('mae')
    plt.xlabel('epoch')
    plt.legend(['train', 'validation'], loc='upper right')
    plt.grid(True)

    # MAPE
    plt.subplot(1, 3, 2)
    plt.plot(history.history['mape'])
    plt.plot(history.history['val_mape'])
    plt.title('model loss:mape')
    plt.ylabel('mape')
    plt.xlabel('epoch')
    plt.legend(['train', 'validation'], loc='upper right')
    plt.grid(True)

    # MSE
    plt.subplot(1, 3, 3)
    plt.plot(history.history['mse'])
    plt.plot(history.history['val_mse'])
    plt.title('model loss: mse')
    plt.ylabel('mse')
    plt.xlabel('epoch')
    plt.legend(['train', 'validation'], loc='upper right')
    plt.grid(True)
    plt.show()
    
plot_history(history)
print("# Training results:")
y_train_pred = model.predict(X_train) 
mae_train , mape_train = report_results(y_train, y_train_pred, scaler = scaler_demand)
print("[MAE ]: %.4f"     % mae_train)
print("[MAPE]: %.4f %% " % mape_train)
print("# Validation results:")
y_val_pred = model.predict(X_val) 
mae_val, mape_val= report_results(y_val, y_val_pred, scaler = scaler_demand)
print("[MAE ]: %.4f"     % mae_val)
print("[MAPE]: %.4f %% " % mape_val)
# save our MLP model
filename = './models/MLP.demand.short.TF' + '.hdf5'
model.save(filename)
del model, history<jupyter_output><empty_output><jupyter_text>B. Long Term Prediction (6 Hours)<jupyter_code>validation_ratio = 0.25
n_dim_in = 24
n_dim_out = 12
X_train, y_train, X_val, y_val, scalar_demand = generate_dataset(
    df_csv, n_dim_in, n_dim_out, validation_ratio)

print(f'Training samples: input {X_train.shape}, target {y_train.shape}')
print(f'Validation samples: input {X_val.shape}, target {y_val.shape}')
# Init a model
model = Sequential(name = 'MLP_Demand_Prediction_Model_6hs')
model.add(layers.Dense(100, input_shape = (n_dim_in,), activation='relu', name = 'Hidden_Layer'))
model.add(layers.Dense(n_dim_out, name ='Output_Layer'))
model.summary()

# Stop training when a monitored metric has stopped improving.
earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)

# Callback to save the Keras model or model weights at some frequency.
checkpoint_filepath =  './models/checkpoints/weights.best.hdf5'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True, 
    monitor='val_loss',
    mode='min',
    save_best_only=True,
    verbose = 0)

model.compile(loss='mae', optimizer='Adam', metrics=['mae', 'mape', 'mse'])
 
print('# Fit model on training data with validtion on validation data')
# Model (weights) are saved at the end of every epoch, if it's the best seen so far.
history = model.fit(X_train, y_train,
                    batch_size=1024,
                    epochs = 400,
                    # We pass some validation for
                    # monitoring validation loss and metrics
                    # at the end of each epoch
                    validation_data=(X_val, y_val),
                    verbose=0,
                    callbacks=[model_checkpoint_callback, earlystop_callback])

print('The model(that are considered the best) are loaded back into the model.')
model.load_weights(checkpoint_filepath)
print('# Done')
plot_history(history)
print("# Training results:")
y_train_pred = model.predict(X_train) 
mae_train , mape_train = report_results(y_train, y_train_pred, scaler = scaler_demand)
print("[MAE ]: %.4f"     % mae_train)
print("[MAPE]: %.4f %% " % mape_train)

print("# Validation results:")
y_val_pred = model.predict(X_val) 
mae_val, mape_val= report_results(y_val, y_val_pred, scaler = scaler_demand)
print("[MAE ]: %.4f"     % mae_val)
print("[MAPE]: %.4f %% " % mape_val)
# save our MLP model
filename = './models/MLP.demand.long.TF' + '.hdf5'
model.save(filename)
del model, history<jupyter_output><empty_output><jupyter_text>C. Test the Model<jupyter_code>filename = './models/MLP.demand.short.TF' + '.hdf5'
model_ShortTerm = tf.keras.models.load_model(filename)
model_ShortTerm.summary() # check the information
filename = './models/MLP.demand.long.TF' + '.hdf5'
model_LongTerm = tf.keras.models.load_model(filename)
model_LongTerm.summary() # check the information<jupyter_output>Model: "MLP_Demand_Prediction_Model_6hs"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Hidden_Layer (Dense)         (None, 100)               2500      
_________________________________________________________________
Output_Layer (Dense)         (None, 12)                1212      
=================================================================
Total params: 3,712
Trainable params: 3,712
Non-trainable params: 0
_________________________________________________________________<jupyter_text>get the test data ready<jupyter_code>path = Path("../Data/raw/AEMO/test/")
all_files = glob.glob(os.path.join(path, "*.csv"))
df_csv = pd.concat((pd.read_csv(f) for f in all_files))<jupyter_output><empty_output><jupyter_text>test model<jupyter_code>def generate_test_data(df_csv, n_dim_in, n_dim_out, scaler):
    data_demand = df_csv["TOTALDEMAND"].to_numpy().reshape(-1, 1) # extract the column
    X_demand = scaler.transform(data_demand) # normalise the values with statistic we learned from training data
    M_demand = vec2mat(X_demand, n_dim_in, n_dim_out)
    n_row, n_col = M_demand.shape
    X = M_demand[:, 0:n_col-n_dim_out]
    y = M_demand[:, n_col-n_dim_out:]
    return X, y
def test_model(model, df_csv, scaler = scaler_demand):
    # get the input and ouput shape from our model
    n_dim_in = model.input_shape[1]
    n_dim_out = model.output_shape[1]
    # the salar function is from our training dataset.
    X_test, y_test = generate_test_data(df_csv, n_dim_in, n_dim_out, scaler = scaler_demand)

    print(f'Test samples: input {X_test.shape}, target {y_test.shape}')
    
    print("# Test results:")
    y_pred = model.predict(X_test)
    mae  , mape  = report_results(y_test, y_pred, scaler = scaler_demand)
    print("[MAE ]: %.4f"     % mae)
    print("[MAPE]: %.4f %% " % mape)
    
    return X_test, y_test, y_pred<jupyter_output><empty_output><jupyter_text>Test the short term prediction model<jupyter_code>X_test, y_test, y_pred = test_model(model_ShortTerm, df_csv, scaler = scaler_demand)
plot_result(y_pred[:, 0], y_test[:, 0], scaler_demand, length=100)<jupyter_output>Test samples: input (6993, 12), target (6993, 4)
# Test results:
[MAE ]: 134.2629
[MAPE]: 2.8462 %<jupyter_text>Test the long term prediction model<jupyter_code>X_test, y_test, y_pred = test_model(model_LongTerm, df_csv, scaler = scaler_demand)
plot_result(y_pred[:, 0], y_test[:, 0], scaler_demand, length=100)<jupyter_output>Test samples: input (6973, 24), target (6973, 12)
# Test results:
[MAE ]: 257.9126
[MAPE]: 5.5952 %
{'fraction_non_alphanumeric': 0.13552787663107949, 'fraction_numerical': 0.01631079478054567, 'mean_word_length': 4.4686486486486485, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '178193', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3399, 'n_words': 773, 'path': 'notebooks/Demo (MLP.TF) 03 Short and Long Term Prediction.ipynb', 'repo_name': 'cuicaihao/time-series-forecasting', 'size': '10116'}