Machine learning notes [up to date]

Machine learning notes [Starter]

This article is Andrew Ng "machine learning" notes due course content more basic, so this for some lack of knowledge many math

Multivariate linear regression

  1. Linearization nonlinear function

1568943455519

In this case, the zoom feature becomes very important (because even in the case of cubic square, features a lot of value to enlarge)

Not necessarily as long as the square, square root can also:

1568944277368


The normal equation

Compared gradient descent method, the optimal solution can be obtained more quickly.

Normal equation method is more suitable for smaller problem, and gradient descent method is applicable to larger data sets

Zoom feature does not require the normal equation

1568945243848

Wherein at the beginning of each column 1, and then added something transposed, is calculated by the formula:

1568945308521

1568945227901

If X ^ tX irreversible how to do?

Overall, this is still relatively small

pinv () command can also solve for the correct answer in the case of matrix irreversible

1568945509678

It can also cause problems when you want to solve characteristic is greater than the number of samples.

Solution irreversible problem:

  1. Delete linearly dependent variables
  2. When too many features, remove some features, or use the normalization method

Vectorization

Multi-use built-in function library (teacher means made their own wheels smaller than others in 2333)

1568946076219

Left is to write cycle, the right is a vector multiplication method call. Since the right use of the built-in multiplication, efficiency is higher than their writing

1568946191981

C ++ libraries that are also linear operations

1568946436701

Then the situation is a little complicated to quantify slowly after treatment


Logistic regression

Still is an algorithm commonly used in machine learning

Linear Programming performance in the general classification problems is not very good

1568948465054

Linear programming method of setting a threshold value to do so in Classification

Logistic regression is a classification algorithms, regression analysis, and it does not matter, the name a little problem, a problem left over from history

  • Sigmoid function\Logistic function

These two are a thing, just not the same name

1568948696773

1568948730151

So long

1569047880702

Its use is to achieve and maintain the continuity of the unit step function, so that after the differential operation and the like. It is used primarily because of its shape and more similar to the unit step function, it may serve a similar function.

1569048021851

Now our problem is to predict θ

1568948867575

Output in the current conditions, the probability of y = 1, and 1 is subtracted from y = 0, the probability

Decision boundary decision limit

1568949219059

1568949295714

Function is a sigmoid function g is above

Decisions and assumptions supporting plane is, and it does not matter training set

One more example:

1568949474315

Assuming that function:

1568949502589

So, after using the sigmoid function, decision boundary is:

1568949549236


Dry warning

What is the difference Logistic regression and linear regression is?

初学的时候,我对这两种方法总是很疑惑,总感觉他们明里暗里是差不多的意思,后来我思考了之后才豁然开朗。

Logistic回归和线性回归可以说是用同样的技术解决不同的问题线性回归解决的是典型的回归分析问题。

在统计学中,回归分析(regression analysis)指的是确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法。

另外,对于一个回归方程,如果自变量的指数大于1,那么它就是多项式回归方程。如下方程所示:

y=a+bx2

在这种回归技术中,最佳拟合线不是直线。而是一个用于拟合数据点的曲线。

来源:百度百科

线性回归的作用可以描述为拟合确定变量间的线性关系,以便于通过一个指标来预测另外一个指标,在二维平面上,可以理解为“由点画线”。而Logistic回归解决的问题却是分类,但是却是在用回归的思路解决:在二维平面上,Logistic回归利用回归的思想来确定决策平面,之后通过Logistic函数这个近似的阶跃函数来达到二分类的目的。我们可以把Logistic函数看做一个开关,其实它核心的功能函数构建决策平面,而这一过程和线性回归一样,都是通过求代价函数的极小值点实现的,只不过这两种方法由于公式不一样,要是都套用线性规划的代价函数,Logistic函数的代价函数会出现多个极值点,这就给梯度下降法的使用造成了很大困难,所以又自己构造了个。

Logistic方法的决策边界一定是线性的吗?当然不是的,具体看上一节的例子。但这种就属于符合函数了,不是原本的Logistic模型了。

举一个使用Logistic回归解决问题的例子:

1569049381795


代价函数

以一个例子开头:

1568949870545

h(x)是我们选择的预测模型,现在的问题是如果确定模型中的参数

1568950081801

