Keras神经网络训练时准确率始终为0.00e+00问题求助
Hey there, let's break down why your model is stuck at 0.0 accuracy and fix this step by step. This is a common pitfall when mixing regression and classification workflows, so let's start with the most likely culprits:
1. You're Using the Wrong Metric for Your Task
First off—accuracy is a classification metric, not a regression metric. Your goal is to predict continuous scores for each team, right? Accuracy measures how often predictions exactly match discrete labels, which is almost impossible for continuous values. That's why you're seeing 0.00e+00 every time.
Fix:
Swap out accuracy for regression-specific metrics like Mean Squared Error (mse) or Mean Absolute Error (mae):
model.compile( optimizer='adam', loss='mean_squared_error', # Use a regression loss too! metrics=['mean_absolute_error', 'mean_squared_error'] )
2. Misaligned Loss Function
If you're still using a classification loss (like categorical_crossentropy) for your score prediction task, your model has no way to learn meaningful patterns. Regression tasks require loss functions that measure the distance between predicted and actual continuous values.
Fix:
Stick with mean_squared_error (MSE) or mean_absolute_error (MAE) as your loss function. MSE penalizes larger errors more heavily, which is often ideal for score prediction.
3. Suboptimal Model Architecture
Your input shape is (116, 2, 3*58)—each sample represents two teams, each with 3 one-hot encoded players (flattened to 174 features). Your model should likely treat each team's features symmetrically (since Team A vs B and B vs A are logically comparable) to learn consistent patterns.
Example Architecture:
Use a shared subnetwork to process each team's features, then predict scores for both:
from tensorflow.keras.layers import Input, Dense, Lambda, Concatenate from tensorflow.keras.models import Model # Input layer: (samples, 2 teams, 174 features) inputs = Input(shape=(2, 3*58)) # Split input into individual team features team1 = Lambda(lambda x: x[:, 0, :])(inputs) # Shape: (samples, 174) team2 = Lambda(lambda x: x[:, 1, :])(inputs) # Shape: (samples, 174) # Shared dense layers to learn team feature representations shared_dense = Dense(64, activation='relu', kernel_regularizer='l2') t1_features = shared_dense(team1) t2_features = shared_dense(team2) # Predict score for each team (ReLU prevents negative scores) t1_score = Dense(1, activation='relu')(t1_features) t2_score = Dense(1, activation='relu')(t2_features) # Combine outputs to match your target shape (samples, 2, 1) outputs = Concatenate(axis=1)([t1_score, t2_score]) outputs = Lambda(lambda x: x[:, :, None])(outputs) # Add final dimension model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer='adam', loss='mse', metrics=['mae'])
4. Small Dataset Size
With only 116 samples, your model might struggle to generalize or even learn basic patterns. Here's how to mitigate this:
- Data Augmentation: Generate synthetic samples by swapping team positions (e.g., take a sample where Team A vs Team B has scores (X,Y), create a new sample where Team B vs Team A has scores (Y,X)). This doubles your dataset instantly.
- Regularization: Add
Dropout(0.2)layers orkernel_regularizer='l2'to dense layers to prevent overfitting. - Simplify the Model: Start with a smaller network (fewer layers/units) before scaling up—complex models will overfit tiny datasets quickly.
5. Label and Input Checks
- Verify your target labels are continuous values (not categorical). If scores are discrete ranks, you might need a classification approach, but that would require adjusting your output layer and metrics accordingly.
- Ensure your one-hot encoded inputs are correctly formatted: each player's vector should have exactly one 1.0 and the rest 0.0. A quick check with
np.sum(inputs[0,0,:].reshape(3,58), axis=1)should return[1,1,1]for each team's players.
Start with fixing the metric and loss function first—this is almost certainly the root cause of your 0.0 accuracy issue. Once that's sorted, tweak the architecture and dataset to improve performance.
内容的提问来源于stack exchange,提问作者Rushil Joshi

