Deep Learning Solution
Deep Learning Solution
Beginner 🔠
Answer:
Deep learning is a branch of Artificial Intelligence that uses algorithms inspired by the
human brain's structure, enabling computers to learn from large amounts of data and
make intelligent decisions.
It is a subfield of statistical machine learning. The key difference is that deep learning
models build layers one after another, using outputs from preceding layers as inputs,
allowing them to learn features automatically, whereas most traditional machine
learning algorithms are "shallow" and use features directly from the training data.
Answer:
An artificial neuron is the basic unit in a neural network, inspired by biological neurons.
It computes a weighted sum of its inputs plus a bias and then applies an activation
function.
Answer:
Activation functions are critical components that introduce non-linearity into the
neural network model. Without non-linearity, a deep network would behave like a
[Link]
simple linear model, severely limiting its ability to learn complex patterns and
relationships in the data.
Answer:
Overfitting occurs when a model learns the training data too well, including its noise
and specific characteristics, leading to excellent performance on the training set but
poor generalization and accuracy on new, unseen data.
Answer:
Traditional Machine Learning often requires manual feature engineering, where human
experts extract relevant features from raw data.
In contrast, Deep Learning algorithms automatically learn and extract hierarchical features
through their multiple layers. Deep learning models use the outputs of preceding layers as
inputs for subsequent layers, allowing them to discover increasingly abstract and complex
patterns directly from the raw data, eliminating the need for explicit feature engineering.
Answer:
𝑒
𝑠𝑜𝑓𝑡 max(𝑧) =
∑ 𝑒
It ensures the outputs are non-negative and sum to 1, making them interpretable as
probabilities. Softmax is typically used as the final activation in multi-class
classification networks (with cross-entropy loss), so that the model’s outputs
represent class probabilities.
7. What are GPUs and TPUs, and why are they used in Deep Learning?
[Link]
Answer:
GPUs (Graphics Processing Units) are hardware accelerators originally built for
graphics, with thousands of cores optimized for parallel matrix and tensor operations.
This makes them ideal for the heavy linear algebra in neural networks.
TPUs (Tensor Processing Units) are custom ASIC chips developed by Google
specifically for accelerating AI workloads. TPUs are highly optimized for tensor
computations (matrix multiplications, convolutions, etc.) and can offer higher
throughput/efficiency than GPUs for large-scale models.
In short, both GPUs and TPUs speed up training and inference dramatically compared
to CPUs. A concise summary: "GPUs are specialized for accelerated compute tasks
(graphics and AI), and TPUs are Google’s custom ASICs for AI workloads". Deep
learning models are typically trained on GPUs or TPUs to reduce training time from
months to days.
Answer:
In training a Neural Network, a batch is a subset of the training data used to compute a
single update of the model’s parameters.
An iteration (or update step) refers to one forward/backward pass using that batch.
For example, if you have 1,000 training examples and use a batch size of 100, then one
epoch consists of 10 iterations (10 batches). During training, we often run for many
epochs, meaning the model sees each example multiple times (with parameter
updates along the way).
Answer:
The bias term in a neuron acts like an intercept in a linear model. It allows the
activation function to be shifted left or right, enabling the neuron to activate even when
inputs are zero. In practice, bias lets each neuron learn a threshold.
[Link]
For example, without bias, a linear neuron’s output is forced through the origin; with
bias, it can fit data that do not pass through zero. Intuitively, bias gives the network
greater flexibility.
10. What is a PyTorch Tensor and how does it differ from a NumPy array?
Answer:
Tensors can be moved to GPU memory for fast computation, whereas NumPy arrays
live on CPU.
Importantly, PyTorch tensors can track computations for automatic differentiation (if
you set requires_grad=True).
PyTorch is tightly integrated with NumPy: you can easily convert between a
[Link] and a [Link] (using methods like torch.from_numpy or
[Link]()), and PyTorch’s tensor operations are largely analogous to NumPy’s.
The key difference is that PyTorch tensors support GPU acceleration and autograd,
while NumPy arrays do not.
Intermediate 🚀
1. What is Autograd? Elaborate w.r.t Pytorch.
Answer:
This gradient information is stored in each tensor’s .grad attribute. As the PyTorch
documentation states, [Link] “is the automatic differentiation engine that
powers neural network training”. Autograd handles the complexity of computing partial
derivatives, so you don’t have to derive them manually.
[Link]
2. What is gradient descent and how does backpropagation work in training
neural networks?
Answer:
At each step, it computes the gradient (slope) of the loss with respect to the model’s
parameters and updates the parameters by moving a small step in the opposite
direction of the gradient. In formula: 𝑤 ← 𝑤 − 𝜂∇ 𝐿, where 𝜂 is the learning rate.
Intuitively, one starts at an initial point on the loss surface, computes the derivative
(slope), and “steps downhill” towards a minimum.
Repeating this process iteratively reduces the loss. As described in literature, gradient
descent “uses the slope of the loss function to iteratively update parameters” until
convergence. Properly tuned, this process finds weights that minimize prediction error.
Backpropagation is the algorithm for efficiently computing gradients of the loss with
respect to each model parameter.
Conceptually, you perform a forward pass to compute the loss, then a backward pass
where errors are propagated back through the network.
During backpropagation, each neuron applies the chain rule to compute how the loss
changes with respect to its inputs and weights. Mathematically, gradients “flow
backwards” through the computational graph. As one source explains: “The backward
pass then performs backpropagation which starts at the end and recursively applies
the chain rule to compute the gradients (shown in red) all the way to the inputs”. These
gradients are then used by an optimizer to update the weights.
Answer:
Batch Gradient Descent (BGD) calculates the gradient using the entire dataset for
each parameter update, leading to stable convergence but being slow and memory-
intensive for large datasets.
Stochastic Gradient Descent (SGD) updates parameters after processing each single
data point, making it faster and memory-efficient but resulting in noisy gradients that
can cause oscillations. However, this noise can help escape local minima.
[Link]
Mini-Batch Gradient Descent combines aspects of both, splitting the dataset into
small batches and updating parameters after each batch. It strikes a balance between
computational efficiency, speed, and stability, reducing noise compared to SGD while
being faster than BGD, and is often the preferred method for large datasets.
4. Explain the Vanishing Gradient problem and one common way to mitigate it.
Answer:
One common way to mitigate it is by using the ReLU (Rectified Linear Unit) activation
function. For positive inputs, ReLU has a constant gradient of 1, allowing gradients to
pass through unchanged during backpropagation, which helps prevent them from
vanishing.
Answer:
Answer:
Both L1 (Lasso) and L2 (Ridge) regularization add a penalty term to the loss function to
prevent overfitting.
The key difference lies in their penalty terms and effects on coefficients:
[Link]
L1 Regularization adds the absolute value of the sum of coefficients as a
penalty. It can reduce some coefficient values exactly to zero, effectively
performing feature selection and creating sparse models.
L2 Regularization adds the squared sum of coefficients as a penalty. It reduces
coefficient values towards zero but never exactly to zero, meaning it doesn't
perform feature selection but is effective at reducing the magnitude of all
coefficients and handling multicollinearity.
Answer:
A CNN has several specialized layers:
Convolutional layers: Apply learnable filters (kernels) that slide over the input image or
feature map. Each filter performs a convolution operation that multiplies and sums
local patches of the input, detecting features like edges or textures. Stacking many
filters yields multiple feature maps.
Stride: The step size of how the filter moves across the input. A stride of 1 moves the
filter one pixel at a time; larger strides down sample the output by skipping positions.
Padding: Adding a border (usually zeros) around the input so that filters can properly
scan the edges or control the output dimensions. For example, “same” padding keeps
output the same size as input by padding sufficiently.
[Link]
8. What is Data Augmentation and why is it used in CNN training?
Answer:
Data Augmentation involves creating modified versions of the training images (or other
data) to artificially enlarge the dataset.
Common techniques include random crops, flips, rotations, color jitter, and scaling.
The goal is to expose the model to a wider variety of inputs, improving robustness and
reducing overfitting.
For example, flipping an image of a cat horizontally still yields a valid cat image, so the
model learns that flipping doesn’t change the class.
Augmentation acts as a regularizer by introducing variability; it effectively trains the
network on many “noisy” variants of the data, which helps generalization to new,
unseen data.
9. What is Transfer Learning in the context of CNNs, and how are pre-trained
models (ResNet, EfficientNet, MobileNet, etc.) used?
Answer:
Transfer learning means taking a model pre-trained on a large dataset (like ImageNet)
and adapting it to a new task.
These pre-trained architectures have already learned useful image features, so even
with limited data you can achieve high accuracy by fine-tuning. Transfer learning
accelerates training and often improves performance compared to training from
scratch on the same dataset.
10. Explain RNNs (Recurrent Neural Networks) and how LSTM/GRU cells work.
Answer:
[Link]
RNNs are neural networks designed for sequential data. In a standard (vanilla) RNN,
the network maintains a hidden state that is updated step-by-step as it processes each
element of the input sequence, allowing it to carry information across time. However,
vanilla RNNs suffer from vanishing (and exploding) gradients, making it hard to learn
long-range dependencies.
LSTM (Long Short-Term Memory) cells solve this by having a more complex internal
structure: a memory cell and gates (input, forget, output) that control what information
to write, keep, or output. This architecture lets the network learn to retain or forget
information over long sequences. GRU (Gated Recurrent Unit) is a simpler gated variant
(with update and reset gates) that also mitigates vanishing gradients. In short, LSTM
and GRU are special RNN units that maintain and
Advanced 🔥
1. What is the Transformer architecture? Describe embeddings, positional
encoding, attention, multi-head attention, encoder, and decoder.
Answer:
The Transformer is a neural architecture for sequence modelling that relies entirely on self-
attention mechanisms, dispensing with recurrence. Key components:
Self-Attention: For each position in the input, attention computes a weighted sum of values
at all positions, where weights come from similarity between a query (at that position) and
keys (at other positions). This lets the model relate different parts of the sequence.
Multi-Head Attention: Several attention “heads” run in parallel, each learning to focus on
different types of relationships. Their outputs are concatenated and projected. This allows the
model to jointly attend to information from different representation subspaces.
Encoder & Decoder: A Transformer model typically has an encoder stack and a decoder
stack. Each encoder layer has a self-attention sublayer and a feedforward sublayer; each
decoder layer has self-attention, encoder-decoder attention (attending to encoder outputs),
and feedforward. In tasks like translation, the encoder processes the source sentence, and
the decoder generates the target sentence one token at a time, attending to the encoder’s
outputs.
[Link]
In summary, Transformers use positional embeddings plus multi-headed self-attention to
capture contextual relationships in parallel, followed by feedforward networks. This
architecture is at the core of models like BERT and GPT.
2. Between ReLU and Sigmoid, which activation would you prefer for a hidden
layer in a large MLP? Why?
Answer:
Reasons:
However, ReLU can suffer from dying units (neurons stuck at zero), for which Leaky
ReLU or variants like GELU are used in practice.
Answer:
To handle imbalanced class labels in a fraud detection model, I’d use a combination of
the following strategies:
Data-Level Approaches
Resampling Techniques:
[Link]
o Oversampling minority class using methods like SMOTE (Synthetic
Minority Oversampling Technique).
o Undersampling majority class, possibly with techniques like Tomek links
or NearMiss to avoid discarding informative samples.
o Combine both (e.g., SMOTE + Tomek) for better balance.
Stratified Splitting:
o Ensure train/val/test splits preserve the class distribution using stratified
sampling.
Algorithm-Level Approaches
Class Weights:
o Assign higher weight to the minority class in the loss function (e.g., weight
parameter in CrossEntropyLoss or class_weight='balanced' in sklearn).
Custom Loss Functions:
o Use Focal Loss to down-weight easy examples and focus the model on
hard, minority-class samples.
Model Evaluation
Ensemble Methods
Train models like Random Forest, XGBoost, or LightGBM, which handle class
imbalance better and can use built-in scale_pos_weight or is_unbalance
parameters.
I'd evaluate multiple strategies experimentally and choose the combination that yields
high recall with acceptable precision in production constraints.
[Link]
4. You’re tasked with building a real-time object detection system for
autonomous drones. What trade-offs would you consider between accuracy
and latency?
Answer:
In a real-time object detection system for autonomous drones, the primary trade-offs
between accuracy and latency revolve around ensuring the system is both fast enough
to react in real-time and accurate enough to avoid false positives/negatives. Key
considerations:
[Link]
o Incorporate temporal smoothing or tracking (e.g., SORT, Deep SORT) to
compensate for occasional missed detections while maintaining low
inference latency.
Answer:
Attention in transformers enables direct access to all tokens in the input sequence at
each layer, allowing the model to compute dependencies between distant words
without sequential processing. Unlike RNNs, which process inputs step-by-step and
struggle with vanishing gradients over long sequences, and CNNs, which rely on fixed-
size kernels limiting context size, attention mechanisms compute pairwise relevance
scores globally.
For example, in the sentence: "The book that the professor who the student admired
wrote was well-received." Understanding that "book" is the subject of "was well-
received" requires capturing dependencies across multiple clauses. Transformers can
compute attention between "book" and "was" directly, regardless of how many tokens
lie in between.
6. Design a model pipeline to classify medical X-rays, and include steps for
ensuring regulatory compliance, reproducibility, and fairness.
[Link]
Answer:
To classify medical X-rays, I'd design a pipeline that starts with de-identified, diverse
datasets, ensuring HIPAA compliance and balanced representation across age,
gender, and ethnicity. I’d preprocess the images (normalize, augment) and split data by
patient ID to prevent leakage.
For modeling, I’d fine-tune a pretrained CNN like DenseNet-121 and use Grad-CAM for
explainability. I’d incorporate uncertainty estimation (e.g., MC Dropout) to flag
ambiguous cases for radiologists. Model performance would be validated with
stratified cross-validation and fairness metrics (e.g., per-group AUC).
To ensure reproducibility, I’d use MLflow for experiment tracking, Docker for
containerization, and fix random seeds. Regulatory compliance would involve
maintaining audit logs, generating model cards, and aligning with FDA SaMD practices.
The deployed model would be monitored for drift, latency, and fairness, with a
feedback loop for periodic retraining.
Answer:
Data Augmentation: For image, text, or audio data, apply advanced augmentation
techniques to increase effective dataset diversity:
o Image: Mixup, CutMix, RandAugment.
o Text: Back-translation, contextual word replacement.
o Tabular: SMOTE, conditional GAN-based synthesis.
Label Smoothing: Prevent the model from becoming too confident by assigning
soft targets (e.g., 0.9 for true class, 0.1 distributed across others). This acts as a
regularizer on the output distribution.
Early Stopping with Checkpoint Averaging: Beyond just halting training early,
average model weights across several top-performing checkpoints (Stochastic
Weight Averaging) to generalize better.
Ensembling: Combine predictions from multiple models (or checkpoints) using
techniques like bagging, snapshot ensembling, or test-time augmentation to reduce
variance.
[Link]
Bayesian Approaches: Use Bayesian Neural Networks or MC Dropout at
inference to model epistemic uncertainty, which can regularize the training process
indirectly.
Adversarial Training: Introduce adversarial examples during training (FGSM, PGD)
to improve robustness, which often improves generalization.
Gradient Clipping / Sharpness-Aware Minimization (SAM): SAM explicitly
penalizes sharp minima in the loss landscape, encouraging flatter solutions that
generalize better.
Noise Injection: Add Gaussian noise to weights, activations, or inputs during
training to regularize the model.
Reduce Model Capacity or Prune: If the model is still overfitting, reduce the
number of parameters or apply structured/unstructured pruning post-training.
Cross-validation Monitoring: Use cross-validation instead of a static validation
split to make the generalization metric more robust and representative.
These techniques are context-dependent, and I would evaluate them based on the data
type, training dynamics, and failure modes observed in metrics.
Answer:
[Link]
How It Affects Context Awareness:
Answer:
First, I would prioritize recall (sensitivity) to ensure we minimize false negatives, which
in healthcare could mean missing a critical diagnosis. At the same time, I'd monitor
precision to avoid unnecessary treatments caused by false positives. The F1 score can
help balance these two when appropriate. However, due to class imbalance often seen
in medical data (e.g., rare diseases), I’d lean on PR-AUC over ROC-AUC to better
reflect the model’s effectiveness on the minority (positive) class.
Calibration is equally important—if the model predicts a 90% chance of disease, that
should correspond to an actual 90% prevalence in similar patients. I’d use calibration
curves and Brier scores to assess this. In addition, I’d run threshold analysis, tuning
the decision boundary based on utility functions or clinical cost matrices, not arbitrary
cutoffs like 0.5.
Beyond performance metrics, I’d conduct detailed error analysis, especially on false
negatives, to identify patterns or biases in the model's failure modes. This leads into
[Link]
fairness analysis—I’d measure group-wise metrics (e.g., TPR, FPR) across
demographic slices to catch any equity issues, using fairness constraints if necessary.
Finally, I’d ensure explainability and auditability using SHAP or similar tools,
especially to meet regulatory standards and maintain clinician trust. In sum, evaluation
in healthcare is about ensuring safe generalization, interpretability, and fairness—not
just scoring well on a test set.
10. Suppose you’re training a CNN on CIFAR-10 and your training accuracy
improves, but test accuracy drops. What could be going wrong?
Answer:
This indicates overfitting—the model is learning patterns specific to the training data but
failing to generalize.
Possible causes:
[Link]