[Python] Problem with 80% accuracy(I need to get 90%) and I have fight it for hours [closed]

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    Good night, people . I’ve been fighting with this model for 3 hoours and can’t get validation accuracy higher than ≈ 78 – 82 %. My goal is at least 90 %.

    1 . Dataset Rows: 3 000

    Features: 25 Likert‑scale (1‑5) answers to an interest inventory for high‑school students

    Target: 7 careers (Software Engineering, AI, Cybersecurity, etc.)

    text Copy Edit Software Engineering 451 Artificial Intelligence 451 Cybersecurity 445 Computer Engineering 444 Video Game Development 422 Computer Science 400 Data Science 387 (Slightly imbalanced but nothing crazy.)

    2 . What I’m doing Train/test split: 80 / 20 stratified

    Pre‑processing: StandardScaler on the 25 numeric columns

    Label encoding: LabelEncoder + to_categorical

    Model: 512‑256‑128 DenseReLU + BatchNorm + Dropout + L2; softmax output

    Optimizer: Adam − LR = 0.0005–0.0015

    Callbacks:

    EarlyStopping(monitor='val_loss', patience=40, restore_best_weights=True)

    ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)

    Batch size: 64

    Epochs: up to 200 (often stops ≈ 120‑150 by early‑stopping)

    3 . Code (minimal but reproducible) python

    import numpy as np
    import pandas as pd
    from sklearn.model_selection import train_test_split
    import keras
    from tensorflow.keras import layers, models # type: ignore
    from tensorflow.keras.models import Sequential # type: ignore
    from tensorflow.keras.optimizers import Adam # type: ignore
    from keras.callbacks import EarlyStopping # type: ignore
    from sklearn.preprocessing import StandardScaler
    from keras.callbacks import ReduceLROnPlateau # type: ignore
    from sklearn.preprocessing import LabelEncoder
    import matplotlib.pyplot as plt
    import pickle
    from tensorflow.keras import regularizers # type: ignore



    def main():
    df = pd.read_csv("../../../datasets/ComputerDataSet.csv")

    evidence = df.iloc[:, 0:25].values
    label = df["Career"]

    evidence_array = np.array(evidence, dtype=int)

    encoder = LabelEncoder()
    labeled_label = encoder.fit_transform(label)
    categorical_label = keras.utils.to_categorical(labeled_label)



    lr_stopping = EarlyStopping(
    monitor = 'val_loss',
    patience = 80,
    restore_best_weights = True,
    verbose = 1
    )

    lr_reduce = ReduceLROnPlateau(
    monitor = 'val_loss',
    factor = 0.5,
    patience = 10,
    verbose = 1,
    min_lr = 1e-6
    )
    #normalize
    scaler = StandardScaler()
    evidence_normalize = scaler.fit_transform(evidence_array)

    x_train, x_test, y_train, y_test = train_test_split(
    evidence_normalize,
    categorical_label,
    train_size=0.80,
    test_size=0.20,
    stratify=labeled_label,
    random_state=42
    )

    with open("scalerNormalizeComputer.pkt", "wb") as f1:
    pickle.dump(scaler, f1)

    with open("encoderComputer.pkt", "wb") as f2:
    pickle.dump(encoder, f2)


    model = train_model(x_train.shape[1], len(encoder.classes_))

    model.fit(x_train, y_train, batch_size=32, epochs=300, validation_split=0.2, callbacks=[lr_stopping, lr_reduce])

    test_loss, test_accuracy = model.evaluate(x_test,y_test)


    print(f"this is the accuracy {test_accuracy}")
    print(f"this is the loss {test_loss}")

    if test_accuracy >= 0.90:
    model.save("ComputerModel.h5")
    """
    history = model.fit(
    x_train, y_train,
    batch_size=64,
    epochs=200,
    validation_split=0.20,
    callbacks=[lr_stopping]
    )

    # Plot
    plt.plot(history.history['accuracy'], label='train acc')
    plt.plot(history.history['val_accuracy'], label='val acc')
    plt.title('Accuracy over epochs')
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.grid(True)
    plt.show()

    """



    def train_model(input_shape,output_shape):
    optimizer = Adam(learning_rate=0.001)
    model = keras.models.Sequential()

    model.add(keras.Input(shape=(input_shape, )))



    model.add(layers.Dense(512, activation="relu", kernel_regularizer=regularizers.l2(1e-4)))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.2))

    model.add(layers.Dense(256, activation="relu", kernel_regularizer=regularizers.l2(1e-4)))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.2))

    model.add(layers.Dense(128, activation="relu"))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.3))

    model.add(layers.Dense(64, activation="relu"))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.3))

    model.add(layers.Dense(32, activation="relu"))
    model.add(layers.BatchNormalization())
    #softmax == multiclasification
    model.add(layers.Dense(output_shape, activation='softmax'))
    #regulartization
    model.compile(
    optimizer=optimizer,
    loss='categorical_crossentropy',
    metrics=['categorical_accuracy']
    )


    return model

    if __name__ == "__main__":
    main()


    4 . What I already tried

    Attempt Result Lower initial LR (0.0003) Training slower, still ~80 % Higher LR (0.0015) Oscillates, early‑stops ~77 % Add label smoothing 0.05 +1 % at best Increase/Decrease Dropout Little effect Add extra Dense(64) layer ±0.5 % Class weights (sklearn compute_class_weight) No measurable gain 5 . What I observe train_acc often climbs to ≈ 95 %, but val_acc plateaus 78–82 %.

    Confusion matrix shows the model confuses AI ↔︎ Software Eng and Cyber Sec ↔︎ Comp Eng most.

    Validation loss still goes down slowly after LR drops, but accuracy barely moves.

    6 . Question What architectural/hyper‑parameter changes or data strategies would you recommend to push validation accuracy toward 90 %?

    I’d especially appreciate insight on:

    Better regularization (e.g., different L2 value, dropout locations)

    Alternative optimizers or LR schedules (Cosine, One‑Cycle, etc.)

    Feature engineering ideas given the Likert‑scale nature of the data

    When a plateau like this suggests my model has reached its information ceiling

    Thanks for any pointers!

    Continue reading...

Compartilhe esta Página