Tensorflow accuracy at .99 but predictions awful
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
When TensorFlow reports 0.99 accuracy but real predictions are poor, the model is usually optimizing the wrong thing for your problem setup. Accuracy alone can hide major failure modes.
Why this happens
1. Class imbalance
If 99% of samples are class A, a model that always predicts class A gets 99% accuracy while being useless for class B.
2. Data leakage
If training features include future information or label-derived signals, validation accuracy can look excellent but collapse in production.
3. Overfitting
The model memorizes training patterns and does not generalize to unseen examples.
4. Train/serve mismatch
Different preprocessing in training vs inference (normalization, tokenization, label encoding, image resize) causes bad live predictions.
5. Wrong threshold
For probabilistic classifiers, a default threshold of 0.5 may be suboptimal. Accuracy can look high while precision/recall for the target class is poor.
What to check first
- Inspect class distribution in train, validation, and test splits.
- Plot a confusion matrix, not just a single scalar metric.
- Report precision, recall, F1, and PR-AUC/ROC-AUC.
- Verify preprocessing parity between training and inference pipelines.
- Evaluate on a truly held-out dataset from the same production distribution.
Metric formulas (plain text)
- Precision = TP / (TP + FP)
- Recall = TP / (TP + FN)
- F1 = 2 * (Precision * Recall) / (Precision + Recall)
TensorFlow-focused fixes
- Use class weights:
model.fit(..., class_weight=...) - Use stratified splits when possible.
- Add regularization (dropout, weight decay) and early stopping.
- Calibrate probability thresholds on validation data.
- Use data augmentation only when it preserves label meaning.
Minimal diagnostic workflow
- Train baseline model and save per-class metrics.
- Generate confusion matrix on validation set.
- Tune decision threshold for target business metric.
- Re-run evaluation on held-out test set.
- Sanity-check with manual examples from production.
Summary Table
| Problem Area | Explanation | Potential Solutions |
| Class Imbalance | Majority class dominates, inflating accuracy | Class weights, resampling, stratified split |
| Overfitting | Model memorizes training data | Dropout, regularization, early stopping |
| Data Leakage | Validation contains leaked target signal | Rebuild splits/features, strict pipeline boundaries |
| Evaluation Gaps | Single metric hides failure modes | Confusion matrix, F1, PR-AUC, per-class metrics |
| Train/Serve Drift | Different preprocessing at inference time | Shared preprocessing code and parity tests |
Related reading
- TensorFlow Adding Class to Pre-trained Inception Model Outputting Full Image Hierarchy
- TensorFlow Adding Class to Pre-trained Inception Model Outputting Full Image Hierarchy
- Tensorflow after 1.15 - No need to install tensorflow-gpu package
- Tensorflow aggregation_method for optimizers
- Tensorflow allocating GPU memory when using tf.device'/cpu0
- Tensorflow Allocation Memory Allocation of 38535168 exceeds 10 of system memory
- Tensorflow always predict the same output
- Tensorflow and Anaconda on Ubuntu?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.