# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] pycharm={"name": "#%% md\n"}
# # Testing ML architectures implemented on the MLTSA package
#
# In this packaged there are multiple architectures built in for testing on the different data available

# + pycharm={"name": "#%%\n"}
"""First we import our dataset examples, and as usual generate data to work with"""
from OneD_pot_data import potentials
from OneD_pot_data import dataset

#This sets the potentials, don't re-run
total_n_pots = 25
n_DW = 5
relevant_DW_n = 2
#After defining the desired parameters we define the potentials accordingly
pots = potentials(total_n_pots, n_DW, relevant_DW_n)
# This creates the first dataset of data.
# It creates the mixing coefficients don't re-run
n_features = 180
degree_of_mixing = 2
#We specified the number of features wanted and how much they will mix
oneD_dataset = dataset(pots, n_features, degree_of_mixing)

# + pycharm={"name": "#%%\n"}
"""Now we generate the trajectories we will use for the whole experiment"""
#Generate the trajectories
n_simulations = 100
n_steps = 250
data, ans = oneD_dataset.generate_linear(n_simulations, n_steps)
data_val, ans_val = oneD_dataset.generate_linear(int(n_simulations/2), n_steps)

#Prepare it for training
from sklearn.preprocessing import OneHotEncoder
time_frame = [30, 60] #Same time frame as the sklearn one
X, Y = oneD_dataset.PrepareData(data, ans, time_frame, mode="Normal")
X_val, Y_val = oneD_dataset.PrepareData(data_val, ans_val, time_frame, mode="Normal")



# + [markdown] pycharm={"name": "#%% md\n"}
# Note that we will start by using the simple MLP, and we will move on to more advanced models.
#
# First we will import the model builder, and we will try both the simple MLP and the more "deep" version with 5 layers.

# + pycharm={"name": "#%%\n"}
#We will start with the basic Multi-Layer Perceptron
from MLTSA_tensorflow import TF_2_MLP

MLP = TF_2_MLP.build_MLP(n_steps, n_features, n_labels=2).model
MLP_deep = TF_2_MLP.build_MLP(n_steps, n_features, n_labels=2, type="deep").model

# + pycharm={"name": "#%%\n"}
from tensorflow.keras.callbacks import EarlyStopping

MLP.fit(X, Y, epochs=500, batch_size=n_steps, verbose=1, validation_split=0.2,
        callbacks=[EarlyStopping(monitor='loss', min_delta=1e-8, restore_best_weights=True, patience=50)])
val_acc = MLP.evaluate(X_val, Y_val, verbose=1)
print("We achieved", val_acc[1]*100, "% accuracy on Validation")

# + pycharm={"name": "#%%\n"}
#A deep learning model needs more epochs to train to get to convergence. We will also allow more patience
MLP_deep.fit(X, Y, epochs=1000, batch_size=n_steps, verbose=1, validation_split=0.2,
        callbacks=[EarlyStopping(monitor='loss', min_delta=1e-8, restore_best_weights=True, patience=100)])
val_acc = MLP_deep.evaluate(X_val, Y_val, verbose=1)
print("We achieved", val_acc[1]*100, "% accuracy on Validation")

# + pycharm={"name": "#%%\n"}
#We will start with the basic LSTM ("Vanilla")
from MLTSA_tensorflow import TF_2_LSTM

LSTM = TF_2_LSTM.build_LSTM(n_steps, n_features, n_labels=2).model


# + pycharm={"name": "#%%\n"}
LSTM.fit(X, Y, epochs=1000, batch_size=n_steps, verbose=1, validation_split=0.2,
        callbacks=[EarlyStopping(monitor='loss', min_delta=1e-8, restore_best_weights=True, patience=100)])
val_acc = MLP_deep.evaluate(X_val, Y_val, verbose=1)
print("We achieved", val_acc[1]*100, "% accuracy on Validation")

# + pycharm={"name": "#%%\n"}


# + pycharm={"name": "#%%\n"}


# + pycharm={"name": "#%%"}


StarcoderdataJupyterScriptsDedupFiltered