Machine Learning Interview Cheatsheets
Cheat Sheet –Bias-Variance Tradeoff
What is Bias? • Error between average model prediction and ground truth • The bias of the estimated function tells us the capacity of the underlying model to predict the values What is Variance? • Average variability in the model prediction for the given dataset • The variance of the estimated function tells you how much the function can adjust to the change in the dataset High Bias High Variance Overly-simplified Model Under-fitting High error on both test and train data Overly-complex Model Over-fitting Low error on train data and high on test Starts modelling the noise in the input Bias variance Trade-off Minimum Error • Increasing bias reduces variance and vice-versa • Error = bias2 + variance +irreducible error • The best model is where the error is reduced. • Compromise between bias and variance
What is PCA? • Based on the dataset find a new set of orthogonal feature vectors in such a way that the data spread is maximum in the direction of the feature vector (or dimension) • Rates the feature vector in the decreasing order of data spread (or variance) • The datapoints have maximum variance in the first feature vector, and minimum variance in the last feature vector • The variance of the datapoints in the direction of feature vector can be termed as a measure of information in that direction. Steps 1. Standardize the datapoints 2. 3. 4. Find the covariance matrix from the given datapoints Carry out eigen-value decomposition of the covariance matrix Sort the eigenvalues and eigenvectors Dimensionality Reduction with PCA • Keep the first m out of n feature vectors rated by PCA. These m vectors will be the best m vectors preserving the maximum information that could have been preserved with m vectors on the given dataset Steps: 1. Carry out steps 1-4 from above 2. 3. 4. Keep first m feature vectors from the sorted eigenvector matrix Transform the data for the new basis (feature vectors) The importance of the feature vector is proportional to the magnitude of the eigen value
What is Bayes’ Theorem? • Describes the probability of an event, based on prior knowledge of conditions that might be related to the event. • How the probability of an event changes when we have knowledge of another event P(A) P(A B) Example • Probability of fire P(F) = 1% Usually a better estimate than P(A) • Probability of smoke P(S) = 10% • Prob of smoke given there is a fire P(S F) = 90% • What is the probability that there is a fire given we see a smoke P(F S)? P(A B) Posterior Probability Likelihood Bayes’ Theorem P(A) P(B A) Prior Probability Evidence P(B) Maximum Aposteriori Probability (MAP) Estimation The MAP estimate of the random variable y, given that we have observed iid (x1, x2, x3, … ), is given by. We try to accommodate our prior knowledge when estimating. ˆ MAP y that maximizes the product of prior and likelihood Maximum Likelihood Estimation (MLE) The MAP estimate of the random variable y, given that we have observed iid (x1, x2, x3, … ), is given by. We assume we don’t have any prior knowledge of the quantity being estimated. ˆ MLE y that maximizes only the likelihood MLE is a special case of MAP where our prior is uniform (all values are equally likely) Naïve Bayes’ Classifier (Instantiation of MAP as classifier) Suppose we have two classes, y=y1 and y=y2. Say we have more than one evidence/features (x1, x2, x3, … ), using Bayes’ theorem
What is Regression Analysis? Fitting a function f(.) to datapoints yi=f(xi) under some error function. Based on the estimated function and error, we have the following types of regression 1. Linear Regression: Fits a line minimizing the sum of mean-squared error for each datapoint. 2. Polynomial Regression: Fits a polynomial of order k (k+1 unknowns) minimizing the sum of mean-squared error for each datapoint. 3. Bayesian Regression: For each datapoint, fits a gaussian distribution by minimizing the mean-squared error. As the number of data points xi increases, it converges to point estimates i.e. 4. Ridge Regression: Can fit either a line, or polynomial minimizing the sum of mean-squared error for each datapoint and the weighted L2 norm of the function parameters beta. 5. LASSO Regression: Can fit either a line, or polynomial minimizing the the sum of mean-squared error for each datapoint and the weighted L1 norm of the function parameters beta. 6. Logistic Regression (NOT regression, but classification): Can fit either a line, or polynomial with sigmoid activation minimizing the sum of mean-squared error for each datapoint. The labels y are binary class labels.
Cheat Sheet –Regularization in ML What is Regularization in ML? • Regularization is an approach to address over-fitting in ML. • Overfitted model fails to generalize estimations on test data • When the underlying model to be learned is low bias/high variance, or when we have small amount of data, the estimated model is prone to over-fitting. • Regularization reduces the variance of the model Types of Regularization: 1.Modify the loss function: Figure 1. Overfitting • L2 Regularization: Prevents the weights from getting too large (defined by L2 norm). Larger the weights, more complex the model is, more chances of overfitting. • L1 Regularization: Prevents the weights from getting too large (defined by L1 norm). Larger the weights, more complex the model is, more chances of overfitting. L1 regularization introduces sparsity in the weights. It forces more weights to be zero, than reducing the the average magnitude of all weights • Entropy: Used for the models that output probability. Forces the probability distribution towards uniform distribution. 2.Modify data sampling: • Data augmentation: Create more data from available data by randomly cropping, dilating, rotating, adding small amount of noise etc. • K-fold Cross-validation: Divide the data into k groups. Train on (k-1) groups and test on 1 group. Try all k possible combinations. 3.Change training approach: • Injecting noise: Add random noise to the weights when they are being learned. It pushes the model to be relatively insensitive to small variations in the weights, hence regularization • Dropout: Generally used for neural networks. Connections between consecutive layers are randomly dropped based on a dropout-ratio and the remaining network is trained in the current iteration. In the next iteration, another set of random connections are dropped.
AlexNet –2012 Why: AlexNet was born out of the need to improve the results of the ImageNet challenge. What: The network consists of 5 Convolutional (CONV) layers and 3 Fully Connected (FC) layers. The activation used is the Rectified Linear Unit (ReLU). How: Data augmentation is carried out to reduce over-fitting, Uses Local response localization. VGGNet –2014 Why: VGGNet was born out of the need to reduce the # of parameters in the CONV layers and improve on training time What: There are multiple variants of VGGNet (VGG16, VGG19, etc.) How: The important point to note here is that all the conv kernels are of size 3x3 and maxpool kernels are of size 2x2 with a stride of two. ResNet –2015 Why: Neural Networks are notorious for not being able to find a simpler mapping when it exists. ResNet solves that. What: There are multiple versions of ResNetXX architectures where ‘XX’ denotes the number of layers. The most used ones are ResNet50 and ResNet101. Since the vanishing gradient problem was taken care of (more about it in the How part), CNN started to get deeper and deeper How: ResNet architecture makes use of shortcut connections do solve the vanishing gradient problem. The basic building block of ResNet is a Residual block that is repeated throughout the network.
Cheat Sheet –Convolutional Neural NetworkConvolutionalNeuralNetwork:ThedatagetsintotheCNNthroughtheinputlayerandpassesthroughvarioushiddenlayersbeforegettingtotheoutputlayer.Theoutputofthenetworkiscomparedtotheactuallabelsintermsoflossorerror.Thepartialderivativesofthislossw.r.tthetrainableweightsarecalculated,andtheweightsareupdatedthroughoneofthevariousmethodsusingbackpropagation.CNNTemplate:Mostofthecommonlyusedhiddenlayers(notall)followapattern1.Layerfunction:Basictransformingfunctionsuchasconvolutionalorfullyconnectedlayer.a.FullyConnected:Linearfunctionsbetweentheinputandtheoutput.-2.0-1.00.01.02.00.00.51.01.52.02.53.03.54.0MSELossmse=(x°ˆx)2mse=(x°ˆx)2-2.0-1.00.01.02.00.00.250.50.751.01.251.51.752.0MAELossmae=|x°ˆx|mae=|x°ˆx|-2.0-1.00.01.02.00.00.250.50.751.01.251.51.752.0HuberLossΩ12(x°ˆx)2:|x°ˆx|<∞∞|x°ˆx|°12∞2:elseæ∞=1.9Ω12(x°ˆx)2:|x°ˆx|<∞∞|x°ˆx|°12∞2:elseæ∞=1.9-2.0-1.00.01.02.00.00.51.01.52.02.53.0HingeLossΩmax(0,1°ˆx):x=1max(0,1+ˆx):x=°1æΩmax(0,1°ˆx):x=1max(0,1+ˆx):x=°1æ0.00.20.40.60.81.00.02.04.06.08.0CrossEntropyLoss°ylog(p)°(1°y)log(1°p)°ylog(p)°(1°y)log(1°p)0.00.20.40.60.81.00.00.20.40.60.81.0Input MapKernelOutput MapConvolutional Layera.ConvolutionalLayers:Theselayersareappliedto2D(3D)inputfeaturemaps.Thetrainableweightsarea2D(3D)kernel/filterthatmovesacrosstheinputfeaturemap,generatingdotproductswiththeoverlappingregionoftheinputfeaturemap.b.TransposedConvolutional(DeConvolutional)Layer:Usuallyusedtoincreasethesizeoftheoutputfeaturemap(Upsampling)Theideabehindthetransposedconvolutionallayeristoundo(notexactly)theconvolutionallayer2.Pooling:Non-trainablelayertochangethesizeofthefeaturemapa.Max/AveragePooling:Decreasethespatialsizeoftheinputlayerbasedonselectingthemaximum/averagevalueinreceptivefielddefinedbythekernelb.UnPooling:Anon-trainablelayerusedtoincreasethespatialsizeoftheinputlayerbasedonplacingtheinputpixelatacertainindexinthereceptivefieldoftheoutputdefinedbythekernel.3.Normalization:Usuallyusedjustbeforetheactivationfunctionstolimittheunboundedactivationfromincreasingtheoutputlayervaluestoohigha.LocalResponseNormalizationLRN:Anon-trainablelayerthatsquare-normalizesthepixelvaluesinafeaturemapwithinalocalneighborhood.b.BatchNormalization:Atrainableapproachtonormalizingthedatabylearningscaleandshiftvariableduringtraining.3.Activation:Introducenon-linearitysoCNNcanefficientlymapnon-linearcomplexmapping.a.Non-parametric/Staticfunctions:Linear,ReLUb.Parametricfunctions:ELU,tanh,sigmoid,LeakyReLUc.Boundedfunctions:tanh,sigmoid5.Lossfunction:QuantifieshowfarofftheCNNpredictionisfromtheactuallabels.a.RegressionLossFunctions:MAE,MSE,Huberlossb.ClassificationLossFunctions:Crossentropy,Hingeloss
Cheat Sheet –Ensemble Learning in ML What is Ensemble Learning? Wisdom of the crowd Combine multiple weak models/learners into one predictive model to reduce bias, variance and/or improve accuracy. Types of Ensemble Learning: N number of weak learners 1.Bagging: Trains N different weak models (usually of same types –homogenous) with N non-overlapping subset of the input dataset in parallel. In the test phase, each model is evaluated. The label with the greatest number of predictions is selected as the prediction. Bagging methods reduces variance of the prediction 2.Boosting: Trains N different weak models (usually of same types –homogenous) with the complete dataset in a sequential order. The datapoints wrongly classified with previous weak model is provided more weights to that they can be classified by the next weak leaner properly. In the test phase, each model is evaluated and based on the test error of each weak model, the prediction is weighted for voting. Boosting methods decreases the bias of the prediction. 3.Stacking: Trains N different weak models (usually of different types –heterogenous) with one of the two subsets of the dataset in parallel. Once the weak learners are trained, they are used to trained a meta learner to combine their predictions and carry out final prediction using the other subset. In test phase, each model predicts its label, these set of labels are fed to the meta learner which generates the final prediction.
Comments
Post a Comment