Miscellaneous
Some basic machine learning knowledge which are frequently applied in various research (also often asked in interview). I wrote them down for review.
Pareto optimality
It is a resource allocation state in which it is impossible that reallocate the resources so as to improve an individual situation without making other individuals worse off.
It is a minimum notion of efficiency but unnecessarily lead to desire results since it doesn’t take fairness and equality into account.
Pareto improvement
It means we can reallocate the limited resources to make some individuals better off without making any other individuals worse off.
Precision-recall estimation
False positive (FP): classify the negative class into positive category.
False negative (FN): classify the positive class into negative category.
Recall越低,很多正例被错误分类(放走了很多正例) Precision越低,很多负例被错误分类(包进了很多负例)
ROC and AUC
ROC is the curve based on TPR and FPR.
AUC is the area under the ROC curve.
AUC作为评价指标,可以有效处理样本不平衡问题。比如:在垃圾邮件分类中,垃圾邮件的label = 1,只占总样本的1%;非垃圾邮件的label = 0,占总样本99%。 如果一个naive分类器把所有的样本分成0,那么也有99%的精确度。 但是如果用AUC作为评价指标时,可以得到TPR=0(实际是垃圾邮件的,分对了多少),FPR=0(实际非垃圾邮件,分错了多少),不管分类的阈值怎么取,始终都是坐标系中的点(0, 0),因此AUC=0 ? (需要进一步搞清楚AUC的计算公式)
Average precision (AP and mAP)
AP is a common metric in various object detection paper. It is related with recall and precision. Conceptually, it is the area of recall-precision curve. See below picture.
But for convenience, people usually approximately estimate this area by interpolation. Check here for the specific algorithm.
Intersection over union (IoU)
This metric has been widely used on object detection. It is used for measuring how accurate the predicted bounding box is.
Bias and variance
Bias is used to describe how close it is between the prediction and true value.
Variance is used to describe how concentrated the predictions are.
Overfitting usually lead to high variance since it is trained to fit the noises in training data.
Underfitting usually lead to high bias since it is unable to capture enough correlation between input and target.
Classification and regression
The training target in classification is discrete.
The training target in regression is continuous.
Supervised learning, unsupervised learning, semi-supervised learning
The training data is labeled in supervised learning like classification
The training data is unlabeled in unsupervised learning like clustering, generative adversarial network, EM algorithm, PCA and etc.
Off-policy and on policy
Off-policy is the training target of a policy is different from the behavior policy like 𝜀-greedy (more exploration)
On policy is the training target of a policy is exact the same with the behavior policy. (less exploration)
On policy can converge faster than off-policy.
Feature selection (how to select feature from massive statistical data)
Filtering: according to the feature variance
Wrapper: we can randomly pick some features to train and evaluate the result
Embedded: we can use a machine learning method to train first and then check how important those features are.
Feature dimensionality reduction
There two common methods: PCA and LDA. PCA is an unsupervised dimensionality reduction method, whereas LDA is supervised.
The main difference between them:
PCA try to extract new feature vector which has big variance. LDA try to project original space to low dimensional space so as to maximize the distance between different classes and minimize the within-class variance. Please check here for details about LDA.
Generative model
All types of generative models aims at learning the true distribution of training data so as to generate new data point with some variations. But it is not always possible to learn the exact data distribution.
It belongs to unsupervised learning (we don’t need to label the training data).
Two types of generative models:
- Variational autoencoder (VAE)
- Generative Adversarial Networks (GAN) check here and paper for detailed introducation.
- The generator network and discriminator network (evaluator).
P, NP, NP-Complete, NP-hard
P is the problem that we can find a solution which can be finished in polynomial time.
The relationship between P, NP, NP-hard and NP-Complete.
NP is the problem that we may not be able to find a polynomial time complexity solution but we can validate if a specific solution is correct or not within polynomial time.
NP-hard is a more general problem that some NP problems can reduce to within polynomial time. Once a NP-hard problem is solved, all such reducible NP problems will be solved. (Note: Sometimes, after NP reduce to NP-hard, this NP-hard problem may not be validated within polynomial time). The global composition in jigsaw puzzle solving can be seen as a SAT problem variant, but it cannot be validated within polynomial time. So it belongs to NP-hard.
NP-complete is overlap between NP-hard and NP, which means they are reduced from NP and still can be validated within polynomial time.
SAT (satisfiability) problem: if we can find a Boolean assignment that make a system output true. For example, we cannot find such assignment for , but we can find for .
Variant: 2-SAT and 3-SAT. See this intuitive example (过年了,正打算烧年夜饭 blabla…)
Problem Reduce: this means we can convert a problem into a more general (usually more difficult) problem. For example, problem A: find the minimum element in an array. Problem B: sort the whole array. We can say A can be reduce to B, since if we can solve the problem B, problem A will be solved trivially.
The advantage of max pooling layer
Reduce the number of parameters. To avoid over-fitting
Increase the perceptual field
Extract the main features.
Convolutional Neural Network
Batch normalization
Suppose input= [batch, height, width, depth]. If we use axes= [0,1,2] to calculate (e.g. mean, var = tf.nn.moments(input, axes=[0, 1, 2])), then the output of mean, var will be a 1-D vector with size=depth. If we use axes=[0] to calculate, then the output of mean, var will be a 3-D vector with size=[height, width, depth]. The picture below demonstrates this 3-D vector case.
Group normalization
The comparison between several normalization methods
Batch normalization has several defects:
- BN only works fine with large batch size, but this cannot guaranteed on complex tasks (e.g. object detection) due to insufficient memory.
- BN use mini-batch’s mean and variance to normalize during training, but use moving-average (滑动平均) mean and variance to normalize during testing. It introduces inconsistency, especially when data distribution between training and testing are different.
Unlike BN to normalize on dimension [batch, width, height], the group normalization (GN) try to normalize on dimension [width, height, channel=k] (k is a hyperparameter). It doesn’t matter with batch. So it solves the first defect. Moreover, GN always use group mean and variance during training and testing. IN, LN and GN doesn’t use batch dimension to normalize.
Recurrent Neural Network (RNN)
An tutorial and introduction about RNN
Hidden Markov Model
An specific dice example will help to understand.
The basic elements: Transition matrix, emission matrix, initial state.
Three basic problems:
- Given the HMM model, how to calculate the probability of an observation. (forward algorithm)
- Given the HMM model and a sequence of observations, how to estimate which the most possible hidden state sequences are. (Viterbi algorithm, dynamic programming)
- Given the observation data, how to estimate the model parameters. (learning problem, Baum–Welch algorithm)
Note: In the learning problem, the target of forward algorithm is to calculate the . The target of backward algorithm is to calculate the . The target of forward-backward algorithm is to calculate the .
For the details, please check here
EM (Expectation-Maximization) algorithm
An intuitive tutorial.
EM algorithm is an unsupervised learning.
Maximum likelihood vs Maximum A Posterior (MAP)
<=> .
Usually given the training data , our target is to maximize the posterior , where is the model parameters. To do that, we usually have two approaches:
-
Maximum likelihood. Since , if we can assume the prior is a constant, then . Maximum likelihood estimation is widely used in solving various machine learning models, especially when prior is unknown.
-
Maximum A Posterior (MAP). Sometimes, we may not be able to make such assumption that the prior is a constant. Instead, prior may satisfy a distribution. In this case, the target is . Since every data is generated independently, we have . If we use prior to represent the complexity of a model, then MAP is linked with structure risk minimization.
A systematic probabilistic graph model course
https://ermongroup.github.io/cs228-notes/
Bayes rule and Bayes network
Bayes rule is widely used on various machine learning problems. Some basic math/probabilistic knowledge need to be clarified.
The general Bayes rule for joint distribution:
If we have a bayes network which models the problem, we can simplify the calculation. For example, given a simple network as below, we have
This can work because (when we know variable B, the variable A and C can be seen independent. But if B is unknown, A and C are dependent. This is because we can use the medium variable B to represent the dependence between A and C.)
From this network, we can have some other property like
Because and
Generally, we also have below property without graph structure if are independent.
Because
Several concepts need to be distinguished
- Bayesian Network
- Markov Random Field
- Conditional Random Field
- Markov Chain
- Hidden Markov Model
- Markov Decision Process
Why divide n-1 to get unbiased variance estimation in sampling data
http://blog.sina.com.cn/s/blog_c96053d60101n24f.html
A good systematic Chinese tutorial for machine learning
https://nndl.github.io/
The summary of math part is a good material for reviewing the math background.
Information theory
Refer to here for detailed introducation.
Encode length for a random variable , .
Entropy: the average length for optimal encoding the whole of random variable X. . The more stochastic variable is, the larger entropy is.
Cross-entropy: the average encoding length when we use distribution to encode information whose real distribution is .
Obviously, , when . We have minimum , when .
Kullback-Leibler divergence (KL-divergence):
The meaning of KL-divergence is very similar with cross-entropy. Both of them measure how different between distribution and . So when is given, the optimization process of and is the same. Comparing with cross-entropy, KL-divergence measures the absolute difference. When , .
Logistic regression
sigmoid function : convert linear classification result to a probability.
maximize likelihood => solve the parameters in linear classification.
The key here is that sigmoid function normalize raw result into 0 and 1. Then we can construct below likelihood.
Apply logarithm on both side.
This is equivalent to cross-entropy. And then we can apply gradient descend to optimization.
Check here for details.
Support Vector Machine
Please check here1 and here2 for the details about naive SVM, SVM with kernel and SVM with soft margin.
Here, we give a brief introducation.
In SVM, we want to find a hyperplane (w, b are the parameters to learn), so that this hyperplane can correctly divide two classes (the labels are -1, 1) data points.
We define that the data points which are the nearest ones to hyperplane should locate on (for positive datapoint) and (for negative datapoint). The reason why this definition makes sense is that we can always adjust the learned hyperplane by multiple a factor (e.g. k) without change the hyperplane in space (i.e. <=> ).
So our goal is to maximize the distance between and . Obviously, the distance is . We can write the objective function as below
To solve this objective, we apply Lagrangian multipler to convert original constrained problem to
Then calculate the partial gradients w.r.t. first, then . This is the naive SVM
Sometimes, the datasets cannot be divided by linear plane. So we usually use kernel tricks, which introduce nonlinear property in classifier. The main idea of kernel trick is to find a mapping to map original space to a higher space. The picture below gives an example. This is the SVM with kernel. Check here for the reasons why kernel can map to a higher dimensions.
The P mapping in above picture is the . You should distinguish the kernel function and dimensional mapping function.
However, sometimes, kernel can still fail. So people introduce the soft margin, which allows misclassification on some data points. The objective is This is soft margin.
The can be seen as hinge loss: . And SVM can be explained in hinge loss.
Kalman filter
It is a algorithm for accurately estimating or predicting based on multiple observed data. See here for an intuitive explanation and example.
A classic example is SLAM in which we have multiple data collecting sensors like odometry, IMU and visual features. The Kalman filter is to solve how to reliably combine all sensor data and estimate a accurate pose.
In linear case, the Kalman filter problem can be formulated as
The first equation is motion equation where is the state at -th moment. is motion measurement with noise which is satisfied a gauss distribution .
The second equation is observation equation where is the observation measurement with noise which is also satisfied a gauss distribution .
Since the motion and observation measurements are noisy (subject to gauss distribution) which means that the current state estimation is noisy too, our goal now is to find an optimal state estimation which has the minimum uncertainty (i.e. minimum covariance).
The way is to calculate Kalman Gain which is the optimal weight between motion and observation. I ignore the derivation of Kalman Gain, since it is a little bit complicated. You can search online about this.
The complete algorithm is:
First, predict currect state from previous state based on motion equation. (The previous state can be represented by gauss distribution )
This equation is easy to understand according to the properties of gauss distribution (The new mean and new covariance from two gauss distribution).
Second, calculate the Kalman Gain
Finally, correct/modify the rough estimation from motion equation.
If you derivate a little, you can find that if (no covariance in observation), then , which means the final estimation is totally depended on observation equation. There is a similar conclusion when .
Now we know the idea of Kalman filter in linear system.
In reality, the system is usually nonlinear (e.g. SLAM problem). To apply above idea, we need to extend KF to nonlinear case. The Taylor expansion can do this.
Cross-entropy instead of mean square error (MSE) as the loss function in classification?
when we do classification, we usually apply softmax (this is an important premise) to normalize the output value to 0-1. In this case, the gradient of MSE loss will be prone to 0 when the prediction is closed to 0 or 1. This will lead to slow convergence. On the contrary, the gradient of cross-entropy is linear with the prediction changing. When the prediction is closed to the label, the gradient will be small and vice versa.
* Cross-entropy
where is the objective cross-entropy function. To minimize it, we calculate the gradient w.r.t.
So we have
When is closed to 1, the gradient will be prone to 0. (Remember that we use one-hot encoding to represent )
* MSE
So we have the gradient
When is closed to 0 and 1, the gradient will be closed to 0. This is not desirable and will slow down the learning process. Because if , we hope the learning step is large, but the gradient is 0.
The property of softmax
- property: the big value will take a large portion of the probability.
- potential drawback: exponential problem.
Covariance and correlation coefficients
Covariance is a measure of how two variables change together, but its magnitude is unbounded, so it is difficult to interpret. By dividing covariance by the product of the two standard deviations, one can calculate the normalized version of the statistic. This is the correlation coefficient.
- Covariance: represent the unnormalized correlation.
- Correlation coefficient: represent the normalized correlation.
Refer to here for details.
Normal distribution and multivariate normal distribution
- One-variable
- Multi-variables
where is the determinant of covariance matrix C(X). By the way, C(X) is usually denoted as in many literatures.
Refer to here for details.
Some important concepts in machine learning (statistic learning)
1. Hypothesis space
Hypothesis space is a set of models which map the input to output. When hypothesis space is given, the range of learning space is determined. For example, if we plan to use a neural network as the model, the hypothesis space consists of all possible parameters of this neural network.
2. Expected risk, Empirical risk, Structure risk
In machine learning, we usually suppose all data are i.i.d (independently drawn from identical distribution). The expected risk is defined as
where are training data. is loss function. is joint probability distribution. The ideal learning procedure is to find a best which can minimize . However, it is impossible to know joint probability distribution beforehand. Because there is no need to learn if we already know . Therefore, machine learning is usually an ill-posed problem. About the ill-posed problem, you can check here and here
In practice, we minimize empirical risk instead of expected risk.
Obviously, is the loss w.r.t. joint probability distribution which is absolutely accurate. is the loss w.r.t. the average of training data which is an approximation. According to law of large numbers, this approximation could be accurate when giving enough training data.
Structure risk is similar with empirical risk. The only difference is that it introduces regularization term to penalize complex model to relieve overfitting.
3. Cross validation, k-fold cross validation
Cross validation is a strategy to select a model with best generalization ability. When we have enough labeled data, we can simply split them into training, validation, testing sets and use validation set to select model. K-fold cross validation is also a common strategy when data are relatively insufficient.
The general procedure is as follows:
- Shuffle the dataset randomly.
- Split the dataset into k groups
- For each unique group:
- Take the group as a hold out or test data set
- Take the remaining groups as a training data set
- Fit a model on the training set and evaluate it on the test set
- Retain the evaluation score and model
- Select a model with best score
4. Generative model, Discriminative model
If a model try to learn joint probability distribution , it is a generative model. If a model learn conditional probability distribution (i.e. decision boundary), it is a discriminative model.
These two concepts here are different from they are in GAN (generative adversarial network).
5. Learning Bias (Inductive Bias)
The introduction from wiki
In machine learning, models or algorithms are trained on training dataset and make predictions on testing datasets which are unseen data. Without any additional assumptions, this problem cannot be solved exactly since unseen situations might have an arbitrary output value. The kind of necessary assumptions about the nature of the target function are subsumed in the phrase inductive bias
The difference between bagging and boosting, the advantage of assembly learning
The difference see here.
I make a brief summary here.
Bagging (usually used in random forest)
- Bootstrap Sampling from training data (random sampling without replacement).
- All samples with same weights in training.
- K seperate models with same importance. So all models can be trained parallelly
Boosting (e.g. adaptive boosting method)
- Training on all data with weights (misclassified data will be more important in next training round)
- K seperate models with different importance.
Why Bagging improve performance?
There are two explanations:
- It can reduce the variance of single decision tree since model may fit some minor features if we use all training data. Minor feature fitting leads to high variance (over-fitting).
- From probabilistic perspective, the probability of wrong prediction from multiple models is low.
Why Boosting improve performance?
The goal of optimization is to minimize the misclassified data.