[Python] Regression Neural Network - Unable to minimize the MSE

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am struggling to minimize the MSE for a regression based NN. I have three inputs and two outputs. I am playing around with epochs, batch size, and also with number of layers. But I am coming empty. The MSE is no where to close to zero, or even less than 10. Could use some guidance on what I could change in my code, to better fit a neural network to my data. Should I add more layers, or change the activation function etc.

    Also not clear on how to display MSE for each output separately. So at least one output is doing well. Any suggestions?

    import pandas as pd
    import numpy as np
    import os
    os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
    import tensorflow as tf
    import keras
    from keras import layers

    import matplotlib.pyplot as plt
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.model_selection import train_test_split
    # from sklearn.metrics import accuracy_score

    # Random seed
    seed = 42
    tf.config.experimental.enable_op_determinism()
    tf.random.set_seed(seed)
    keras.utils.set_random_seed(812)

    # get the generated data into train & test folder
    df = pd.read_csv('./nominal_data/train.csv')
    df_train, df_test = train_test_split(df, test_size=0.2)

    # Constant Flow mode
    # input will be rpm & flow & dp_sidecar
    # output will be supply_temp & dp_rpm_available

    #Train Data
    x_train = df_train[['pump1_rpm', 'liquid_flow', 'dp_sidecar']]
    y_train = df_train[['supply_temp', 'dp_rpm_available']]
    # Test Data
    x_test = df_test[['pump1_rpm', 'liquid_flow', 'dp_sidecar']]
    y_test = df_test[['supply_temp','dp_rpm_available']]

    #print (x_train)
    # scaler = MinMaxScaler(feature_range=(0,1))
    # scaled_x_train = scaler.fit_transform(x_train)
    # scaled_y_train = scaler.fit_transform(y_train)

    model = keras.Sequential(name="my_sequential")
    model.add(layers.Dense(32, activation="relu", name="layer1", input_shape = (3,)))
    model.add(layers.Dense(64, activation="relu", name="layer2"))
    model.add(layers.Dense(64, activation="relu", name="layer3"))
    model.add(layers.Dense(32, activation="relu", name="layer4"))
    model.add(layers.Dense(2, activation="linear", name="layer5"))
    #
    model.compile(loss='mse', optimizer= 'adam', metrics = ['mse','mae'])
    model.fit(x_train, y_train, epochs = 1000, batch_size = 1200, verbose = 0, validation_data = (x_test, y_test))
    #
    results = model.evaluate(x_test, y_test)
    print("test loss, test acc:", results)


    The file is here:

    https://limewire.com/d/dd6c5b89-560...9#nJDcHuucDsEAKwHEMaW-JJ-x8zd5iYKuRIjMiHhQSXU

    Continue reading...

Compartilhe esta Página