Building Keras Models
- 28Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Describe the overall training flow of a neural network, from input to weight update.
28 questions
- 1
Describe the overall training flow of a neural network, from input to weight update.
Model answer
The input goes through network layers to produce predictions. The predictions and true values are fed into a loss function to compute a loss score. The optimizer calculates the gradient of the loss with respect to parameters and updates the weights.
Explanation
This is the fundamental loop: forward pass, loss computation, backward pass (gradient), and weight update.
- 2
What is the key difference between gradient descent and stochastic gradient descent (SGD)?
Model answer
Gradient descent uses all data points in each iteration to compute the gradient, while SGD uses a small random subset (a batch).
Explanation
SGD updates weights more frequently, making it faster and often leading to better convergence.
- 3
Define an epoch in neural network training.
Model answer
An epoch is one full pass through the entire training data.
Explanation
One epoch means every training sample has been seen once by the network.
- 4
In SGD, when is a weight update performed?
Model answer
A weight update is performed after processing each batch (minibatch) of data.
Explanation
Each batch yields a gradient estimate, and weights are updated immediately, leading to multiple updates per epoch.
- 5
If you have 194 training samples and a batch size of 32, how many batches are there in one epoch?
Model answer
7 batches: 6 batches of 32 samples and 1 final batch of 2 samples.
Explanation
Number of batches = ceil(training size / batch size) = ceil(194/32) = 7.
- 6
Why are batch sizes often set to 32 or 64?
Model answer
Because these sizes align well with the parallel processing capabilities of modern hardware (GPUs).
Explanation
GPUs are optimized for certain matrix sizes; powers of two are common.
- 7
What is overfitting and how does it affect validation error?
Model answer
Overfitting occurs when the model fits the idiosyncrasies of the training data instead of general patterns, causing validation error to stop decreasing and eventually increase.
Explanation
Overfitting leads to poor generalization on unseen data.
- 8
What is early stopping and how does it prevent overfitting?
Model answer
Early stopping monitors validation loss during training; when validation loss stops improving (or starts to rise), training is halted before overfitting worsens.
Explanation
It is a simple regularization method that saves training time and improves generalization.
- 9
What is the rank of a tensor? Give examples of tensors with rank 0, 1, and 2.
Model answer
Rank is the number of dimensions. Rank 0: scalar (a single number). Rank 1: vector (list). Rank 2: matrix (table).
Explanation
Higher-rank tensors are generalizations to more dimensions.
- 10
How can you construct a higher-rank tensor from lower-rank tensors?
Model answer
A list of rank-n tensors forms a rank-(n+1) tensor. For example, a list of videos (each rank 4) gives a rank 5 tensor.
Explanation
Tensors can be nested; the first dimension indexes the list.
- 11
List two key features of TensorFlow that simplify neural network training.
Model answer
Automatic gradient calculation and built-in optimizers (like SGD and Adam).
Explanation
TensorFlow also handles distributed computing and GPU acceleration automatically.
- 12
What is Keras and how does it relate to TensorFlow?
Model answer
Keras is a high-level API that sits on top of TensorFlow, providing convenient building blocks like layers, activation functions, and preprocessing tools.
Explanation
It simplifies model building and training, making deep learning more accessible.
- 13
Why must categorical variables be one-hot encoded before feeding into a neural network?
Model answer
Neural networks can only handle numeric inputs; one-hot encoding converts categorical variables into a binary numeric format.
Explanation
Each category becomes a separate column with a 0 or 1.
- 14
Why should numerical variables be standardized (mean 0, std 1) before training a neural network?
Model answer
Neural networks work best when inputs are in a small, consistent range; standardization helps avoid large variations that can hinder training.
Explanation
It improves convergence and stability of gradient-based optimization.
- 15
When preparing data for a neural network, why is it important to split into training and test sets before standardizing numerical variables?
Model answer
To prevent the test set from being influenced by the training set; standardization should be based only on training data to keep the test set truly independent.
Explanation
If you standardize before splitting, the test set statistics are contaminated by training data.
- 16
In the heart disease classification model, what are the input size, hidden layer size, and output activation?
Model answer
Input size: 29 features. Hidden layer: 16 neurons with ReLU activation. Output: 1 neuron with sigmoid activation.
Explanation
The model had 29 inputs, one hidden dense layer, and a sigmoid output for binary classification.
- 17
What loss function is appropriate for a binary classification problem with a sigmoid output?
Model answer
Binary cross-entropy.
Explanation
It measures the difference between predicted probabilities and true binary labels.
- 18
What optimizer is commonly used as a default for training neural networks, and why?
Model answer
Adam (Adaptive Moment Estimation) is a popular default because it works well across many problems.
Explanation
Adam combines momentum and adaptive learning rates, often outperforming basic SGD.
- 19
What is the purpose of the compile step in Keras?
Model answer
It reorganizes the model for efficient parallel and distributed computation, setting up the optimizer, loss, and metrics.
Explanation
Compilation does not train the model; it prepares it for training.
- 20
In the heart disease example, what was the baseline accuracy and why?
Model answer
72.6% by always predicting the majority class (0, no heart disease), since 72.6% of patients were negative.
Explanation
Any model must beat this baseline to be useful.
- 21
How is a grayscale image represented as a tensor?
Model answer
As a 2D tensor (matrix) of pixel intensity values ranging from 0 (black) to 255 (white).
Explanation
Each pixel's lightness is stored in a single number.
- 22
How is a color image represented as a tensor?
Model answer
As a 3D tensor with three channels (Red, Green, Blue), each channel being a 2D array of values from 0 to 255.
Explanation
The combination of RGB intensities produces any color. This tensor has rank 3 (height, width, channels).
- 23
What is the difference between image classification and object detection?
Model answer
Image classification assigns a single label to the entire image. Object detection identifies multiple objects and localizes each one with a bounding box.
Explanation
Object detection combines classification with localization.
- 24
What is semantic segmentation?
Model answer
Semantic segmentation classifies every pixel in an image into a category (e.g., road, sheep, grass), producing a pixel-level label map.
Explanation
It assigns a class to each pixel, but does not distinguish between instances of the same class.
- 25
In the fashion-MNIST example, why is the input image flattened before feeding into dense layers?
Model answer
Dense layers expect a 1D vector input, so the 2D image (28x28) is flattened into a 784-element vector.
Explanation
Flattening rearranges the pixel values into a single list without changing the data.
- 26
What is the softmax function and why is it used for multi-class classification with mutually exclusive classes?
Model answer
Softmax converts a vector of arbitrary numbers into probabilities that sum to 1 by exponentiating and normalizing. It is used when only one class is correct.
Explanation
The output probabilities sum to 1, making it suitable for single-label classification.
- 27
What loss function should you use if your target labels are integers (e.g., 0-9) representing classes?
Model answer
Sparse categorical cross-entropy.
Explanation
It is equivalent to categorical cross-entropy but avoids one-hot encoding the labels.
- 28
In the fashion-MNIST example, what is the shape of the input tensor (training data)?
Model answer
(60000, 28, 28) – 60,000 images, each 28x28 pixels, making it a rank-3 tensor.
Explanation
The first dimension indexes the samples, the next two are height and width.

Ask anything
We've read this whole study set, so questions about it get the best answers.