0
0
TensorFlowml~20 mins

K-fold cross-validation in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
K-fold Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of K-fold split indices
Given the following code snippet using sklearn's KFold, what will be the output of the printed train and test indices for fold 1?
TensorFlow
from sklearn.model_selection import KFold
import numpy as np

X = np.array([10, 20, 30, 40, 50])
kf = KFold(n_splits=5, shuffle=False)

for fold, (train_index, test_index) in enumerate(kf.split(X), 1):
    if fold == 1:
        print('Train indices:', train_index)
        print('Test indices:', test_index)
A
Train indices: [1 2 3 4]
Test indices: [0]
B
Train indices: [0 1 2 3]
Test indices: [4]
C
Train indices: [0 2 3 4]
Test indices: [1]
D
Train indices: [0 1 3 4]
Test indices: [2]
Attempts:
2 left
💡 Hint
Remember that KFold splits data into equal parts without shuffling by default.
Model Choice
intermediate
2:00remaining
Choosing model evaluation strategy
You have a small dataset and want to estimate your TensorFlow model's performance reliably. Which cross-validation strategy is best to reduce variance in performance estimates?
AUse 10-fold cross-validation with shuffling
BUse a single train-test split with 80% training data
CUse 5-fold cross-validation without shuffling
DUse leave-one-out cross-validation without shuffling
Attempts:
2 left
💡 Hint
More folds with shuffling usually give better performance estimates on small datasets.
Hyperparameter
advanced
2:00remaining
Effect of number of folds on training time
If you increase the number of folds in K-fold cross-validation from 5 to 10 for training a TensorFlow model, what is the expected effect on total training time?
ATraining time will be unpredictable and may increase or decrease randomly
BTraining time will roughly double because the model trains on more folds
CTraining time will stay the same because total data trained is unchanged
DTraining time will decrease because each fold trains on less data
Attempts:
2 left
💡 Hint
Each fold requires training a separate model on a subset of data.
Metrics
advanced
2:00remaining
Calculating average accuracy from K-fold results
You performed 4-fold cross-validation on a classification model and got accuracies: [0.82, 0.85, 0.80, 0.83]. What is the correct average accuracy to report?
A0.825
B0.8250
C82.5%
DAll of the above
Attempts:
2 left
💡 Hint
Average accuracy is the mean of fold accuracies; formatting can vary.
🔧 Debug
expert
3:00remaining
Identifying error in K-fold TensorFlow training loop
Consider this TensorFlow K-fold training loop code snippet. What error will it raise? ```python from sklearn.model_selection import KFold import tensorflow as tf import numpy as np X = np.random.rand(100, 10) y = np.random.randint(0, 2, 100) kf = KFold(n_splits=5, shuffle=True) for train_idx, test_idx in kf.split(X): model = tf.keras.Sequential([ tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.fit(X[train_idx], y[train_idx], epochs=3) loss, acc = model.evaluate(X[test_idx], y[test_idx]) print(f"Test accuracy: {acc}") print(model.predict(X)) ```
ANo error; code runs and prints predictions
BValueError due to shape mismatch in model.predict input
CNameError because model is undefined outside the loop
DRuntimeError because model.fit is called multiple times on same model
Attempts:
2 left
💡 Hint
Check where the variable 'model' is defined and used.