这里讲了,我们原来常见的Cost函数不适用于现在的问题,因为它画出来的代价函数的图像是一个非凸函数,有很多的局部最优解,这给我们使用梯度下降法来求解全局最优解带来了很多问题。我们希望的代价函数是右面形式的。例如线性回归的代价函数就是一个凸函数:

1568953964982

Logistic上常用的代价函数为:

1568950409199

上面的式子还可以合成为一个:

1568951347414

这个估计方法是由统计中的极大似然法来的

这里的h(x)输出的是没有用决策平面截断之前的连续值:可以看一下,第一个公式中如果h(x)=0的问题有多大!(当然这一点也不是我们选择的代价函数有问题,而是我们主动选择的这一特性,因为在输出值为[0,1]的情况下,y=1而h(x)=0确实是最严重的预测失误了)

1568950571447

使用梯度下降法计算最合适的θ:

1568952951909

这个式子和做线性回归的梯度下降法时是一样的。当然这和h(x)没什么关系,其实体现的是用在两种方法上的代价函数的导数形式是一样的。

关于梯度下降法,有一些实践上的细节需要注意:

  1. θ0和θ1的更新需要是同时进行的,不可以更新完θ0之后再计算θ1,这样结果会有偏差

1568953521342

  1. 一般所说的梯度下降方法,也就是这里说的Batch梯度下降方法,是遍历了整个样本集合的,也就是对每一个样本都求一遍,最后取平均值。也有方法不遍历所有的样本

1568954156804

对梯度现下降法的高级优化:

1568994006217

课程没有讲这些部分

自动选择学习率α,甚至为每一次迭代设置不同的学习率,所以他们的收敛速度比梯度下降法要快很多

“很有可能成功应用了这些方法并解决了实际问题,但并不知道这些算法的内循环中在做什么”“如果你不是数值计算方面的专家,不要试着自己去实现像BFGS这样的算法”2333

功能的实现是否足够好对于学习过程是有影响的,所以如果使用的是其他的编程语言,比如Java、C++等,可能要多试几个库来找那个效果最好的

一些高级方法的具体实现:

1568994496732

1568994568071

fminunc会调用高级方法之一来解决问题

initialTheta:θ的初始值

100:迭代次数

exitFlag指示算法是否收敛

要调用fminunc方法,θ的数量必须大于等于2。

1568994948115

由于这些方法的实现我们都是不知道的,算是一个黑盒,所以在debug的时候可能会有些麻烦,但是由于它的速度要远好于梯度下降法,所以一般还是选择这种方法


Logistic在多元分类中的应用

总体思想:将多元分类问题拆分为多个二分类问题,对于每一个分类,只区分“我”和“非我”

我们分别设计三个分类函数,之后:

1568995669268

选择概率最大的那个


正则化

过度拟合(Overfitting)

1568996413235

欠拟合、拟合正常和过拟合

过拟合也被称为高方差

如果我们拟合出来的是一个很高阶数的多项式,那么它可以完全拟合任何小于其阶数数量的数据点,这是没有意义的

同样的问题不仅仅存在于线性回归中,还存在于Logistic回归等问题中

对于一维和二维的数据,overfitting的问题很好解决,因为我们可以直观地看出数据的分布以选择最合适的拟合方法,也可以将拟合的方法和数据做比较来看它是不是overfitting了。但是这种情况太少了,大多数情况下,我们的问题的维度都很高,这种方法是不行的。

如果变量太多而数据量太少的话,overfitting就会出现

减少过拟合的方法:

  1. 减少变量数,手动确定保留哪些变量
  2. 使用模型选择算法,自动选择要保留的变量

但是,在减少变量数的同时,我们关于问题的信息量也减少了

另外的方法就是正则化了:保留所有变量,但是减少量级

为特征项加入惩罚项

1569030113689

在求和函数中加入正则项,注意这里约定成俗地只对1到n进行求和,而不去管第0项(实践中来说,是否包括0对于结果没什么影响)

这里的λ称为正则化参数,作用是控制两个不同目标(更好的拟合数据和保持参数尽可能小)之间的取舍

这里的λ如果设置的过大,可能会导致参数结果变得太小,使得拟合不准确

  1. 线性回归的正则化

1569030832369

如图,加入惩罚项后θj相比原来变得更小了(减小地更快了)

如果使用正规方程呢?

1569045348410

那如果正规方程中的1569045382611不可逆怎么办?

