Bird
Raised Fist0
TensorFlowml~20 mins

Binary classification model in TensorFlow - Practice Problems & Coding Challenges

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
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.

Practice

(1/5)
1. What activation function is commonly used in the output layer of a binary classification model in TensorFlow?
easy
A. Tanh
B. ReLU
C. Softmax
D. Sigmoid

Solution

  1. Step 1: Understand output layer role in binary classification

    The output layer must produce a probability between 0 and 1 to represent two classes.
  2. Step 2: Identify suitable activation function

    Sigmoid activation compresses output to range [0, 1], perfect for binary decisions.
  3. Final Answer:

    Sigmoid -> Option D
  4. Quick Check:

    Binary output needs sigmoid = Sigmoid [OK]
Hint: Binary output needs sigmoid activation [OK]
Common Mistakes:
  • Using softmax for binary output
  • Using ReLU which outputs unbounded values
  • Using tanh which outputs between -1 and 1
2. Which of the following is the correct way to compile a binary classification model in TensorFlow?
easy
A. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
B. model.compile(optimizer='rmsprop', loss='hinge', metrics=['accuracy'])
C. model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['accuracy'])
D. model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Solution

  1. Step 1: Identify appropriate loss for binary classification

    Binary classification requires 'binary_crossentropy' loss to measure error correctly.
  2. Step 2: Check optimizer and metrics

    'adam' optimizer and 'accuracy' metric are standard choices for training and evaluation.
  3. Final Answer:

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) -> Option A
  4. Quick Check:

    Binary loss = binary_crossentropy [OK]
Hint: Use binary_crossentropy loss for binary classification [OK]
Common Mistakes:
  • Using categorical_crossentropy for binary tasks
  • Using mean_squared_error which is for regression
  • Choosing hinge loss which is for SVMs
3. Given the following TensorFlow model code, what will be the shape of the output layer?
model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, activation='relu', input_shape=(5,)),
  tf.keras.layers.Dense(1, activation='sigmoid')
])
medium
A. (None, 1)
B. (None, 10)
C. (5, 1)
D. (1,)

Solution

  1. Step 1: Analyze the last layer configuration

    The last Dense layer has 1 unit and sigmoid activation, so output shape is (batch_size, 1).
  2. Step 2: Understand batch dimension placeholder

    TensorFlow uses None for batch size, so output shape is (None, 1).
  3. Final Answer:

    (None, 1) -> Option A
  4. Quick Check:

    Output units = 1 means shape = (None, 1) [OK]
Hint: Output shape matches last layer units with batch size None [OK]
Common Mistakes:
  • Confusing input shape with output shape
  • Ignoring batch size dimension
  • Assuming output shape is (1,) without batch
4. You trained a binary classification model but the accuracy stays around 50% after many epochs. Which fix is most likely to improve the model?
medium
A. Change the output activation to softmax
B. Use binary_crossentropy loss instead of categorical_crossentropy
C. Increase the batch size to 1024
D. Remove the activation function from the output layer

Solution

  1. Step 1: Identify the cause of poor accuracy

    Using categorical_crossentropy loss with a single sigmoid output causes wrong loss calculation.
  2. Step 2: Apply correct loss function

    Switching to binary_crossentropy aligns loss with sigmoid output for binary classification.
  3. Final Answer:

    Use binary_crossentropy loss instead of categorical_crossentropy -> Option B
  4. Quick Check:

    Loss must match output activation [OK]
Hint: Match loss to output activation for correct training [OK]
Common Mistakes:
  • Using softmax for binary output
  • Removing output activation causing invalid probabilities
  • Assuming batch size alone fixes accuracy
5. You want to build a binary classification model to predict if an email is spam or not. Your dataset has 1000 samples with 20 features each. Which model architecture and compile settings are best?
hard
A. Sequential model with one Dense layer (1 unit, sigmoid), compile with binary_crossentropy and adam
B. Sequential model with one Dense layer (20 units, softmax), compile with categorical_crossentropy and sgd
C. Sequential model with two Dense layers (10 units relu, then 1 unit sigmoid), compile with binary_crossentropy and adam
D. Sequential model with three Dense layers (64 relu, 32 relu, 1 tanh), compile with mean_squared_error and rmsprop

Solution

  1. Step 1: Choose model complexity for dataset size

    Two layers with relu then sigmoid balance learning capacity and binary output.
  2. Step 2: Select correct loss and optimizer

    Binary_crossentropy fits binary tasks; adam optimizer adapts well for small datasets.
  3. Final Answer:

    Sequential model with two Dense layers (10 units relu, then 1 unit sigmoid), compile with binary_crossentropy and adam -> Option C
  4. Quick Check:

    Two layers + sigmoid + binary_crossentropy = Best practice [OK]
Hint: Use relu hidden layers + sigmoid output + binary_crossentropy [OK]
Common Mistakes:
  • Using softmax for binary classification
  • Using tanh output activation
  • Using mean_squared_error loss for classification