Building Keras Models
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary difference between gradient descent and stochastic gradient descent?
What is the primary difference between gradient descent and stochastic gradient descent?
44 questions
- 1
What is the primary difference between gradient descent and stochastic gradient descent?
- A
Gradient descent requires a fixed learning rate, while stochastic gradient descent adapts it automatically.
- B
Gradient descent updates weights after each data point, while stochastic gradient descent updates after each epoch.
- C
Gradient descent uses a small batch per update, while stochastic gradient descent uses all data points.
- D
Gradient descent uses all data points per weight update, while stochastic gradient descent uses a small batch.
Correct answer
DGradient descent uses all data points per weight update, while stochastic gradient descent uses a small batch.
Explanation
Gradient descent computes the gradient using the entire dataset before updating weights, while stochastic gradient descent (SGD) uses a random subset (minibatch) for each update, making it more efficient and allowing more frequent updates.
- A
- 2
In stochastic gradient descent, how many weight updates occur per epoch?
- A
Exactly one update per epoch.
- B
The number of updates equals the number of epochs multiplied by the batch size.
- C
The number of updates equals the number of batches in the epoch.
- D
The number of updates equals the number of data points in the training set.
Correct answer
CThe number of updates equals the number of batches in the epoch.
Explanation
In SGD, each batch triggers a weight update, so the number of updates per epoch equals the number of batches (training set size divided by batch size, rounded up).
- A
- 3
What is an epoch in the context of training a neural network?
- A
A single forward and backward pass for one data point.
- B
The total number of training iterations performed.
- C
A single weight update using the gradient from one batch.
- D
One complete pass through the entire training dataset.
Correct answer
DOne complete pass through the entire training dataset.
Explanation
An epoch means that every training sample has been processed once through the network. In gradient descent, one epoch corresponds to one weight update; in SGD, it corresponds to as many updates as there are batches.
- A
- 4
What is a commonly recommended default batch size for stochastic gradient descent?
- A
128
- B
512
- C
32
- D
256
Correct answer
C32
Explanation
The lecture states that 32 is a good default batch size, and it aligns well with parallel hardware. Other common sizes include 64 and 128.
- A
- 5
As model complexity increases, what typically happens to the error on the training data?
- A
It decreases initially, then increases.
- B
It increases monotonically.
- C
It remains unchanged.
- D
It decreases monotonically.
Correct answer
DIt decreases monotonically.
Explanation
Training error generally decreases as the model becomes more complex because the model can fit the training data better. Overfitting causes validation error to increase, but training error continues to drop.
- A
- 6
What is overfitting in machine learning?
- A
When the training error increases as the model becomes more complex.
- B
When the model fits the training data too well and performs poorly on new data.
- C
When the model achieves high accuracy on both training and validation sets.
- D
When the model is too simple and fails to capture patterns in the training data.
Correct answer
BWhen the model fits the training data too well and performs poorly on new data.
Explanation
Overfitting occurs when the model learns idiosyncrasies of the training data rather than general patterns, causing validation error to rise while training error continues to drop.
- A
- 7
What is underfitting?
- A
When the model requires more epochs to converge.
- B
When the model is too simple and has high training error.
- C
When the model has too many parameters and overfits the data.
- D
When the model achieves low training error but high validation error.
Correct answer
BWhen the model is too simple and has high training error.
Explanation
Underfitting means the model is not complex enough to capture the underlying patterns, resulting in high error on the training data. It corresponds to the left side of the model complexity curve.
- A
- 8
What is early stopping as a regularization technique?
- A
Training is stopped when the batch size becomes too large.
- B
Training is stopped when the training loss reaches zero.
- C
Training is stopped when the validation loss stops improving to prevent overfitting.
- D
Training is stopped after a fixed number of epochs regardless of performance.
Correct answer
CTraining is stopped when the validation loss stops improving to prevent overfitting.
Explanation
Early stopping monitors the validation loss during training and halts the process when the loss begins to increase (or stops decreasing), thereby preventing overfitting. Geoff Hinton called it a 'beautiful free lunch'.
- A
- 9
Who famously referred to early stopping as a 'beautiful free lunch'?
- A
Andrew Ng
- B
Geoff Hinton
- C
Yann LeCun
- D
François Chollet
Correct answer
BGeoff Hinton
Explanation
Geoff Hinton, one of the pioneers of deep learning, used this phrase to describe early stopping because it is a simple and effective regularization method that requires no additional computational cost.
- A
- 10
What is dropout?
- A
A technique to drop the learning rate when validation loss plateaus.
- B
A regularization technique that randomly drops neurons during training to prevent overfitting.
- C
A method to drop unimportant features from the input data.
- D
A way to drop gradients that are too large.
Correct answer
BA regularization technique that randomly drops neurons during training to prevent overfitting.
Explanation
Dropout is a regularization method where randomly selected neurons are ignored (dropped out) during training, which forces the network to learn more robust features. It was mentioned as a topic for the next lecture.
- A
- 11
What is the rank of a scalar?
- A
3
- B
2
- C
0
- D
1
Correct answer
C0
Explanation
A scalar is a single number with no dimensions, so its tensor rank is 0. A vector has rank 1, a matrix has rank 2, and higher-dimensional arrays have higher ranks.
- A
- 12
What is the rank of a vector?
- A
1
- B
2
- C
0
- D
3
Correct answer
A1
Explanation
A vector is a one-dimensional list of numbers, so its tensor rank is 1. Rank corresponds to the number of dimensions or axes.
- A
- 13
What is the rank of a matrix?
- A
2
- B
0
- C
1
- D
3
Correct answer
A2
Explanation
A matrix is a two-dimensional table of numbers, so its tensor rank is 2. It has rows and columns as its two axes.
- A
- 14
What is the rank of a grayscale image represented as a grid of pixel intensities?
- A
1
- B
2
- C
0
- D
3
Correct answer
B2
Explanation
A grayscale image is a 2D array of pixel values (height and width), so it is a rank-2 tensor. Each value represents the intensity at a specific location.
- A
- 15
What is the rank of a color image?
- A
2
- B
3
- C
1
- D
0
Correct answer
B3
Explanation
A color image has three channels (red, green, blue) in addition to height and width, making it a rank-3 tensor (height, width, channels).
- A
- 16
What is the rank of a video?
- A
4
- B
3
- C
2
- D
5
Correct answer
A4
Explanation
A video is a sequence of color images, so it has an additional time dimension (frames). Thus, it is a rank-4 tensor: frames, height, width, channels.
- A
- 17
What is a tensor?
- A
A generalization of scalars, vectors, and matrices to higher dimensions.
- B
A special type of matrix used only in deep learning.
- C
A mathematical object that always has exactly three dimensions.
- D
A data structure that stores only floating-point numbers.
Correct answer
AA generalization of scalars, vectors, and matrices to higher dimensions.
Explanation
Tensors generalize the concept of numbers (scalars), lists (vectors), and tables (matrices) to arbitrary numbers of dimensions. They are the fundamental data structure in TensorFlow.
- A
- 18
What does TensorFlow provide that makes it useful for neural network training?
- A
Automatic gradient computation, built-in optimizers, and distribution across hardware.
- B
Pre-trained models and a graphical user interface for designing networks.
- C
A programming language specifically for deep learning.
- D
Automated feature selection and data cleaning.
Correct answer
AAutomatic gradient computation, built-in optimizers, and distribution across hardware.
Explanation
TensorFlow automatically calculates gradients, provides various optimizers (like SGD and Adam), and handles distribution across multiple servers, GPUs, and TPUs, allowing users to focus on model design.
- A
- 19
What is Keras?
- A
A high-level API on top of TensorFlow that provides convenience features for building and training models.
- B
A collection of pre-trained models only.
- C
A library for data preprocessing and visualization.
- D
A standalone deep learning framework that does not use TensorFlow.
Correct answer
AA high-level API on top of TensorFlow that provides convenience features for building and training models.
Explanation
Keras sits on top of TensorFlow and offers user-friendly abstractions like layers, activation functions, and training routines. It is the recommended high-level API for TensorFlow.
- A
- 20
What are the three Keras APIs mentioned in the lecture?
- A
Input, Hidden, and Output.
- B
Dense, Dropout, and Flatten.
- C
Linear, Convolutional, and Recurrent.
- D
Sequential, Functional, and Subclassing.
Correct answer
DSequential, Functional, and Subclassing.
Explanation
Keras provides three ways to define models: the Sequential API (simple stack of layers), the Functional API (more flexible, allows complex topologies), and the Subclassing API (fully custom via Python classes). The course primarily uses the Functional API.
- A
- 21
What does the compile step in Keras do?
- A
It converts the model into a format that can be run on mobile devices.
- B
It splits the data into training and validation sets.
- C
It reorganizes the model for efficient parallel execution and distribution.
- D
It initializes the weights of the model randomly.
Correct answer
CIt reorganizes the model for efficient parallel execution and distribution.
Explanation
The compile step prepares the model for training by setting up the optimizer, loss function, and metrics, and reorganizing the computational graph for parallel hardware and distributed computing.
- A
- 22
What does the model.fit function return in Keras?
- A
A History object containing training and validation metrics per epoch.
- B
A dictionary of the model's architecture.
- C
The accuracy on the test set.
- D
The final trained model with optimal weights.
Correct answer
AA History object containing training and validation metrics per epoch.
Explanation
model.fit returns a History object that records the loss and metric values for each epoch on the training and validation sets. This can be used for plotting and diagnostics.
- A
- 23
What is the purpose of the model.evaluate function in Keras?
- A
To compute the gradient of the loss function with respect to the weights.
- B
To plot the training and validation loss curves.
- C
To save the model's weights to a file.
- D
To evaluate the model on a test dataset and return loss and metric values.
Correct answer
DTo evaluate the model on a test dataset and return loss and metric values.
Explanation
model.evaluate uses a separate test set (held out during training) to compute the final performance metrics, giving an unbiased estimate of how the model will perform in the real world.
- A
- 24
Why must categorical variables be one-hot encoded before being fed into a neural network?
- A
Because one-hot encoding ensures that all features have the same scale.
- B
Because one-hot encoding reduces the dimensionality of the data.
- C
Because neural networks can only process numerical inputs, and one-hot encoding converts categories to numeric form.
- D
Because categorical variables are already numerical and do not need encoding.
Correct answer
CBecause neural networks can only process numerical inputs, and one-hot encoding converts categories to numeric form.
Explanation
Neural networks perform mathematical operations on numbers, so categorical data must be converted to a numeric representation. One-hot encoding creates binary columns for each category, which is a standard approach.
- A
- 25
Why should numeric variables be standardized (e.g., subtract mean, divide by standard deviation) before training a neural network?
- A
To convert categorical variables into numeric form.
- B
To keep all numeric inputs within a small range, which helps the network converge faster.
- C
To increase the variance of the data.
- D
To reduce the number of features in the dataset.
Correct answer
BTo keep all numeric inputs within a small range, which helps the network converge faster.
Explanation
Neural networks work best when inputs are on a similar scale; standardization (z-scoring) ensures that no variable dominates the gradient updates, leading to more stable and faster training.
- A
- 26
Why is it important to split the data into training and test sets before performing standardization?
- A
To prevent information from the test set from influencing the preprocessing parameters, which would lead to data leakage.
- B
To ensure that both sets have the same distribution after standardization.
- C
Because standardization requires knowing the test set mean and standard deviation.
- D
Because the test set should be standardized separately from the training set.
Correct answer
ATo prevent information from the test set from influencing the preprocessing parameters, which would lead to data leakage.
Explanation
If standardization uses test set statistics, the test set is no longer independent; the preprocessing becomes part of the model building, and performance estimates become overly optimistic. The test set should only be used for final evaluation.
- A
- 27
What does the Pandas get_dummies function do?
- A
It standardizes numeric variables to have mean 0 and standard deviation 1.
- B
It splits the data into training and test sets.
- C
It performs one-hot encoding on categorical variables.
- D
It converts a DataFrame into a NumPy array.
Correct answer
CIt performs one-hot encoding on categorical variables.
Explanation
get_dummies converts categorical variable(s) into dummy/indicator variables (one-hot encoding). It is used to prepare categorical data for machine learning models that require numerical input.
- A
- 28
What loss function is used for binary classification with a sigmoid output?
- A
Binary cross-entropy.
- B
Mean squared error.
- C
Categorical cross-entropy.
- D
Sparse categorical cross-entropy.
Correct answer
ABinary cross-entropy.
Explanation
For binary classification where the output is a single probability (sigmoid), binary cross-entropy is the appropriate loss function. It measures the discrepancy between the predicted probability and the true binary label.
- A
- 29
What loss function is used for multi-class classification when labels are one-hot encoded?
- A
Mean squared error.
- B
Categorical cross-entropy.
- C
Binary cross-entropy.
- D
Sparse categorical cross-entropy.
Correct answer
BCategorical cross-entropy.
Explanation
Categorical cross-entropy is used when the target labels are one-hot encoded vectors. It generalizes binary cross-entropy to multiple classes and compares the predicted probability distribution with the true distribution.
- A
- 30
What loss function is used for multi-class classification when labels are integers (sparse encoding)?
- A
Sparse categorical cross-entropy.
- B
Binary cross-entropy.
- C
Mean squared error.
- D
Categorical cross-entropy.
Correct answer
ASparse categorical cross-entropy.
Explanation
Sparse categorical cross-entropy is equivalent to categorical cross-entropy but accepts integer labels (e.g., 0, 1, 2, ...) instead of one-hot vectors. It is computationally more efficient for large numbers of classes.
- A
- 31
Why cannot multiple sigmoid activations be used for multi-class classification with mutually exclusive classes?
- A
Because multiple sigmoids require more parameters.
- B
Because the probabilities would not sum to 1.
- C
Because sigmoid cannot output values between 0 and 1.
- D
Because sigmoid outputs are not differentiable.
Correct answer
BBecause the probabilities would not sum to 1.
Explanation
Sigmoid outputs are independent probabilities that do not necessarily sum to 1. For mutually exclusive classes, a single correct answer must have probability 1, and the others 0, which requires the softmax function to enforce that the outputs sum to 1.
- A
- 32
What is the purpose of the softmax function?
- A
To squash a single real number into the range (0, 1).
- B
To normalize the inputs to a layer.
- C
To convert a vector of arbitrary real numbers into probabilities that sum to 1.
- D
To introduce non-linearity into a neural network.
Correct answer
CTo convert a vector of arbitrary real numbers into probabilities that sum to 1.
Explanation
Softmax takes a vector of logits (unbounded values) and applies the exponential function to each element, then divides by the sum of exponentials. This yields a probability distribution that sums to 1, making it suitable for multi-class classification.
- A
- 33
What are the possible integer values for pixel intensity in a grayscale image?
- A
0 to 255
- B
-1 to 1
- C
0 to 1
- D
0 to 100
Correct answer
A0 to 255
Explanation
Pixel intensity in a grayscale image is typically represented as an integer from 0 (black, no light) to 255 (white, maximum light intensity). This is a common 8-bit representation.
- A
- 34
How are color images represented digitally?
- A
Using a single channel with values from 0 to 255 that encode color.
- B
Using three channels: red, green, and blue, each with values from 0 to 255.
- C
Using four channels: cyan, magenta, yellow, and black.
- D
Using a 3D vector for each pixel with coordinates in color space.
Correct answer
BUsing three channels: red, green, and blue, each with values from 0 to 255.
Explanation
Color images are represented by three separate tables (channels) for red, green, and blue light intensities, each ranging from 0 to 255. Mixing these primary colors produces the full color spectrum.
- A
- 35
What is semantic segmentation in computer vision?
- A
Classifying every pixel in an image into a category.
- B
Identifying individual instances of objects and segmenting each.
- C
Classifying the entire image into one of several categories.
- D
Detecting and localizing objects in an image with bounding boxes.
Correct answer
AClassifying every pixel in an image into a category.
Explanation
Semantic segmentation assigns a class label to each pixel in the image, effectively partitioning the image into regions corresponding to different object categories (e.g., road, grass, sheep).
- A
- 36
What is object detection?
- A
Classifying the whole image into a single category.
- B
Estimating the pose of a single object in an image.
- C
Segmenting every pixel into object categories.
- D
Identifying and localizing multiple objects in an image with bounding boxes.
Correct answer
DIdentifying and localizing multiple objects in an image with bounding boxes.
Explanation
Object detection involves both recognizing objects (classification) and determining their locations (often via bounding boxes). It is a key task in applications like self-driving cars, where multiple objects must be detected simultaneously.
- A
- 37
What is instance segmentation?
- A
Segmenting an image into uniform regions based on color.
- B
Detecting objects with bounding boxes only.
- C
Combining semantic segmentation with distinguishing different objects of the same class.
- D
Classifying each pixel into a category without distinguishing between objects.
Correct answer
CCombining semantic segmentation with distinguishing different objects of the same class.
Explanation
Instance segmentation goes a step beyond semantic segmentation by not only labeling each pixel with a class but also differentiating individual instances of the same class (e.g., separating sheep 1, sheep 2, etc.).
- A
- 38
What are the dimensions of each image in the Fashion-MNIST dataset?
- A
32 pixels by 32 pixels.
- B
28 pixels by 28 pixels.
- C
224 pixels by 224 pixels.
- D
64 pixels by 64 pixels.
Correct answer
B28 pixels by 28 pixels.
Explanation
Fashion-MNIST consists of 28x28 grayscale images, which is the same size as the original MNIST digit dataset. This small size allows for fast experimentation.
- A
- 39
How many classes are there in the Fashion-MNIST dataset?
- A
1000
- B
26
- C
10
- D
100
Correct answer
C10
Explanation
Fashion-MNIST has 10 classes of clothing items (e.g., T-shirt, trouser, boot). The dataset is balanced, with each class having 10% of the total images.
- A
- 40
What is the baseline accuracy for a naive model on Fashion-MNIST?
- A
50%
- B
90%
- C
10%
- D
72.6%
Correct answer
C10%
Explanation
Since the dataset is balanced across 10 classes, a model that always predicts the most frequent class (or random guessing) would achieve roughly 10% accuracy. Any useful model must exceed this baseline.
- A
- 41
What is flattening in the context of image inputs?
- A
Converting a 2D image matrix into a 1D vector by concatenating rows.
- B
Resizing the image to a smaller resolution.
- C
Converting a color image to grayscale.
- D
Normalizing pixel values to a small range like [0,1].
Correct answer
AConverting a 2D image matrix into a 1D vector by concatenating rows.
Explanation
Flattening takes a 2D array (e.g., 28x28) and rearranges it into a single long vector (784 elements in this case). This allows the image to be fed into a standard dense layer.
- A
- 42
After flattening a 28x28 image, how many input features are there?
- A
56
- B
28
- C
784
- D
256
Correct answer
C784
Explanation
28 multiplied by 28 equals 784, so the flattened vector has 784 elements. This becomes the number of input neurons in the first hidden layer.
- A
- 43
What activation function is typically used for hidden layers in neural networks?
- A
Sigmoid.
- B
ReLU (Rectified Linear Unit).
- C
Tanh.
- D
Softmax.
Correct answer
BReLU (Rectified Linear Unit).
Explanation
ReLU is the default activation for hidden layers in modern neural networks because it helps mitigate the vanishing gradient problem and is computationally efficient. It outputs zero for negative inputs and the input itself for positive values.
- A
- 44
How many neurons were used in the hidden layer of the Fashion-MNIST model built in the lecture?
- A
784
- B
256
- C
512
- D
128
Correct answer
B256
Explanation
The lecture used a hidden layer with 256 ReLU neurons, which was found to perform well. The choice was based on experimentation.
- A

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