Bird
Raised Fist0
TensorFlowml~10 mins

Error analysis patterns in TensorFlow - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the mean absolute error between true and predicted values.

TensorFlow
from sklearn.metrics import [1]
true = [3, -0.5, 2, 7]
pred = [2.5, 0.0, 2, 8]
error = [1](true, pred)
print(error)
Drag options to blanks, or click blank then click option'
Aaccuracy_score
Bmean_squared_error
Cr2_score
Dmean_absolute_error
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean_squared_error which squares errors instead of absolute values.
Using accuracy_score which is for classification, not regression.
2fill in blank
medium

Complete the code to identify samples where the prediction error is greater than 1.0.

TensorFlow
errors = [abs(t - p) for t, p in zip(true, pred)]
high_error_indices = [i for i, e in enumerate(errors) if e [1] 1.0]
print(high_error_indices)
Drag options to blanks, or click blank then click option'
A>
B<=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < which selects small errors instead of large ones.
Using == which only selects errors exactly equal to 1.0.
3fill in blank
hard

Fix the error in the code to compute residuals (difference between true and predicted values).

TensorFlow
residuals = [true[i] [1] pred[i] for i in range(len(true))]
print(residuals)
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Adding true and predicted values instead of subtracting.
Multiplying or dividing which do not represent residuals.
4fill in blank
hard

Fill both blanks to create a dictionary of samples with residuals greater than 0.5.

TensorFlow
large_residuals = {i: residuals[i] for i in range(len(residuals)) if abs(residuals[i]) [1] 0.5 and residuals[i] [2] 0}
print(large_residuals)
Drag options to blanks, or click blank then click option'
A>
B<
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using < which selects smaller residuals.
Using == which only selects exact values.
5fill in blank
hard

Fill all three blanks to create a dictionary of samples where the residual is negative and its absolute value is greater than 0.7.

TensorFlow
negative_large_residuals = {i: residuals[i] for i in range(len(residuals)) if residuals[i] [1] 0 and abs(residuals[i]) [2] 0.7 and residuals[i] [3] -1}
print(negative_large_residuals)
Drag options to blanks, or click blank then click option'
A<
B>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for negative check.
Using == which restricts to exact values.

Practice

(1/5)
1. What is the main purpose of error analysis in TensorFlow models?
easy
A. To find where and why the model makes mistakes
B. To increase the size of the training dataset
C. To speed up model training
D. To reduce the number of layers in the model

Solution

  1. Step 1: Understand error analysis purpose

    Error analysis focuses on identifying mistakes made by the model during prediction.
  2. Step 2: Compare options with purpose

    Only To find where and why the model makes mistakes correctly states that error analysis finds where and why the model makes mistakes.
  3. Final Answer:

    To find where and why the model makes mistakes -> Option A
  4. Quick Check:

    Error analysis = find model mistakes [OK]
Hint: Error analysis = find and understand model mistakes [OK]
Common Mistakes:
  • Confusing error analysis with data augmentation
  • Thinking error analysis speeds up training
  • Believing error analysis changes model architecture
2. Which TensorFlow function is commonly used to compare model predictions with true labels during error analysis?
easy
A. tf.argmax()
B. tf.equal()
C. tf.reshape()
D. tf.reduce_sum()

Solution

  1. Step 1: Identify function for comparing predictions and labels

    tf.equal() compares two tensors element-wise and returns True where they match.
  2. Step 2: Check other options

    tf.argmax() finds index of max value, tf.reshape() changes shape, tf.reduce_sum() sums values; none compare predictions to labels.
  3. Final Answer:

    tf.equal() -> Option B
  4. Quick Check:

    Compare predictions and labels = tf.equal() [OK]
Hint: Use tf.equal() to check prediction correctness [OK]
Common Mistakes:
  • Using tf.argmax() to compare directly
  • Confusing reshape with comparison
  • Summing values instead of comparing
3. Given the following TensorFlow code snippet, what will be the output?
predictions = tf.constant([1, 0, 1, 1])
labels = tf.constant([1, 1, 0, 1])
errors = tf.not_equal(predictions, labels)
print(tf.reduce_sum(tf.cast(errors, tf.int32)).numpy())
medium
A. 4
B. 1
C. 3
D. 2

Solution

  1. Step 1: Compare predictions and labels element-wise

    predictions = [1,0,1,1], labels = [1,1,0,1]. Differences at index 1 (0 vs 1) and index 2 (1 vs 0).
  2. Step 2: Count number of errors

    tf.not_equal returns True for mismatches. There are 2 mismatches, so sum is 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Count mismatches = 2 [OK]
Hint: Count mismatches with tf.not_equal and sum [OK]
Common Mistakes:
  • Counting matches instead of mismatches
  • Confusing tf.equal with tf.not_equal
  • Forgetting to cast booleans to int before summing
4. You run this TensorFlow code to find errors but get a TypeError:
errors = tf.not_equal(predictions, labels)
error_count = tf.reduce_sum(errors).numpy()
What is the likely cause and fix?
medium
A. predictions and labels have different shapes
B. tf.reduce_sum() requires a Python list input
C. errors is boolean; cast to int before summing
D. numpy() cannot be called on TensorFlow tensors

Solution

  1. Step 1: Identify error cause

    tf.not_equal returns a boolean tensor. tf.reduce_sum on booleans causes TypeError without casting.
  2. Step 2: Fix by casting booleans to int

    Use tf.cast(errors, tf.int32) before tf.reduce_sum to count True values correctly.
  3. Final Answer:

    errors is boolean; cast to int before summing -> Option C
  4. Quick Check:

    Cast booleans to int before sum [OK]
Hint: Cast booleans to int before summing [OK]
Common Mistakes:
  • Ignoring data type of errors tensor
  • Assuming reduce_sum works on booleans directly
  • Misunderstanding numpy() usage
5. You want to perform error analysis by grouping model errors by class label in TensorFlow. Which approach best helps identify which classes have the most errors?
hard
A. Use tf.boolean_mask to select errors per class, then count errors per class
B. Shuffle the dataset randomly before error analysis
C. Increase batch size during model training
D. Use tf.argmax on predictions without comparing to labels

Solution

  1. Step 1: Understand grouping errors by class

    To find errors per class, select errors where label equals that class using tf.boolean_mask.
  2. Step 2: Count errors per class

    Counting errors per class after masking helps identify classes with most mistakes.
  3. Final Answer:

    Use tf.boolean_mask to select errors per class, then count errors per class -> Option A
  4. Quick Check:

    Group errors by class using boolean mask [OK]
Hint: Mask errors by class label to count per class [OK]
Common Mistakes:
  • Shuffling data does not group errors
  • Batch size affects training, not error grouping
  • Using tf.argmax alone ignores true labels
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