<jupyter_start><jupyter_text>* Use TensorFlow graph models, which depend on non-eager execution.* Set up output of intermediates to avoid a TensorFlow problem: Connecting to invalid output X of source node Y which has Z outputs.<jupyter_code>from tensorflow.python.framework.ops import disable_eager_execution
import tensorflow as tf

disable_eager_execution()  # for graph mode
tf.compat.v1.experimental.output_all_intermediates(True)<jupyter_output><empty_output><jupyter_text>* Load the dataset using a utility method in the library and wrap it in aTrainingDataSet class.<jupyter_code>from time_series.dataset.utils import get_energy_demand
from time_series.dataset.time_series import TrainingDataSet

train_df = get_energy_demand()
tds = TrainingDataSet(train_df)<jupyter_output><empty_output><jupyter_text>* Set up metrics and plotting methods that use for all the produced forecasts.<jupyter_code>from time_series.utils import evaluate_model<jupyter_output><empty_output><jupyter_text>* Go through the different forecasting methods in turn, DeepAR first.* See the summary of the model and then the training error over time.<jupyter_code>from time_series.models.deepar import DeepAR

ar_model = DeepAR(tds)
ar_model.instantiate_and_fit(verbose=1, epochs=N_EPOCHS)<jupyter_output>Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 10, 10)]          0         
                                                                 
 lstm (LSTM)                 (None, 10, 4)             240       
                                                                 
 dense (Dense)               (None, 10, 4)             20        
                                                                 
 main_output (GaussianLayer)  [(None, 10, 10),         100       
                              (None, 10, 10)]                    
                                                                 
=================================================================
Total params: 360
Trainable params: 360
Non-trainable params: 0
_______________________________________________________________[...]<jupyter_text>* This model is relatively simple, as we can see: only 360 parameters. * Obviously, we could tweak these parameters and add more. * Produce predictions on the test dataset.* See the errors – first the overall error and then for each of the 10 dimensions.* See the plot over the first 10 time-steps.<jupyter_code>y_predicted = ar_model.model.predict(tds.X_test)
evaluate_model(tds=tds, y_predicted=y_predicted, columns=train_df.columns, first_n=10)<jupyter_output>/usr/local/lib/python3.7/dist-packages/keras/engine/training_v1.py:2079: UserWarning: `Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
  updates=self.state_updates,<jupyter_text>* Move on to the next method: N-BEATS.* N-BEATS trains two networks. The forward network has 1,217,024 parameters.<jupyter_code>from time_series.models.nbeats import NBeatsNet

nb = NBeatsNet(tds)
nb.instantiate_and_fit(verbose=1, epochs=N_EPOCHS)
#y_predicted = nb.model.predict(tds.X_test, steps=10)
#evaluate_model(first_n=10)
print(nb.model.evaluate(tds.X_test, tds.y_test))
y_predicted = nb.model.predict(tds.X_test)
evaluate_model(tds=tds, y_predicted=y_predicted, columns=train_df.columns, first_n=10)<jupyter_output>/usr/local/lib/python3.7/dist-packages/keras/engine/training_v1.py:2079: UserWarning: `Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
  updates=self.state_updates,<jupyter_text>* LSTM is next.* This model takes a lot more parameters than DeepAR.* 45,000 parameters – this means this takes much longer to train than DeepAR.<jupyter_code>from time_series.models.LSTM import LSTM
lstm = LSTM(tds)
lstm.instantiate_and_fit(verbose=1, epochs=1000)
#y_predicted = lstm.model.predict(tds.X_test, steps=10)
#evaluate_model(first_n=10)
print(lstm.model.evaluate(tds.X_test, tds.y_test))
y_predicted = lstm.model.predict(tds.X_test)
evaluate_model(tds=tds, y_predicted=y_predicted, columns=train_df.columns, first_n=10)
#tds_1step = TrainingDataSet(train_df, lag=tds.lag, two_dim=False)
#tds_1step.y_test = tds_1step.y_test[:, :, 0]
#tds_1step.y_train = tds_1step.y_train[:, :, 0]<jupyter_output><empty_output><jupyter_text>* Let's do the transformer.<jupyter_code># only training a 1-step prediction here:
from time_series.models.transformer import Transformer

trans = Transformer(tds)
trans.instantiate_and_fit(verbose=1, epochs=N_EPOCHS)
print(trans.model.evaluate(tds.X_test, tds.y_test))<jupyter_output><empty_output><jupyter_text>* This model takes very long to train and the performance was the worst of the bunch.<jupyter_code>y_predicted = trans.model.predict(tds.X_test)  # .reshape(-1, 10)
evaluate_model(tds=tds, y_predicted=y_predicted, columns=train_df.columns, first_n=10)<jupyter_output><empty_output><jupyter_text>* Last deep learning model is the TCN.<jupyter_code>from time_series.models.TCN import TCNModel

tcn_model = TCNModel(tds)
tcn_model.instantiate_and_fit(verbose=1, epochs=N_EPOCHS)
print(tcn_model.model.evaluate(tds.X_test, tds.y_test))
y_predicted = tcn_model.model.predict(tds.X_test)
evaluate_model(tds=tds, y_predicted=y_predicted, columns=train_df.columns, first_n=10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11075384065737763, 'fraction_numerical': 0.01982851018220793, 'mean_word_length': 4.302083333333333, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '485565', 'license': 'MIT', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1732, 'n_words': 420, 'path': 'Multivariate Forecasting/Energy demand forecasting test models.ipynb', 'repo_name': 'thirasit/Time-series-analysis-with-ML', 'size': '5598'}