Bird
Raised Fist0
TensorFlowml~10 mins

Validation split in TensorFlow - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to split the data into training and validation sets using Keras model.fit.

TensorFlow
model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=[1])
Drag options to blanks, or click blank then click option'
ANone
B2
C20
D0.2
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer like 2 or 20 instead of a fraction.
Setting validation_split to None which disables validation.
2fill in blank
medium

Complete the code to create a validation set from training data using sklearn's train_test_split.

TensorFlow
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=[1], random_state=42)
Drag options to blanks, or click blank then click option'
A10
B0.1
C0.5
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer like 10 instead of a fraction.
Setting test_size to None which causes an error.
3fill in blank
hard

Fix the error in the code to correctly use validation_split in model.fit.

TensorFlow
history = model.fit(x_train, y_train, epochs=5, validation_split=[1])
Drag options to blanks, or click blank then click option'
A0.2
BNone
C'0.2'
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Passing validation_split as a string like '0.2'.
Passing an integer like 20 instead of a fraction.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

TensorFlow
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using less than (<) instead of greater than (>) in the condition.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary with uppercase keys, values, and a condition.

TensorFlow
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase.
Using incorrect comparison operators like < or ==.

Practice

(1/5)
1. What is the main purpose of using validation_split in TensorFlow model training?
easy
A. To save the model after each epoch
B. To increase the size of the training dataset
C. To shuffle the training data randomly
D. To automatically reserve a part of training data for checking model performance during training

Solution

  1. Step 1: Understand the role of validation_split

    The validation_split parameter reserves a fraction of training data to test the model during training.
  2. Step 2: Identify the purpose of this reserved data

    This reserved data helps check how well the model generalizes to unseen data and detects overfitting.
  3. Final Answer:

    To automatically reserve a part of training data for checking model performance during training -> Option D
  4. Quick Check:

    Validation split = reserve data for validation [OK]
Hint: Validation split reserves data to test model during training [OK]
Common Mistakes:
  • Thinking validation_split increases training data size
  • Confusing validation_split with data shuffling
  • Assuming validation_split saves the model
2. Which of the following is the correct way to use validation_split in model.fit() in TensorFlow?
easy
A. model.fit(x_train, y_train, validation=0.2, epochs=10)
B. model.fit(x_train, y_train, validation_split=0.2, epochs=10)
C. model.fit(x_train, y_train, val_split=0.2, epochs=10)
D. model.fit(x_train, y_train, split_validation=0.2, epochs=10)

Solution

  1. Step 1: Recall the correct parameter name

    The correct parameter to reserve validation data in model.fit() is validation_split.
  2. Step 2: Check the syntax usage

    The correct syntax is validation_split=0.2 to reserve 20% of training data for validation.
  3. Final Answer:

    model.fit(x_train, y_train, validation_split=0.2, epochs=10) -> Option B
  4. Quick Check:

    Correct parameter name is validation_split [OK]
Hint: Use exact parameter name validation_split in model.fit [OK]
Common Mistakes:
  • Using incorrect parameter names like validation or val_split
  • Misspelling validation_split
  • Placing validation_split outside model.fit()
3. What will be the size of the validation set if you train a model with 1000 samples and use validation_split=0.25 in model.fit()?
medium
A. 250 samples
B. 750 samples
C. 1000 samples
D. 1250 samples

Solution

  1. Step 1: Calculate validation set size from split fraction

    Validation set size = total samples x validation_split = 1000 x 0.25 = 250 samples.
  2. Step 2: Confirm remaining data is for training

    Remaining 750 samples are used for training, validation set is 250 samples.
  3. Final Answer:

    250 samples -> Option A
  4. Quick Check:

    1000 x 0.25 = 250 [OK]
Hint: Multiply total samples by validation_split fraction [OK]
Common Mistakes:
  • Confusing validation set size with training set size
  • Adding instead of multiplying
  • Using validation_split as count instead of fraction
4. You set validation_split=0.3 in model.fit() but get an error saying the validation data is missing. What is the most likely cause?
medium
A. You forgot to specify the number of epochs
B. The validation_split value must be an integer, not a float
C. The training data is a TensorFlow Dataset, which does not support validation_split
D. The model has no output layer

Solution

  1. Step 1: Understand validation_split limitations

    Validation_split works only with arrays or tensors, not with TensorFlow Dataset objects.
  2. Step 2: Identify cause of error

    If training data is a Dataset, validation_split cannot split it automatically, causing the error.
  3. Final Answer:

    The training data is a TensorFlow Dataset, which does not support validation_split -> Option C
  4. Quick Check:

    Dataset input blocks validation_split [OK]
Hint: validation_split works only with arrays, not Dataset inputs [OK]
Common Mistakes:
  • Using float instead of integer for validation_split
  • Ignoring that Dataset inputs need manual validation sets
  • Assuming epochs affect validation_split
5. You want to train a model on 5000 samples and use 10% for validation. However, your data is shuffled before training. How does validation_split=0.1 behave in this case?
hard
A. It takes the last 10% of the data as validation after shuffling
B. It takes the first 10% of the data as validation before shuffling
C. It randomly selects 10% samples for validation regardless of order
D. It cannot split data if shuffled

Solution

  1. Step 1: Understand validation_split behavior

    Validation_split takes the last fraction of the data as validation set, not random samples.
  2. Step 2: Consider data shuffling effect

    If data is shuffled before calling model.fit(), the last 10% after shuffle is used for validation.
  3. Final Answer:

    It takes the last 10% of the data as validation after shuffling -> Option A
  4. Quick Check:

    Validation split = last fraction after shuffle [OK]
Hint: Validation split uses last fraction of data after shuffle [OK]
Common Mistakes:
  • Thinking validation_split randomly samples validation data
  • Assuming validation_split uses first fraction always
  • Believing validation_split fails if data is shuffled