Neural network always predicts the same value

  Kiến thức lập trình

My neural network always predicts the same value, the problem arises when I try to normalize the target value, if I don’t it works fine. Does anyone have an idea of why?

def create_and_train_model_opti2(dff, target_column):
    df = dff.copy()
    
    
    y = df[target_column].values.reshape(-1, 1)
    X = df.drop([target_column], axis=1)
    #X = X.drop(['cmf_pos'], axis=1).values  
    
    # Normalize features
    scaler_X = MinMaxScaler(feature_range=(0, 1))
    X_scaled = scaler_X.fit_transform(X)
    
    # Normalize the target
    scaler_y = MinMaxScaler(feature_range=(0, 1))
    y_scaled = scaler_y.fit_transform(y)
    
    
    X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.2, random_state=42)
    
    
    model = Sequential([
        Dense(128, activation='relu', input_shape=(X_train.shape[1],), kernel_regularizer=l2(0.001)),
        Dropout(0.2),
        Dense(128, activation='relu', kernel_regularizer=l2(0.001)),
        Dropout(0.2),
        Dense(1)
    ])
    model.compile(optimizer=Adam(learning_rate=0.001), loss='mean_squared_error')
    
    
    history = model.fit(X_train, y_train, epochs=60, batch_size=64, validation_split=0.2)
    
    return model, history, y_train, y_test, X_train, X_test, scaler_y

def make_and_view_predictions(model, X_test, y_test, scaler_y):
    
    y_pred_scaled = model.predict(X_test)
    
    # Inverse  predictions
    y_pred = scaler_y.inverse_transform(y_pred_scaled)
    
    # Inverse transform the actual test 
    y_test_original = scaler_y.inverse_transform(y_test)
    
    
    for i in range(len(y_test)):
        print(f"Predicted: {y_pred[i][0]:.2f}, Actual: {y_test_original[i][0]:.2f}")
    
    return y_pred, y_test_original

This is the output that I have:

Predicted: 353.50, Actual: 156.80
Predicted: 353.50, Actual: 193.57
Predicted: 353.50, Actual: 34.67
Predicted: 353.50, Actual: 281.60
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 558.53
Predicted: 353.50, Actual: 21.41
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 560.24
Predicted: 353.50, Actual: 311.48
Predicted: 353.50, Actual: 135.14
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 403.05
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 46.62
Predicted: 353.50, Actual: 2579.44

I tried to search the libraries but nothing works

New contributor

William Dekeyser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT