AI and ML with TensorFlow & PyTorch

Chapter 7
Training, Evaluation and Experimentation

Bachelors Program in AI & ML
AI and ML with TensorFlow & PyTorch   •   Bachelor

Training, Evaluation and Experimentation

Introduction

A machine-learning model is not established by training loss alone. A model may achieve low training error yet fail on unseen data, perform well on average while failing a minority group, or appear accurate because the evaluation procedure accidentally exposes information from the future. Reliable machine learning therefore requires a disciplined experimental process covering data partitioning, model fitting, validation, hyperparameter selection, metric choice, uncertainty analysis, and reproducibility.

This chapter develops a practical framework for training and evaluating models in TensorFlow and PyTorch. It focuses on classification, regression, and ranking tasks, while also examining underfitting, overfitting, distribution drift, ablation studies, and experimental governance.

Learning Objectives

By the end of this chapter, students should be able to:

1. Distinguish training, validation, and test data and select an appropriate validation strategy.

2. Explain underfitting, overfitting, bias, variance, and data leakage.

3. Select and interpret classification, regression, and ranking metrics.

4. Design hyperparameter searches, learning-curve analyses, and ablation studies.

5. Diagnose drift and uncertainty in deployed models.

6. Construct reproducible experiments using TensorFlow or PyTorch.

7. Critically assess the limitations, fairness, and governance implications of an evaluation result.

1. The Experimental Pipeline

A rigorous experiment normally follows these stages:

1. Define the prediction task and operational success criterion.

2. Establish a data-generating and sampling protocol.

3. Create independent training, validation, and test partitions.

4. Fit preprocessing operations only on permitted training data.

5. Train candidate models using the training set.

6. Select hyperparameters using validation data or cross-validation.

7. Lock the final model and evaluate it once on the test set.

8. Report uncertainty, subgroup performance, failure modes, and resource usage.

9. Reproduce the result from a recorded configuration and code version.

The test set is not a second validation set. Repeatedly inspecting test performance and changing the model creates test-set overfitting. If the test set has been used extensively during development, a new untouched test set or an external evaluation set is needed.

A useful experimental record includes the dataset version, inclusion criteria, preprocessing code, random seeds, software versions, hardware, model architecture, hyperparameters, training duration, checkpoints, metric definitions, and evaluation outputs.

2. Data Splitting and Validation Strategies

2.1 Holdout validation

In a simple holdout design, data are divided into training, validation, and test sets. The training set estimates model parameters; the validation set supports model and hyperparameter selection; the test set estimates generalization after decisions are complete.

Random splitting is appropriate only when observations are approximately independent and identically distributed. In a medical-record, financial, sensor, or recommendation system, several rows may belong to the same person, account, device, or time period. Splitting related rows across partitions can produce leakage because the model sees near-duplicates during training.

2.2 Stratification and group splitting

For imbalanced classification, stratified splitting approximately preserves class proportions in each partition. Group splitting ensures that all observations associated with a group remain in one partition. For example, images from one patient should not occur in both training and test sets when the intended task is generalization to new patients.

2.3 Cross-validation

In k-fold cross-validation, the data are divided into k folds. The model is trained k times, each time using one fold for validation and the remaining folds for training. If the scores are m1, m2, ..., mk, the mean score is

mean = (1/k) Σ mi,

and the sample standard deviation is

s = sqrt[Σ(mi − mean)^2/(k − 1)].

The standard deviation describes variation across folds; it is not automatically a confidence interval for future performance. Cross-validation is particularly useful when data are limited, but it can be computationally expensive for deep networks.

Nested cross-validation separates hyperparameter selection from performance estimation. In each outer fold, an inner cross-validation procedure selects hyperparameters, and the outer validation fold estimates performance. This reduces optimistic bias when the dataset is small and many configurations are considered.

2.4 Time-dependent validation

For forecasting or any setting in which the future must be predicted from the past, random splitting is generally invalid. A chronological split or rolling-origin evaluation is more appropriate. Training uses earlier observations, validation uses a later period, and testing uses the latest period. Features must also be available at prediction time; a variable recorded after an event is leakage even if it is statistically predictive.

3. Training Dynamics and Generalization

The empirical risk on a dataset is the average loss over its examples. For observations (xi, yi), a model fθ, and loss function ℓ, empirical risk is

R̂(θ) = (1/n) Σ ℓ(fθ(xi), yi).

Here, θ denotes trainable parameters and n is the number of observations. Optimization attempts to reduce training risk, while the practical objective is low expected risk on new observations from the deployment distribution.

3.1 Underfitting and overfitting

Underfitting occurs when a model is unable to represent the relevant structure or has not been trained sufficiently. Training and validation performance are both poor. Typical remedies include increasing model capacity, improving features, reducing excessive regularization, or training longer.

Overfitting occurs when the model fits idiosyncrasies or noise in the training data. Training performance continues to improve while validation performance deteriorates. Remedies include collecting more data, applying data augmentation, reducing capacity, increasing regularization, early stopping, and improving the validation design.

Bias describes systematic error from restrictive assumptions; variance describes sensitivity to the particular training sample. Increasing model complexity often lowers bias but increases variance. This is a conceptual trade-off rather than a universal monotonic law: optimization, data quality, regularization, and model architecture also matter.

3.2 Learning curves

A learning curve plots training and validation performance against training-set size or training epochs. With epoch-based curves, a widening gap between improving training performance and worsening validation performance suggests overfitting. If both curves remain poor and close, the model may be underfitting.

Learning curves based on increasing dataset size provide additional diagnosis. If validation performance improves substantially as more data are added, additional data may be valuable. If both training and validation scores plateau at a low level, better features, labels, or model structure may be more important.

Early stopping selects the checkpoint with the best validation objective, often subject to a patience parameter. The patience parameter specifies how many epochs without meaningful improvement are tolerated. Early stopping is a model-selection procedure and therefore requires a validation set.

3.3 Regularization

Common regularization methods include weight decay, dropout, data augmentation, label smoothing, and constraints on model complexity. In PyTorch, an optimizer may apply weight decay through an optimizer parameter; in TensorFlow, regularization penalties may be included in the model loss or implemented through optimizer mechanisms. Implementations should be checked carefully because coupled L2 penalties and decoupled weight decay are not identical in all optimizers.

Dropout is ordinarily active during training and disabled during standard evaluation. Forgetting to switch a model to evaluation mode can make results noisy and systematically incorrect. In PyTorch, `model.train()` and `model.eval()` control relevant module behavior; gradients should generally be disabled during evaluation.

4. Hyperparameter Tuning

Parameters are learned from training data; hyperparameters are choices made by the practitioner, such as learning rate, batch size, hidden-layer width, dropout probability, optimizer, and number of training epochs.

A sound tuning procedure begins with a small number of scientifically motivated choices and a predefined evaluation protocol. Common search strategies are:

  • Grid search: evaluates every combination in a specified grid; it is simple but inefficient in large spaces.
  • Random search: samples configurations independently and often explores important dimensions more efficiently than a regular grid.
  • Bayesian optimization: uses previous results to choose promising configurations, which is useful when each training run is expensive.
  • Successive halving or early-stopping-based methods: allocate more resources to configurations that perform well early.

Hyperparameters should be sampled on suitable scales. Learning rates and weight decay are often explored logarithmically because useful values can differ by orders of magnitude. The search space, budget, stopping rule, and selection metric should be fixed before the final comparison.

Tuning must avoid leakage. For example, normalization statistics, vocabulary construction, feature selection, and imputation values must be learned within the training portion of each fold. In deep-learning experiments, tuning dozens of configurations against one validation set can also overfit the validation set. Repeated cross-validation, an external validation set, or a final independent test set helps address this problem.

5. Evaluation Metrics

No single metric is universally best. The metric must reflect the task, class distribution, error costs, and deployment decision.

5.1 Classification

For binary classification, the confusion matrix contains true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN).

Accuracy is

Accuracy = (TP + TN)/(TP + TN + FP + FN).

It can be misleading under severe class imbalance. Precision is

Precision = TP/(TP + FP),

and recall, also called sensitivity or true-positive rate, is

Recall = TP/(TP + FN).

Precision answers, “Among predicted positives, how many are positive?” Recall answers, “Among actual positives, how many were found?” The F1 score is the harmonic mean of precision and recall:

F1 = 2(Precision × Recall)/(Precision + Recall),

provided the denominator is nonzero. F1 gives equal importance to precision and recall and does not incorporate true negatives.

Specificity, or true-negative rate, is TN/(TN + FP). Balanced accuracy for binary classification is the arithmetic mean of recall and specificity. This can be useful when class frequencies are unequal.

The receiver operating characteristic (ROC) curve plots true-positive rate against false-positive rate across thresholds. Area under the ROC curve (AUROC) summarizes ranking ability across thresholds, but may appear strong when the positive class is rare. The precision-recall curve is often more informative for rare-event detection. Average precision is a summary of the precision-recall relationship, but implementations differ in interpolation conventions; reports should identify the definition used.

Log loss evaluates probabilistic predictions. For binary labels yi ∈ {0,1} and predicted probabilities pi, the mean log loss is

−(1/n) Σ [yi log(pi) + (1 − yi) log(1 − pi)].

Probabilities exactly equal to zero or one require numerical protection in implementation. Log loss rewards calibrated probabilities and penalizes confident errors.

Calibration concerns whether predicted probabilities correspond to observed frequencies. A group of cases assigned probability 0.7 should be positive approximately 70% of the time, subject to sampling variation. Reliability diagrams, calibration error summaries, and the Brier score can be used. The binary Brier score is the mean squared error of predicted probabilities: (1/n)Σ(pi − yi)^2.

5.2 Regression

For continuous targets yi and predictions ŷi, mean absolute error (MAE) is

MAE = (1/n)Σ|yi − ŷi|.

Mean squared error (MSE) is

MSE = (1/n)Σ(yi − ŷi)^2,

and root mean squared error (RMSE) is √MSE. MAE is less sensitive to extreme errors than MSE; RMSE retains the target unit and penalizes large errors more strongly. Neither is universally superior.

The coefficient of determination is

R² = 1 − [Σ(yi − ŷi)² / Σ(yi − ȳ)²],

where ȳ is the mean of the observed target values in the evaluation set. R² compares the model with a constant-mean predictor on that set and can be negative when the model performs worse than that baseline. It is not a percentage accuracy measure.

Regression reports should include a meaningful baseline, residual plots, performance across target ranges, and, where relevant, prediction intervals. Percentage metrics such as MAPE can be unstable or undefined when actual values are zero or near zero.

5.3 Ranking

Ranking systems should be evaluated with metrics aligned to the order in which users see results. Precision@k is the proportion of the top k results that are relevant. Recall@k is the proportion of all relevant items retrieved in the top k. These metrics require a definition of relevance and a cutoff k.

Mean reciprocal rank (MRR) focuses on the first relevant result. For query q, reciprocal rank is 1/rq when the first relevant result occurs at rank rq, and zero if no relevant result occurs within the evaluation list. MRR is the mean reciprocal rank across queries.

Normalized discounted cumulative gain at k (NDCG@k) supports graded relevance. A common formulation is

DCG@k = Σ(i=1 to k) [(2^{rel_i} − 1)/log₂(i + 1)],

where rel_i is the relevance grade at rank i. NDCG@k = DCG@k/IDCG@k, where IDCG@k is the maximum possible DCG for that query. The normalization is query-specific. Ranking metrics must account for unobserved relevance, position bias, and differences among users or queries.

6. Ablation Studies and Baselines

An ablation study removes or changes one component while holding other factors as constant as possible. Components might include a feature group, data augmentation, attention mechanism, pretrained initialization, loss term, or retrieval stage. A baseline should be simple, strong enough to be meaningful, and evaluated with the same data partitions and metrics.

For example, to assess data augmentation, compare the complete model with an identical model trained without augmentation, using the same training and validation data, tuning budget, and random-seed protocol. A single run is weak evidence because stochastic optimization may produce differences unrelated to the component. Multiple seeds and confidence intervals or paired comparisons improve the analysis.

Ablation results establish association, not necessarily causal truth. If two components interact, removing one may make the other appear less useful. Factorial experiments or carefully designed combinations may be needed to study interactions. Compute cost, latency, memory, and maintainability should be reported alongside accuracy.

7. Uncertainty, Drift, and Robustness

7.1 Sources of uncertainty

Aleatoric uncertainty reflects irreducible variability or ambiguity in the data, such as noisy measurements. Epistemic uncertainty reflects limited knowledge, such as insufficient training examples or unfamiliar inputs. These categories are conceptually distinct, although practical methods may estimate them imperfectly.

A point estimate should be accompanied by uncertainty where decisions are consequential. Bootstrap resampling can estimate variation in a metric: repeatedly sample evaluation cases with replacement, calculate the metric, and use the resulting distribution to form an interval. Bootstrap intervals require care with dependent observations, such as repeated records from one patient.

For classification, ensembles, stochastic forward passes, and calibrated probabilistic models can provide useful uncertainty signals. High uncertainty is not automatically evidence that a prediction is wrong; it is a reason to consider review, abstention, or additional information.

7.2 Distribution drift

Training data, validation data, and deployment data may differ. Covariate shift refers primarily to a change in the input distribution p(x), while concept shift involves a change in the relationship between inputs and labels, p(y|x). Label shift involves changes in p(y). These distinctions are useful but real systems may exhibit several forms simultaneously.

Monitoring may include feature distributions, missingness, class rates, calibration, subgroup metrics, and delayed ground-truth performance. Statistical drift tests can produce significant results for trivial changes in large samples, so operational thresholds and effect sizes matter. Drift detection should lead to an action plan: investigate data pipelines, retrain, recalibrate, restrict use, or obtain new labels.

Robustness evaluation should include temporal holdouts, geographic or organizational external sets, subgroup analysis, corrupted inputs, and stress tests relevant to deployment. A model that performs well on an IID test set may still fail under domain shift.

8. Reproducibility with TensorFlow and PyTorch

Reproducibility has several levels. Exact reproducibility means obtaining the same computational result under the same environment; statistical reproducibility means obtaining similar conclusions across runs; scientific reproducibility means independently implementing the method and reaching consistent findings.

A reproducible experiment should:

  • version data, code, configuration files, and model checkpoints;
  • record Python, TensorFlow/PyTorch, CUDA, and hardware details;
  • seed Python, NumPy, and framework random-number generators;
  • control data-loader worker seeds and shuffling where practical;
  • record deterministic-operation settings and their performance costs;
  • separate training and evaluation modes;
  • save the preprocessing pipeline and label mapping;
  • log every run, including failed runs and early-stopping decisions.

Deterministic execution may reduce speed or exclude some GPU operations. Even deterministic code cannot guarantee identical results across hardware, drivers, or library versions. Consequently, report both the reproducibility controls and the observed variation across independent seeds.

In TensorFlow, `tf.data` pipelines should be constructed with explicit shuffling, batching, and caching choices. In PyTorch, a `Dataset` and `DataLoader` should clearly define sampling, worker behavior, and collations. In both frameworks, evaluation must not update batch-normalization statistics or apply training-only stochastic transformations.

9. Applied Case: Illustrative Hospital Readmission Model

Background

Consider an illustrative project to predict whether a patient will be readmitted within 30 days of discharge. The dataset contains multiple admissions per patient, laboratory values, diagnosis codes, and discharge information.

Problem

An initial model reports 94% accuracy. However, readmission occurs in only 8% of admissions. Moreover, a feature describing a follow-up appointment scheduled after discharge was included unintentionally.

Analysis

The accuracy is compatible with a trivial model that predicts “no readmission” for every case, achieving approximately 92% accuracy while having zero recall for readmissions. The post-discharge feature is unavailable at the time of prediction and constitutes leakage. Multiple admissions from the same patient also appear across random train and test partitions, allowing patient-specific information to transfer between sets.

Approach

The team removes unavailable features, groups records by patient, and uses a chronological split: earlier admissions for training, a later period for validation, and the most recent period for testing. Because false negatives are clinically important, the team reports recall, precision, PR-AUC, calibration, subgroup performance, and a confusion matrix at a threshold chosen with clinicians. A class-weighted training objective is explored, but the threshold is selected separately from model training because class weighting changes score behavior.

