When training a model, the key metrics to watch are loss and accuracy. Loss tells us how far off the model's predictions are from the true answers. Accuracy shows how many predictions are correct. We focus on these because they guide us to improve the model step-by-step during training.
Model training component in TensorFlow - Model Metrics & Evaluation
For classification tasks, a confusion matrix helps us see detailed results:
| Predicted Positive | Predicted Negative |
|--------------------|--------------------|
| True Positive (TP) | False Positive (FP) |
| False Negative (FN) | True Negative (TN) |
Example with 100 samples:
TP = 40, FP = 10
FN = 5, TN = 45
Total = 100
During training, improving one metric can lower another. For example:
- Precision means when the model says "yes", it is usually right.
- Recall means the model finds most of the "yes" cases.
In training a spam filter, high precision is important to avoid marking good emails as spam. But in training a cancer detector, high recall is critical to catch all cancer cases, even if some false alarms happen.
Good: Loss decreases steadily, accuracy increases, and validation metrics improve without big gaps from training metrics.
Bad: Loss stays high or jumps around, accuracy is low or does not improve, or validation metrics are much worse than training (sign of overfitting).
- Accuracy Paradox: High accuracy can be misleading if data is unbalanced (e.g., 95% accuracy by always predicting the majority class).
- Data Leakage: If training data leaks into validation, metrics look unrealistically good.
- Overfitting: Training accuracy very high but validation accuracy low means the model memorizes training data but fails on new data.
Your model has 98% accuracy but only 12% recall on fraud cases. Is it good for production?
Answer: No. The model misses most fraud cases (low recall), which is dangerous. Despite high accuracy, it fails the main goal of catching fraud. You need to improve recall before using it.
Practice
model.fit function in TensorFlow?Solution
Step 1: Understand the role of
model.fitmodel.fitis used to train the model by adjusting its weights based on the training data.Step 2: Differentiate from other functions
Functions like
model.evaluatecheck performance,model.savesaves the model, andmodel.predictmakes predictions. These are not for training.Final Answer:
To train the model on the given data -> Option DQuick Check:
model.fit= train model [OK]
- Confusing
fitwithpredict - Thinking
fitsaves the model - Mixing up training and evaluation functions
model.fit?Solution
Step 1: Recall the correct parameter name
The parameter to set training cycles isepochs, plural.Step 2: Check the options for correct syntax
Onlymodel.fit(x_train, y_train, epochs=10)usesepochs=10correctly. Others use incorrect parameter names.Final Answer:
model.fit(x_train, y_train, epochs=10) -> Option BQuick Check:
Useepochsto set training cycles [OK]
- Using 'epoch' instead of 'epochs'
- Using 'n_epochs' or 'cycles' which are invalid
- Misspelling the parameter name
import tensorflow as tf import numpy as np x_train = np.array([[1], [2], [3], [4]]) y_train = np.array([2, 4, 6, 8]) model = tf.keras.Sequential([ tf.keras.layers.Dense(1, input_shape=(1,)) ]) model.compile(optimizer='sgd', loss='mse') history = model.fit(x_train, y_train, epochs=2, verbose=0) print(history.history['loss'])
Solution
Step 1: Understand the code training process
The model is a simple linear layer trained with mean squared error loss for 2 epochs on a linear dataset.Step 2: Predict the output of
This returns a list of loss values for each epoch, which should decrease as the model learns.history.history['loss']Final Answer:
A list of two loss values decreasing over epochs -> Option AQuick Check:
Loss list decreases over epochs [OK]
- Expecting exact loss numbers without running
- Thinking metrics are required for loss output
- Assuming loss list is empty or error
model = tf.keras.Sequential([ tf.keras.layers.Dense(1, input_shape=(1,)) ]) model.compile(optimizer='adam', loss='mse') model.fit(x_train, y_train, epoch=5)
Solution
Step 1: Check the parameters of
The parameter to set training cycles ismodel.fitepochs, notepoch.Step 2: Verify other parts of the code
Optimizer and loss are correctly set. Input shape (1,) is valid for single feature input.Final Answer:
The parameter 'epoch' should be 'epochs' in model.fit -> Option AQuick Check:
Use 'epochs' plural in model.fit [OK]
- Using 'epoch' instead of 'epochs'
- Assuming input_shape is wrong for single feature
- Thinking 'mse' is invalid loss
Solution
Step 1: Understand why loss might not decrease
Loss not decreasing often means the optimizer or learning rate is not suitable for the problem.Step 2: Choose the best corrective action
Changing to a better optimizer like 'adam' and tuning learning rate helps the model learn better. Increasing epochs alone won't help if learning is stuck.Final Answer:
Change the optimizer to a different one like 'adam' and check learning rate -> Option CQuick Check:
Adjust optimizer and learning rate to improve training [OK]
- Just increasing epochs without fixing optimizer
- Removing loss function causes errors
- Using predict instead of fit for training
Ready to prove it?
Pass the test and this pattern moves unknown
