%% from sklearn.modelselection import traintestsplit from sklearn.preprocessing import…
- User
%%
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 - Assistant
The error you're encountering is because your model expects the output shape to match between the predictions and the targets for each output layer, but it doesn't in your case. Your model is structured to have three different output layers, each expected to produce an output of shape (None, 1), but you've provided
y_trainwith a shape that matches all three outputs concatenated together into a single array with shape (None, 3).You need to adjust the format of your target data (
y_trainandy_dev) to match the expected structure of your model's outputs. In your model, you have specified three separate outputs: - User
error changed to: { "name": "TypeError", "message": "'NoneType' object is not callable", "stack": "--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[12], line 12 9 y_dev_priority = y_dev[:, [2]] 11 # Adjusting the fit call to provide a list (or dict) of outputs ---> 12 history = model.fit( 13 X_train, 14 [y_train_high, y_train_low, y_train_priority], # Now a list of targets 15 epochs=6000, 16 batch_size=2048, 17 validation_data=(X_dev, [y_dev_high, y_dev_low, y_dev_priority]) 18 ) 20 # history = model.fit(X_train, y_train, epochs=6000, batch_size=2048, 21 # 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..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.) 68 # To get the full stack trace, call: 69 # ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb
- Assistant