Bird
Raised Fist0
TensorFlowml~20 mins

Model training component in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Model Training Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of the training accuracy after 1 epoch?
Consider this TensorFlow model training snippet. What will be the printed training accuracy after 1 epoch?
TensorFlow
import tensorflow as tf
import numpy as np

# Create dummy data
x_train = np.random.random((100, 10))
y_train = np.random.randint(0, 2, 100)

# Build simple model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(5, activation='relu', input_shape=(10,)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=1, batch_size=10, verbose=0)

print(f"Training accuracy: {history.history['accuracy'][0]:.2f}")
ATraining accuracy: 0.00
BTraining accuracy: 1.00
CTraining accuracy: 0.50
DTraining accuracy: 0.75
Attempts:
2 left
💡 Hint
The model is untrained and data is random, so accuracy will be close to random guessing.
❓ Model Choice
intermediate
1:30remaining
Which optimizer is best for fast convergence on a small dataset?
You want to train a small neural network on a small dataset quickly. Which optimizer should you choose for faster convergence?
AAdam optimizer
BSGD (Stochastic Gradient Descent) without momentum
CRMSprop optimizer
DAdagrad optimizer
Attempts:
2 left
💡 Hint
Think about which optimizer adapts learning rates and works well with small datasets.
❓ Hyperparameter
advanced
1:30remaining
What happens if you set batch_size larger than training data size?
In TensorFlow model training, what is the effect of setting batch_size larger than the total number of training samples?
AThe entire dataset is used as one batch in each epoch
BTraining will run with multiple smaller batches ignoring the batch_size
CThe batch size will be automatically reduced to the dataset size
DTraining will fail with an error about batch size
Attempts:
2 left
💡 Hint
Think about how batch size controls how many samples are processed before updating weights.
❓ Metrics
advanced
2:00remaining
Which metric is best for imbalanced binary classification?
You train a binary classifier on a dataset where one class is much rarer than the other. Which metric should you monitor during training to better evaluate model performance?
AAccuracy
BPrecision
CRecall
DF1-score
Attempts:
2 left
💡 Hint
Consider a metric that balances precision and recall for imbalanced data.
🔧 Debug
expert
3:00remaining
Why does this TensorFlow training code raise a ValueError?
Examine the code below. Why does it raise a ValueError during model.fit()?
TensorFlow
import tensorflow as tf
import numpy as np

x_train = np.random.random((50, 20))
y_train = np.random.randint(0, 3, 50)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
AInput shape does not match x_train shape
BOutput layer has 1 unit but softmax requires units equal to number of classes
CLoss function sparse_categorical_crossentropy is incompatible with integer labels
DOptimizer 'adam' is not supported with sparse_categorical_crossentropy
Attempts:
2 left
💡 Hint
Check the output layer units and activation for multi-class classification.

Practice

(1/5)
1. What is the main purpose of the model.fit function in TensorFlow?
easy
A. To make predictions on new data
B. To evaluate the model's performance on test data
C. To save the model to disk
D. To train the model on the given data

Solution

  1. Step 1: Understand the role of model.fit

    model.fit is used to train the model by adjusting its weights based on the training data.

  2. Step 2: Differentiate from other functions

    Functions like model.evaluate check performance, model.save saves the model, and model.predict makes predictions. These are not for training.

  3. Final Answer:

    To train the model on the given data -> Option D
  4. Quick Check:

    model.fit = train model [OK]
Hint: Remember: fit means training the model [OK]
Common Mistakes:
  • Confusing fit with predict
  • Thinking fit saves the model
  • Mixing up training and evaluation functions
2. Which of the following is the correct way to set the number of training cycles (epochs) in model.fit?
easy
A. model.fit(x_train, y_train, epoch=10)
B. model.fit(x_train, y_train, epochs=10)
C. model.fit(x_train, y_train, n_epochs=10)
D. model.fit(x_train, y_train, cycles=10)

