# %%
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.utils import to_categorical
import numpy as np



# %%
import pandas as pd
# Load the dataset using pandas
df = pd.read_csv(r"C:\Users\arisa\Desktop\combined_day_fl_1hot_custom_extra_and_indic_no_h.csv")

# %%
# Assuming ‘df’ is your DataFrame and ‘Label’ is the target column
X = df.drop(['y_High_1d', 'y_Low_1d', 'y_Priority_1d',
                 'y_High_2d', 'y_Low_2d', 'y_Priority_2d',
                  'y_High_3d', 'y_Low_3d', 'y_Priority_3d',
                   'y_High_5d', 'y_Low_5d', 'y_Priority_5d'], axis=1).values
Y = df[['y_High_1d', 'y_Low_1d', 'y_Priority_1d']].values
# Y = to_categorical(y)  # Convert labels to one-hot encoding


# %%
# Normalizing the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# %%
# Normalizing the features
scaler_y = StandardScaler()
Y_scaled = scaler_y.fit_transform(Y)

# %%
X_train, X_temp, y_train, y_temp = train_test_split(X_scaled, Y_scaled, train_size = 0.92, random_state = 42)

# We will keep the shuffle = True since our dataset has not any time dependency.

X_dev, X_test, y_dev, y_test = train_test_split(X_temp, y_temp, test_size = 0.5, random_state = 42)

# %%
print(f'train samples: {len(X_train)}\ndev samples: {len(X_dev)}\ntest samples: {len(X_test)}')

# %%
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout,Input
import tensorflow as tf
from tensorflow.keras.layers import BatchNormalization
import keras
from tensorflow.keras import Model

def build_model():
    # model = Sequential([
    #     Dense(X.shape[1], input_shape=(X.shape[1],), activation='relu',activity_regularizer=tf.keras.regularizers.L2(0.015)),
        
    #     # Dense(2048, activation='relu',activity_regularizer=tf.keras.regularizers.L2(0.010)),
        
    #     # Dense(1024, activation='relu',activity_regularizer=tf.keras.regularizers.L2(0.015)),
        
    #     Dense(512, activation='relu',activity_regularizer=tf.keras.regularizers.L2(0.01)),
        
    #     Dense(256, activation='relu',activity_regularizer=tf.keras.regularizers.L2(0.01)),

    #     Dense(128, activation='relu'),

    #     Dense(3)  # Output layer
    # ])

    inputs = Input(shape=(X.shape[1],))

    x = Dense(X.shape[1], activation='relu', input_shape=(X.shape[1],)) (inputs)

    x = Dense(2048, activation='relu') (x)

    x = Dense(1024, activation='relu') (x)
 
    x = Dense(512, activation='relu') (x)
  
    x = Dense(256, activation='relu') (x)
 
    x = Dense(128, activation='relu') (x)
    x = Dense(128, activation='relu') (x)


    out_high_1d = Dense(1, name='high_output_1d')(x)  # No activation, linear output
    out_low_1d = Dense(1, name='low_output_1d')(x)    # No activation, linear output
    out_priority_1d = Dense(1, activation='sigmoid', name='priority_output_1d')(x)

    model = Model(inputs=inputs, outputs=[
    out_high_1d, out_low_1d, out_priority_1d])

    optimizer = keras.optimizers.Adam(learning_rate=0.001)
    model.compile(optimizer=optimizer,
              loss={
                'high_output_1d': 'mse', 'low_output_1d': 'mse', 'priority_output_1d': 'binary_crossentropy',
                # 'high_output_2d': 'mse', 'low_output_2d': 'mse', 'priority_output_2d': 'binary_crossentropy',
                # 'high_output_3d': 'mse', 'low_output_3d': 'mse', 'priority_output_3d': 'binary_crossentropy',
                # 'high_output_5d': 'mse', 'low_output_5d': 'mse', 'priority_output_5d': 'binary_crossentropy'
              },
              metrics={
                'high_output_1d': ['mae'], 'low_output_1d': ['mae'], 'priority_output_1d': ['accuracy'],
                # 'high_output_2d': ['mae'], 'low_output_2d': ['mae'], 'priority_output_2d': ['accuracy'],
                # 'high_output_3d': ['mae'], 'low_output_3d': ['mae'], 'priority_output_3d': ['accuracy'],
                # 'high_output_5d': ['mae'], 'low_output_5d': ['mae'], 'priority_output_5d': ['accuracy']
              },
              loss_weights={
                'high_output_1d': 1.0, 'low_output_1d': 1.0, 'priority_output_1d': 1.0,
                # 'high_output_2d': 1.0, 'low_output_2d': 1.0, 'priority_output_2d': 1.0,
                # 'high_output_3d': 1.0, 'low_output_3d': 1.0, 'priority_output_3d': 1.0,
                # 'high_output_5d': 1.0, 'low_output_5d': 1.0, 'priority_output_5d': 1.0
              }
              )

    # optimizer = keras.optimizers.Adam(learning_rate=0.001)
    # model.compile(optimizer=optimizer,
    #               loss='mse',
    #               metrics=['mae'])
    return model


# %%
model = build_model()

model.summary()

# %%



history = model.fit(X_train, y_train, epochs=6000, batch_size=2048,
                    validation_data=(X_dev, y_dev))






error:
{
	"name": "ValueError",
	"message": "in user code:

    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1160, in train_function  *
        return step_function(self, iterator)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1135, in run_step  **
        outputs = model.train_step(data)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 994, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1052, in compute_loss
        return self.compiled_loss(
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\compile_utils.py\", line 265, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 152, in __call__
        losses = call_fn(y_true, y_pred)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 272, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 2162, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\backend.py\", line 5677, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(

    ValueError: `logits` and `labels` must have the same shape, received ((None, 1) vs (None, 3)).
",
	"stack": "---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 1
----> 1 history = model.fit(X_train, y_train, epochs=6000, batch_size=2048,
      2                     validation_data=(X_dev, y_dev))

File c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\utils\\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~\\AppData\\Local\\Temp\\__autograph_generated_file2cc22k5_.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

ValueError: in user code:

    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1160, in train_function  *
        return step_function(self, iterator)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1135, in run_step  **
        outputs = model.train_step(data)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 994, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\training.py\", line 1052, in compute_loss
        return self.compiled_loss(
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\engine\\compile_utils.py\", line 265, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 152, in __call__
        losses = call_fn(y_true, y_pred)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 272, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\losses.py\", line 2162, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File \"c:\\Users\\arisa\\.conda\\envs\\tf\\lib\\site-packages\\keras\\backend.py\", line 5677, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(

    ValueError: `logits` and `labels` must have the same shape, received ((None, 1) vs (None, 3)).
"
}