Bird
Raised Fist0
TensorFlowml~20 mins

ROC and AUC curves in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ROC and AUC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding ROC Curve Basics

What does the ROC curve represent in a binary classification model?

AThe relationship between precision and recall for different classes
BThe trade-off between true positive rate and false positive rate at different thresholds
CThe distribution of predicted probabilities for positive class only
DThe accuracy of the model on the training dataset
Attempts:
2 left
💡 Hint

Think about what happens when you change the decision threshold in classification.

❓ Predict Output
intermediate
2:00remaining
Output of TensorFlow AUC Metric Calculation

What is the output of the following TensorFlow code snippet?

TensorFlow
import tensorflow as tf

labels = [0, 0, 1, 1]
predictions = [0.1, 0.4, 0.35, 0.8]
auc_metric = tf.keras.metrics.AUC()
auc_metric.update_state(labels, predictions)
result = auc_metric.result().numpy()
print(round(result, 2))
A0.95
B0.85
C0.65
D0.75
Attempts:
2 left
💡 Hint

Calculate the area under the ROC curve for the given predictions and labels.

❓ Model Choice
advanced
1:30remaining
Choosing the Best Model Based on ROC AUC

You trained three binary classifiers and obtained these ROC AUC scores on the validation set: Model A: 0.82, Model B: 0.91, Model C: 0.88. Which model should you select if you want the best overall ability to distinguish classes?

AModel B
BAll models perform equally well
CModel C
DModel A
Attempts:
2 left
💡 Hint

Higher AUC means better class separation ability.

❓ Hyperparameter
advanced
1:30remaining
Effect of Threshold on ROC Curve

How does changing the classification threshold affect the ROC curve?

AIt moves the point along the ROC curve, changing true positive and false positive rates
BIt changes the shape of the ROC curve itself
CIt only affects precision but not the ROC curve
DIt has no effect on ROC curve metrics
Attempts:
2 left
💡 Hint

Think about what happens when you decide to be more or less strict in classifying positives.

🔧 Debug
expert
2:00remaining
Identifying the Bug in AUC Calculation Code

What error does the following TensorFlow code produce?

import tensorflow as tf

labels = [0, 1, 0, 1]
predictions = [0.2, 0.8, 0.4]
auc_metric = tf.keras.metrics.AUC()
auc_metric.update_state(labels, predictions)
print(auc_metric.result().numpy())
ANo error, outputs a float value
BTypeError because predictions are not tensors
CValueError due to mismatched lengths of labels and predictions
DAttributeError because AUC metric is not imported correctly
Attempts:
2 left
💡 Hint

Check if labels and predictions have the same number of elements.

Practice

(1/5)
1. What does the ROC curve represent in machine learning?
easy
A. The number of layers in a neural network
B. The trade-off between true positive rate and false positive rate
C. The loss value during model training
D. The accuracy of the model on training data

Solution

  1. Step 1: Understand ROC curve basics

    The ROC curve plots the true positive rate (sensitivity) against the false positive rate (1 - specificity) at various threshold settings.
  2. Step 2: Identify what ROC shows

    This curve shows the trade-off between correctly identifying positives and incorrectly labeling negatives as positives.
  3. Final Answer:

    The trade-off between true positive rate and false positive rate -> Option B
  4. Quick Check:

    ROC curve = trade-off between TPR and FPR [OK]
Hint: ROC shows TPR vs FPR trade-off [OK]
Common Mistakes:
  • Confusing ROC with accuracy
  • Thinking ROC shows loss values
  • Assuming ROC relates to model layers
2. Which TensorFlow function is used to compute the AUC metric during model training?
easy
A. tf.metrics.accuracy()
B. tf.keras.layers.Dense()
C. tf.losses.mean_squared_error()
D. tf.keras.metrics.AUC()