Solution

  1. Step 1: Recall the correct parameter name

    The parameter to set training cycles is epochs, plural.
  2. Step 2: Check the options for correct syntax

    Only model.fit(x_train, y_train, epochs=10) uses epochs=10 correctly. Others use incorrect parameter names.
  3. Final Answer:

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

    Use epochs to set training cycles [OK]
Hint: Use 'epochs' plural to set training rounds [OK]
Common Mistakes:
  • Using 'epoch' instead of 'epochs'
  • Using 'n_epochs' or 'cycles' which are invalid
  • Misspelling the parameter name
3. What will be the output of the following code snippet?
import tensorflow as tf
import numpy as np

x_train = np.array([[1], [2], [3], [4]])
y_train = np.array([2, 4, 6, 8])

model = tf.keras.Sequential([
  tf.keras.layers.Dense(1, input_shape=(1,))
])

model.compile(optimizer='sgd', loss='mse')
history = model.fit(x_train, y_train, epochs=2, verbose=0)

print(history.history['loss'])
medium
A. A list of two loss values decreasing over epochs
B. An error because of missing metrics
C. [0.5, 0.25]
D. Empty list []

Solution

  1. Step 1: Understand the code training process

    The model is a simple linear layer trained with mean squared error loss for 2 epochs on a linear dataset.
  2. Step 2: Predict the output of history.history['loss']

    This returns a list of loss values for each epoch, which should decrease as the model learns.
  3. Final Answer:

    A list of two loss values decreasing over epochs -> Option A
  4. Quick Check:

    Loss list decreases over epochs [OK]
Hint: Training loss list shows progress per epoch [OK]
Common Mistakes:
  • Expecting exact loss numbers without running
  • Thinking metrics are required for loss output
  • Assuming loss list is empty or error
4. Identify the error in this TensorFlow training code:
model = tf.keras.Sequential([
  tf.keras.layers.Dense(1, input_shape=(1,))
])

model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epoch=5)
medium
A. The parameter 'epoch' should be 'epochs' in model.fit
B. Missing optimizer in model.compile
C. Input shape should be (None, 1) instead of (1,)
D. Loss function 'mse' is invalid

Solution

  1. Step 1: Check the parameters of model.fit

    The parameter to set training cycles is epochs, not epoch.
  2. Step 2: Verify other parts of the code

    Optimizer and loss are correctly set. Input shape (1,) is valid for single feature input.
  3. Final Answer:

    The parameter 'epoch' should be 'epochs' in model.fit -> Option A
  4. Quick Check:

    Use 'epochs' plural in model.fit [OK]
Hint: Check spelling of 'epochs' parameter carefully [OK]
Common Mistakes:
  • Using 'epoch' instead of 'epochs'
  • Assuming input_shape is wrong for single feature
  • Thinking 'mse' is invalid loss
5. You want to train a TensorFlow model on a dataset but notice the training loss does not decrease after many epochs. Which of the following is the best next step to improve training?
hard
A. Remove the loss function from model.compile
B. Increase the number of epochs to 1000 without changing anything else
C. Change the optimizer to a different one like 'adam' and check learning rate
D. Use model.predict instead of model.fit

Solution

  1. Step 1: Understand why loss might not decrease

    Loss not decreasing often means the optimizer or learning rate is not suitable for the problem.
  2. Step 2: Choose the best corrective action

    Changing to a better optimizer like 'adam' and tuning learning rate helps the model learn better. Increasing epochs alone won't help if learning is stuck.
  3. Final Answer:

    Change the optimizer to a different one like 'adam' and check learning rate -> Option C
  4. Quick Check:

    Adjust optimizer and learning rate to improve training [OK]
Hint: If loss stuck, try different optimizer and tune learning rate [OK]
Common Mistakes:
  • Just increasing epochs without fixing optimizer
  • Removing loss function causes errors
  • Using predict instead of fit for training
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