[Python] How to load a Neural Network Model along with MinMaxScalars - Python

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple neural network model, of 4 layers, that I trained on a numerical dataset of 25K data points.

    It takes a good time to load the data, whenever I want to evaluate new features to python code. So how could I save the model in my project folder, and just load it as required?

    It is a sequential model that looks something like this:

    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(3, activation="linear", name="layer4"))
    # #
    model.compile(loss='mse',
    optimizer='adam',
    metrics=['mse', 'mae', air_vol_flow_mse, air_outlet_temp_mse, heat_load_mse])
    #
    history = model.fit(scaled_x_train, scaled_y_train, epochs = 320, batch_size = 64, verbose = 1)#, validation_data = (x_test, y_test))

    results = model.evaluate(scaled_x_test, scaled_y_test)


    The main aspect is - where I am unable to find the solution is - I scaled the data while training this model. As follows: I have to save this information when loading the model as well - so how can I achieve it.

    # Normalization
    x_scaler = MinMaxScaler(feature_range=(0,1))
    scaled_x_train = x_scaler.fit_transform(x_train)
    scaled_x_test = x_scaler.transform(x_test)
    y_scaler = MinMaxScaler(feature_range=(0,1))
    scaled_y_train = y_scaler.fit_transform(y_train)
    scaled_y_test = y_scaler.transform(y_test)

    Continue reading...

Compartilhe esta Página