Keras MLP模型准确率异常求助:初始为0,调标签后仍无提升
Hey there, let's figure out why your MLP is giving you such frustrating accuracy results—first 0% across the board, then stuck around 46% even after adjusting your labels. Let's break down the issues and fixes step by step:
1. You’re Passing the Wrong Data to model.fit()
Looking at your training code:
model.fit(train_set, test_set, validation_split = 0.10, epochs = 50, batch_size = 10, shuffle = True, verbose = 2)
This is a critical mistake! The fit() method requires training features as the first parameter and training labels as the second. Right now, you’re feeding test_set as the target labels the model should learn—no wonder your initial accuracy was 0%! Even after adjusting labels, the model is trying to learn from incorrect targets, which explains the high loss and stagnant accuracy.
Fix: Split your dataset properly into features and labels first. For example:
# Assuming your train_set is structured as (X_train, y_train) and test_set as (X_test, y_test) model.fit(X_train, y_train, validation_split=0.10, epochs=50, batch_size=10, shuffle=True, verbose=2) # When evaluating, use the correct test features and labels too: loss, accuracy = model.evaluate(X_test, y_test)
2. No Feature Scaling (A Big No-No for MLPs)
MLPs with ReLU activation are super sensitive to the scale of input features. If your features are on drastically different scales (e.g., one ranges from 0-1000, another from 0-1), the model can’t learn meaningful patterns—this leads to high loss and accuracy that doesn’t improve.
Fix: Standardize or normalize your features before training:
from sklearn.preprocessing import StandardScaler # Fit scaler on training data only to avoid data leakage scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Use scaled features for training and evaluation model.fit(X_train_scaled, y_train, ...) loss, accuracy = model.evaluate(X_test_scaled, y_test)
3. Check for Class Imbalance
After adjusting labels, your accuracy hovered around 46-48%—this is a classic sign of class imbalance. If one class makes up ~46% of your dataset, the model might just be predicting that class every time to get a baseline accuracy, instead of learning actual patterns.
Fix:
- First, check your class distribution:
import numpy as np print("Training label distribution:", np.bincount(y_train)) - If imbalance exists, try:
- Adding
class_weight='balanced'to yourmodel.fit()call to weight minority classes more heavily - Oversampling the minority class or undersampling the majority class
- Using metrics like F1-score instead of accuracy to better gauge model performance (accuracy is misleading for imbalanced data)
- Adding
4. Your Model Might Be Overcomplicating Things (Or Missing Regularization)
Your model has 5 dense layers with increasing units (10→20→30→40→50) without any regularization. For many datasets, this is overkill—too many layers can lead to gradient vanishing (where signals get lost as they pass through layers) or overfitting (the model memorizes training data instead of generalizing).
Fixes:
- Add dropout layers to prevent overfitting:
model = keras.models.Sequential() model.add(keras.layers.Dense(layers[0], input_dim=input_dim, activation='relu')) model.add(keras.layers.Dropout(0.2)) # Drop 20% of units randomly for layer in layers[1:]: model.add(keras.layers.Dense(layer, activation='relu')) model.add(keras.layers.Dropout(0.2)) model.add(keras.layers.Dense(1, activation='sigmoid')) - Simplify your model if your dataset isn’t large—try starting with 2-3 dense layers instead of 5
- Add L2 regularization to dense layers to penalize large weights:
from keras import regularizers model.add(keras.layers.Dense(layers[0], input_dim=input_dim, activation='relu', kernel_regularizer=regularizers.l2(0.01)))
5. Double-Check Your Label Conversion
You adjusted labels from [-1,1] to [0,1]—make sure this conversion was done correctly. For example, if your original labels were -1 and 1, the right way to convert is:
y_train = (y_train + 1) / 2 # Converts -1 → 0, 1 → 1 y_test = (y_test + 1) / 2
A mistake here could leave invalid labels that don’t match what your sigmoid output (which ranges from 0-1) can predict.
Start with fixing the model.fit() argument order first—this is the most urgent issue causing your weird results. Then work through the other steps one by one, testing after each change to see how your model improves.
内容的提问来源于stack exchange,提问作者Zedaiq

