# -*- coding: utf-8 -*-
"""
Created on Fri Feb 17 16:44:36 2023

@author: adrie
"""

import matplotlib.pyplot as plt
import tensorflow as tf
import random as ra
import numpy as np
tf.config.list_physical_devices('GPU')
X_train = np.genfromtxt('X_train.txt',delimiter=';')
Y_target = np.genfromtxt('Y_target.txt',delimiter=';') 

    
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(1024, activation="relu"))
model.add(tf.keras.layers.Dense(512, activation="relu"))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.Dense(1))

model.compile(
    loss="mse",
    optimizer="adam"
)
# Definition of the callback for learning rate evolution
def scheduler(epochs):
      
    return  0.01 
    if epochs>10:#200
        return 0.0001 
    
callback_lr = tf.keras.callbacks.LearningRateScheduler(scheduler)

# Definition of the callback for stop the training at here 1e-5
class haltCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if(logs.get('loss')<=0.5 and logs.get('val_loss')<=0.5):
            print("\n\n\nStop traning\n\n\n")
            self.model.stop_training = True
callback_stop = haltCallback()  


history=model.fit(X_train, Y_target, epochs=40, validation_split=0.2,verbose=1,callbacks=[callback_lr,callback_stop])


model.save('prop_meca_verre.h5') 
