TensorFlow Keras Model Building 2 — Questions and Answers
Question 1: Which Keras layer is typically used as the final layer for binary classification?
- Dense with sigmoid activation (Correct answer)
- Dense with softmax activation
- Dense with ReLU activation
- Dense with tanh activation
Correct answer: Dense with sigmoid activation
A Dense layer with sigmoid activation outputs a probability between 0 and 1, suitable for binary classification.
Question 2: What loss function should you use for multi-class classification with integer labels in Keras?
- sparse_categorical_crossentropy (Correct answer)
- binary_crossentropy
- categorical_crossentropy
- mean_squared_error
Correct answer: sparse_categorical_crossentropy
sparse_categorical_crossentropy is used when labels are integers rather than one-hot encoded vectors.
Question 3: How do you save a full Keras model including architecture and weights?
- model.save('model.keras') or model.save('model/') (Correct answer)
- model.export('model.json')
- tf.save(model)
- model.write('model.h5')
Correct answer: model.save('model.keras') or model.save('model/')
model.save() serializes the entire model including architecture, weights, and optimizer state.
Question 4: Which callback stops training when a monitored metric stops improving?
- EarlyStopping (Correct answer)
- ReduceLROnPlateau
- ModelCheckpoint
- TerminateOnNaN
Correct answer: EarlyStopping
EarlyStopping monitors a metric and halts training after a specified number of epochs with no improvement.
Question 5: What does the validation_split parameter in model.fit() do?
- Reserves a fraction of training data for validation (Correct answer)
- Splits data into train and test sets
- Validates model architecture
- Shuffles data before training
Correct answer: Reserves a fraction of training data for validation
validation_split takes a float (e.g., 0.2) and reserves that fraction of training data to evaluate loss and metrics each epoch.
Question 6: Which Keras initializer sets weights to small random values drawn from a normal distribution?
- GlorotNormal (Correct answer)
- Zeros
- Ones
- Constant
Correct answer: GlorotNormal
GlorotNormal (Xavier) initializer draws samples from a truncated normal distribution, scaled by the layer's fan-in and fan-out.
Which Keras layer is typically used as the final layer for binary classification?