这种情况下,如果我们依然在软件中执行Pinv,依然可以得到一个结果,但是这个结果的效果就不会很好了。

1569046929793

可以证明的是,如果对1569045382611乘上了后面带λ的矩阵,这个矩阵就一定是可逆的,这样就不会有不可逆的问题了。

  1. Logistic回归的正则化

对Cost函数进行修改实现正则化:

1569047266961

对梯度下降法进行修改:

1569047348993


神经网络

Logistic分类遇到的问题:

特征数量太多,很容易过拟合(样本数量赶不上特征数量)

Logistic不适合作为非线性分类:一般的问题,例如图片识别,特征的维度都是很高的。

1569050251266


神经网络:生物学

神经重接实验:神经元是具有学习能力的

神经元获取信息,处理后传递给其他神经元


神经网络基础知识

神经元是最小的逻辑单元:

1569051026443

输入x1\x2\x3,输出h(x)的值

1569051094197

X0是一个恒等于1的值,叫做偏置单元或偏置神经元,但有时候也不画了。

激活函数:1569051788129

神经元的参数这里用θ表示,有时也叫权重(weight)

神经网络就是一些神经元连接在一起的集合。

1569051877313

这里省略了第0个神经元:

1569051940799

第一层被称为输入层,我们在这里输入特征(feature);最后一层称为输出层,输出最终计算结果;中间层称为隐藏层

符号表示:

1569052114858

每条连接线都有自己的值和对应的计算权重

1569052234471

这里的函数g是激活函数,一般是Logistic函数

1569052347458

输入有四个(算上一个隐藏的),但是只加在三个上面(因为第0个恒为1,不需要计算

上角标表示第几层,下角标表示这一层的第几个

计算原则:

1569052421822

基本结构就是这样,神经网络的效果通过改变θ实现

前向传播:依次计算输入层-隐藏层-输出层

向量化计算:

1569053878954

(这里的内容和数电有点类似了2333)

  1. 拟合AND运算的网络:

1569054842280

利用偏置神经元,只有两个都为正的时候才能比偏置神经元的绝对值大,结果才能为正,输出才能是1。如图:

1569054922394

  1. 拟合OR运算的网络:

    1569054959439

    一个意思,只要有一个为正结果就是正数

  2. 拟合NOT运算的网络:

1569055053229

这个其实已经不是单纯的NOT了。其实要拟合NOT很简单,只要为对应的神经元上加一个绝对值很大的负数特征,使得只要他为正就会输出0就好了

  1. 拟合XOR(异或)运算的网络:

  2. Fitting the XNOR (exclusive or negated) of the network operator:

XNOR is not basic types, the basic type needs to be combined

1569055306498

XNOR = OR (AND, (NOT) AND (NOT)) // necessarily a true or false

1569055426873

Multilayer result, ultimately achieved on the basis of a complex function of each instruction is executed the simple


Using neural networks for multi-classification

Corresponding multi-output multiple classification, only the i-th output is determined whether i category

Note how the results here for more representation: multi-classification, there are several categories, we need to use a few layers to represent the output results.

1569056261298

Neural network cost function1569068584060

Where K is the dimension of the result vector, summing the squares of θ is actually the last of the neural network in all of θ (in addition to biasing from bias neurons and neuronal fate, attention summation is starting from 1 a) all forget it again. Well of course, if it should be true, but this is not the way some of the more common.


Minimizing the cost function with backpropagation

The following give an example to illustrate how to calculate the cost function and the neural network:

1569069477704

Back-propagation:

1569069767379

δ represents the error between the predicted and actual values, is the derivative of the cost function is computed intermediate terms

The final layer is the best error calculated by subtracting the actual value of the direct enough predicted value

Note that the first layer is no error, it does not exist δ1

The whole process is calculated by the rear forward thrust reverser

Using the back-propagation method:

1569070253749

  1. Set the initial value
  2. Before use each intermediate calculation result to the algorithm
  3. Deviation calculating the output layer
  4. Reverse reverse thrust algorithm using the deviation of the individual layers
  5. Using Equation 1569070376715deviation and adding
  6. Using equation 1569070417565calculating D {i, j}
  7. It can be shown 1569070448297, so that we can use the gradient descent method to calculate the θ

Guess you like

Origin www.cnblogs.com/jiading/p/11570191.html