The team compares logistic regression, a gradient-boosted baseline, and a neural network. It performs an ablation removing laboratory features and repeats the main experiment across five random seeds. The final model is calibrated on validation data and evaluated once on the untouched test period.

Outcome

Accuracy falls from 94% to 88%, but this is not necessarily a deterioration: the corrected evaluation is harder and more realistic. Recall at the operational threshold is 0.62, precision is 0.24, and calibration is acceptable for the intended risk-stratification use. Performance is lower for a subgroup with sparse laboratory measurements, so the deployment plan includes missingness monitoring and human review rather than fully automated decisions.

Learning Points

The case demonstrates that evaluation quality depends on temporal availability, group independence, appropriate metrics, threshold selection, calibration, subgroup analysis, and a credible baseline. A high headline accuracy can conceal both leakage and failure on the clinically important class.

10. Ethical, Governance, and Professional Considerations

Evaluation choices distribute risks. A threshold that improves recall may increase false alarms and burden already disadvantaged groups. A model may have different calibration or error rates across demographic groups because of unequal data quality or prevalence. Group metrics should therefore be reported with sample sizes and uncertainty, not interpreted mechanically as proof of fairness or unfairness.

Sensitive attributes may be needed for auditing even when they are excluded from prediction. Access should be governed, and their use should comply with privacy and sector-specific requirements. Medical, financial, employment, and educational applications may require documentation, human oversight, impact assessment, and records of model changes.

Researchers should avoid selective reporting, repeated test-set experimentation, unexplained metric changes, and claims that exceed the evaluation design. Reproducibility is not only a technical convenience; it supports accountability and allows errors to be found.

11. Best-Practice Checklist

Before accepting an experiment, ask:

1. Is the deployment question and decision threshold explicit?

2. Are the partitions independent with respect to time, people, devices, or organizations?

3. Could preprocessing or feature construction leak information?

4. Is there a simple, relevant baseline?

5. Are metrics aligned with costs and class imbalance?

6. Were hyperparameters selected without repeatedly using the test set?

7. Are learning curves, subgroup results, uncertainty, and failure cases reported?

8. Were ablations controlled and repeated across seeds?

9. Are code, data versions, configurations, and environment recorded?

10. Is there a monitoring and retraining plan for deployment drift?

Chapter Summary

Reliable machine learning requires more than minimizing training loss. Training, validation, and test data serve different purposes, and their separation must respect time, groups, and information availability. Cross-validation, stratification, chronological evaluation, and nested validation are tools for different data conditions.

Underfitting produces poor training and validation performance, whereas overfitting produces a widening generalization gap. Learning curves, regularization, early stopping, and suitable baselines support diagnosis. Classification metrics include precision, recall, F1, ROC and precision-recall analysis, log loss, and calibration. Regression evaluation should use MAE, MSE or RMSE, R², residual analysis, and meaningful baselines. Ranking requires query-aware measures such as Precision@k, MRR, and NDCG.

Hyperparameter tuning must be budgeted and leakage-free. Ablation studies test the contribution of components but require controlled comparisons and repeated runs. Uncertainty, drift, subgroup performance, and robustness are essential for deployment. Finally, reproducibility depends on versioning, seeds, environment records, evaluation discipline, and transparent reporting.

Key Terms

Data leakage: Use of information unavailable at prediction time or improperly shared across partitions.

Early stopping: Ending training using a validation-based stopping rule.

Empirical risk: Average loss measured over a finite dataset.

Epistemic uncertainty: Uncertainty arising from limited knowledge or data.

F1 score: Harmonic mean of precision and recall.

Group split: Partitioning that keeps related observations in the same data subset.

Hyperparameter: A configuration selected outside the ordinary parameter-learning process.

Calibration: Agreement between predicted probabilities and observed frequencies.

Learning curve: Plot of performance against training size or training progress.

Nested cross-validation: Cross-validation with an inner selection loop and outer evaluation loop.

NDCG: A ranking metric that discounts lower-ranked graded relevance.

