TensorFlow Keras中model.predict()预测结果异常问题求助
Hey there, let's break down why your model's predictions are all identical and work through the fixes step by step.
1. First: Make Sure Your Input Shape Matches the Model's Expectations
Your model's input layer is defined with input_dim=w (and based on your test samples, w is 6), which means the model expects inputs in the shape (None, 6) — a 2D array where each row is a sample with 6 features.
Common Input Mistakes & Fixes:
- When you grab a single sample like
b = X[11], first check its shape:print("Single sample shape:", b.shape)- If the output is
(6,)(a 1D array): Convert it to a 2D array with shape(1,6)usingreshape(this works reliably no matter how many features you have):input_data = b.reshape(1, -1) # The -1 lets Python auto-calculate the dimension size n = model.predict(input_data, verbose=0) - If the output is
(1,6)(already 2D): Pass it directly tomodel.predict(b)— don’t wrap it innp.array([b]), since that would create a 3D array(1,1,6)and trigger the shape mismatch warning you saw earlier.
- If the output is
2. The Core Issue: Mismatched Feature Scales
Looking at your test samples, your features have wildly different value ranges:
- The first 5 features are between
0.004and0.45 - The last feature ranges from
8.055all the way up to526.18
Neural networks are extremely sensitive to input scale. Without normalization/standardization, the model can’t learn effectively — it either suffers from vanishing/exploding gradients, or gets dominated by the large-scale feature, leading to identical predictions for all inputs.
Fix: Normalize Your Data Before Training
Use a scaler from Scikit-learn to standardize your features (always fit the scaler only on training data to avoid data leakage):
from sklearn.preprocessing import StandardScaler # Initialize scaler and fit it to training data scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X[train]) X_test_scaled = scaler.transform(X[test]) # Train with scaled data history = model.fit(X_train_scaled, Y[train], epochs=epoc, batch_size=bs) # When predicting, scale the input with the same scaler input_data_scaled = scaler.transform(np.array([b]).reshape(1, -1)) n = model.predict(input_data_scaled)
3. Verify Your Model Is Actually Learning
Check the training loss history to confirm the model is improving:
print("Training loss over epochs:", history.history['loss'])
If the loss drops steadily and stabilizes, the model is training correctly. If it stays flat or barely changes, you may need to:
- Adjust the learning rate (try
1e-3or1e-4instead of your currentlr) - Tweak the network structure (enable the commented-out hidden layer, or adjust the size of
lw1) - Increase the number of training epochs (
epoc)
Wrap-Up
Start with normalizing your data — this is the most likely cause of your constant predictions. Then double-check your input shape matches what the model expects. After these fixes, your model.predict() should return varied, reasonable results.
内容的提问来源于stack exchange,提问作者Artur Perovskii

