You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow Keras中model.predict()预测结果异常问题求助

Fixing the Constant Output Issue with 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) using reshape (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 to model.predict(b) — don’t wrap it in np.array([b]), since that would create a 3D array (1,1,6) and trigger the shape mismatch warning you saw earlier.

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.004 and 0.45
  • The last feature ranges from 8.055 all the way up to 526.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-3 or 1e-4 instead of your current lr)
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:03:10