Solution

  1. Step 1: Identify TensorFlow metric for AUC

    TensorFlow provides a built-in metric called tf.keras.metrics.AUC() to calculate the Area Under the ROC Curve.
  2. Step 2: Differentiate from other functions

    tf.metrics.accuracy() measures accuracy, mean_squared_error() is a loss, and Dense() is a layer, not a metric.
  3. Final Answer:

    tf.keras.metrics.AUC() -> Option D
  4. Quick Check:

    AUC metric in TensorFlow = tf.keras.metrics.AUC() [OK]
Hint: AUC metric is tf.keras.metrics.AUC() [OK]
Common Mistakes:
  • Using accuracy metric instead of AUC
  • Confusing loss functions with metrics
  • Selecting layer functions as metrics
3. Given the following TensorFlow code snippet, what will be the printed AUC value?
import tensorflow as tf

labels = [0, 0, 1, 1]
predictions = [0.1, 0.4, 0.35, 0.8]
auc_metric = tf.keras.metrics.AUC()
auc_metric.update_state(labels, predictions)
print(round(auc_metric.result().numpy(), 2))
medium
A. 0.75
B. 0.85
C. 0.65
D. 0.95

Solution

  1. Step 1: Understand AUC calculation

    AUC measures how well predictions rank positive samples higher than negatives. Here, predictions are [0.1, 0.4, 0.35, 0.8] with labels [0,0,1,1].
  2. Step 2: Calculate AUC manually

    Pairs: (0.35 > 0.1), (0.35 < 0.4), (0.8 > 0.1), (0.8 > 0.4). 3 out of 4 pairs rank correctly, so AUC ≈ 0.75.
  3. Final Answer:

    0.75 -> Option A
  4. Quick Check:

    Correct ranking pairs / total pairs = 0.75 [OK]
Hint: Count correct positive-negative pairs for AUC [OK]
Common Mistakes:
  • Confusing AUC with accuracy
  • Rounding errors in calculation
  • Misordering prediction-label pairs
4. Identify the error in this TensorFlow code for computing ROC AUC:
import tensorflow as tf
labels = [0, 1, 0, 1]
predictions = [0.2, 0.8, 0.4, 0.6]
auc = tf.keras.metrics.AUC()
auc.update_state(predictions, labels)
print(auc.result().numpy())
medium
A. Labels and predictions are swapped in update_state
B. AUC metric is not initialized correctly
C. Labels contain invalid values
D. Missing model compilation step

Solution

  1. Step 1: Check update_state argument order

    tf.keras.metrics.AUC().update_state() expects labels first, then predictions.
  2. Step 2: Identify swapped arguments

    In the code, predictions are passed first and labels second, which is incorrect and causes wrong metric calculation.
  3. Final Answer:

    Labels and predictions are swapped in update_state -> Option A
  4. Quick Check:

    update_state(labels, predictions) order matters [OK]
Hint: Labels go first, predictions second in update_state [OK]
Common Mistakes:
  • Swapping labels and predictions
  • Ignoring argument order in functions
  • Assuming metric auto-corrects input order
5. You trained a binary classifier in TensorFlow and want to improve the AUC score. Which approach is most effective?
hard
A. Use a smaller batch size only
B. Increase the number of epochs without changing data
C. Tune the classification threshold and retrain with balanced data
D. Remove the AUC metric from model evaluation

Solution

  1. Step 1: Understand factors affecting AUC

    AUC depends on model's ability to rank positives higher than negatives, which improves with balanced data and proper threshold tuning.
  2. Step 2: Evaluate options for improving AUC

    Increasing epochs alone may cause overfitting; smaller batch size alone may not improve ranking; removing AUC metric stops monitoring it.
  3. Final Answer:

    Tune the classification threshold and retrain with balanced data -> Option C
  4. Quick Check:

    Balanced data + threshold tuning improves AUC [OK]
Hint: Balance data and tune threshold to boost AUC [OK]
Common Mistakes:
  • Ignoring data imbalance
  • Overtraining without validation
  • Removing important metrics
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard