0
0
TensorFlowml~20 mins

Binary classification model in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Classification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple binary classification model training
Consider the following TensorFlow code that trains a binary classification model on dummy data. What will be the printed training accuracy after 5 epochs?
TensorFlow
import tensorflow as tf
import numpy as np

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

# Build a simple model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(8, 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=5, batch_size=10, verbose=0)

print(f"Training accuracy after 5 epochs: {history.history['accuracy'][-1]:.2f}")
ATraining accuracy after 5 epochs: 0.50
BTraining accuracy after 5 epochs: 0.90
CTraining accuracy after 5 epochs: 0.10
DTraining accuracy after 5 epochs: 1.00
Attempts:
2 left
💡 Hint
The data is random and labels are random, so the model cannot learn meaningful patterns.
Model Choice
intermediate
1:30remaining
Choosing the correct output layer for binary classification
Which output layer configuration is correct for a binary classification model in TensorFlow?
Atf.keras.layers.Dense(2, activation='softmax')
Btf.keras.layers.Dense(1, activation='sigmoid')
Ctf.keras.layers.Dense(1, activation='softmax')
Dtf.keras.layers.Dense(2, activation='sigmoid')
Attempts:
2 left
💡 Hint
Binary classification usually uses one output neuron with a sigmoid activation.
Hyperparameter
advanced
1:30remaining
Best loss function for binary classification
Which loss function is most appropriate when training a binary classification model with a sigmoid output in TensorFlow?
Atf.keras.losses.BinaryCrossentropy()
Btf.keras.losses.CategoricalCrossentropy()
Ctf.keras.losses.MeanSquaredError()
Dtf.keras.losses.SparseCategoricalCrossentropy()
Attempts:
2 left
💡 Hint
Binary classification with sigmoid output requires a loss that handles probabilities for two classes.
Metrics
advanced
1:30remaining
Interpreting model accuracy on imbalanced binary data
A binary classification model is trained on a dataset where 95% of samples belong to class 0 and 5% to class 1. The model always predicts class 0. What accuracy will the model achieve?
A0.50
B0.05
C1.00
D0.95
Attempts:
2 left
💡 Hint
Accuracy counts how many predictions match true labels.
🔧 Debug
expert
2:00remaining
Identifying the error in binary classification model compilation
What error will this TensorFlow code raise when compiling a binary classification model?
TensorFlow
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
ARuntimeError: Activation function 'sigmoid' not supported with categorical_crossentropy.
BTypeError: optimizer argument must be a string or tf.keras.optimizers.Optimizer instance.
CValueError: You are passing a target array of shape (None, 1) while using categorical_crossentropy loss. Expected shape is (None, num_classes).
DNo error, code compiles successfully.
Attempts:
2 left
💡 Hint
Check if the loss function matches the output layer and label format.