Overfitting: Learning training-specific patterns that do not generalize.

Precision: Proportion of predicted positives that are truly positive.

Recall: Proportion of actual positives that are detected.

Regularization: Methods that constrain or discourage excessive model complexity.

Stratification: Partitioning that approximately preserves class proportions.

Test set: Data reserved for final performance estimation.

Underfitting: Failure to capture relevant structure in the data.

Validation set: Data used for model or hyperparameter selection.

Drift: Change between the data-generating conditions used for development and deployment.

Review Questions

Short Answer Questions

1. Why should the test set not be used repeatedly during model development?

2. Distinguish precision from recall.

3. What is the purpose of early stopping?

4. Why can accuracy be misleading for imbalanced classification?

5. What information does a learning curve provide?

Long Answer Questions

1. Compare holdout validation, k-fold cross-validation, nested cross-validation, and chronological validation. State when each is appropriate.

2. Explain underfitting and overfitting and discuss at least four diagnostic or remedial techniques.

3. Compare MAE, RMSE, and R², including situations in which each may be misleading.

4. Explain how to design a rigorous ablation study for a neural-network component.

5. Discuss the role of reproducibility in TensorFlow and PyTorch experiments.

Analytical / Application Questions

1. A fraud dataset has a 1% positive rate. A model achieves 99% accuracy but zero recall. Which metrics and baseline would you use to evaluate it, and why?

2. A model performs well on a random test split but poorly on the newest month of data. Diagnose possible causes and propose an evaluation and monitoring plan.

3. A hospital model has high overall recall but much lower recall for patients with sparse records. Design an analysis covering data quality, subgroup metrics, uncertainty, threshold selection, and governance.

Multiple Choice Questions with Answers

Question 1

Which partition is primarily used for selecting hyperparameters?

A. Training set

B. Validation set

C. Test set

D. Deployment log

Correct Answer: B. Validation set

Question 2

Which metric is often more informative than AUROC when the positive class is very rare?

A. Precision-recall analysis

B. Training accuracy

C. R-squared

D. Mean squared error

Correct Answer: A. Precision-recall analysis

Question 3

A model has improving training performance but worsening validation performance. What is the most likely diagnosis?

A. Underfitting

B. Overfitting

C. Label shifting only

D. Perfect calibration

Correct Answer: B. Overfitting

Question 4

What does recall measure in binary classification?

A. The proportion of predicted positives that are correct

B. The proportion of actual positives that are detected

C. The proportion of actual negatives that are detected

D. The average squared prediction error

Correct Answer: B. The proportion of actual positives that are detected

Question 5

Which validation design is most appropriate for forecasting future demand?

A. Random splitting across all dates

B. Splitting duplicate records randomly

C. Chronological or rolling-origin evaluation

D. Using only the training loss

Correct Answer: C. Chronological or rolling-origin evaluation

Question 6

Which metric is designed for graded relevance in ranked results?

A. NDCG

B. MAE

C. Specificity

D. Brier score

Correct Answer: A. NDCG

Question 7

What is the main purpose of an ablation study?

A. To guarantee zero deployment drift

B. To measure the contribution of a component by controlled removal or change

C. To replace the test set with training data

D. To eliminate the need for baselines

Correct Answer: B. To measure the contribution of a component by controlled removal or change

Question 8

Which statement about R² is correct?

A. It is always between zero and one

B. It is identical to accuracy

C. It can be negative when a model is worse than a mean baseline

D. It is defined only for classification

Correct Answer: C. It can be negative when a model is worse than a mean baseline

Question 9

What is calibration concerned with?

A. Whether predicted probabilities correspond to observed frequencies

B. Whether training batches have equal size

C. Whether rankings contain exactly k items

D. Whether all features have identical units

Correct Answer: A. Whether predicted probabilities correspond to observed frequencies

Question 10

Which is an example of data leakage?

A. Using a chronological test split

B. Reporting subgroup metrics

C. Using a feature recorded after the prediction event

D. Saving model checkpoints

Correct Answer: C. Using a feature recorded after the prediction event

AI and ML with TensorFlow & PyTorch
Chapter 7 of 15