逻辑回归(二)

代价函数(Cost Function)

对于线性回归模型,我们定义的代价函数J(θ)为:

现在对于逻辑回归模型我们沿用此定义,但问题是hθ(x) = g(z),而函数g为S形函数,故代价函数J(θ)将会变为像下图中左边的图那样,此时我们将其称之为非凸函数(non-convex function)。

注:国外凸函数定义与国内是相反的。

这意味着代价函数J(θ)存在无数个局部最小值,从而影响梯度下降算法的运行。为了将代价函数J(θ)变为像上图中右边的图那样,我们想要将代价函数J(θ)重新定义为:

其中

hθ(x)与Cost(hθ(x), y)之间的函数关系如下图所示:

y=1:

  • hθ(x) -> 1,则Cost(hθ(x), y) = 0;
  • hθ(x) -> 0,则Cost(hθ(x), y) -> ∞。

y=0:

  • hθ(x) -> 0,则Cost(hθ(x), y) = 0;
  • hθ(x) -> 1,则Cost(hθ(x), y) -> ∞。
补充笔记
Cost Function

We cannot use the same cost function that we use for linear regression because the Logistic Function will cause the output to be wavy, causing many local optima. In other words, it will not be a convex function.

Instead, our cost function for logistic regression looks like:

When y = 1, we get the following plot for J(θ) vs hθ(x):

Similarly, when y = 0, we get the following plot for J(θ) vs hθ(x):

If our correct answer 'y' is 0, then the cost function will be 0 if our hypothesis function also outputs 0. If our hypothesis approaches 1, then the cost function will approach infinity.

If our correct answer 'y' is 1, then the cost function will be 0 if our hypothesis function outputs 1. If our hypothesis approaches 0, then the cost function will approach infinity.

Note that writing the cost function in this way guarantees that J(θ) is convex for logistic regression.

梯度下降算法

之前为了得到一个凸函数(国内为凹函数),我们重新定义代价函数J(θ)为:

对于Cost(hθ(x), y)函数而言,它在y=1和y=0时有不同的函数表达式。为此我们可以将其合并为如下表达式:

因此,代价函数J(θ)可以简化为:

既然有了上述的代价函数代价函数J(θ),我们就要使用梯度下降算法来求得参数θ使得代价函数J(θ)最小化。

梯度下降算法

为了方便推导出迭代表达式,我们假设在单个样本下的代价函数J(θ)为:

则:

其中:

注:上述推导过程摘自bitcarmanlee博主的logistic回归详解(二):损失函数(cost function)详解logistic回归详解(三):梯度下降训练方法这两篇文章。谢谢!

因此对于全体样本其梯度下降算法为:

虽然该表达式和线性回归模型下的梯度下降算法表达式相同,但其hθ(x)是不同的。逻辑回归模型下的假设函数hθ(x)为:

在使用梯度下降算法时,我们仍然推荐将参数θ和特征变量x向量化,其表达式为:

补充笔记
Simplified Cost Function and Gradient Descent

We can compress our cost function's two conditional cases into one case:

Notice that when y is equal to 1, then the second term (1−y)log⁡(1−hθ(x)) will be zero and will not affect the result. If y is equal to 0, then the first term −ylog⁡(hθ(x)) will be zero and will not affect the result.

We can fully write out our entire cost function as follows:

A vectorized implementation is:

Gradient Descent

Remember that the general form of gradient descent is:

We can work out the derivative part using calculus to get:

Notice that this algorithm is identical to the one we used in linear regression. We still have to simultaneously update all values in theta.

A vectorized implementation is:

高级优化

除了我们之前学习的梯度下降算法之外,还有一些高级的算法可以替代梯度下降算法使得代价函数J(θ)最小化,例如:共轭梯度算法(Conjugate Gradient)、局部优化算法(Broyden fletcher goldfarb shann,BFGS)和有限内存局部优化算法(LBFGS)。

在Octave或MATLAB中,我们可以使用fminunc()函数,该函数可以用来求解无约束非线性问题的最小值。

例如:

如上图所示,我们假设代价函数J(θ) = (θ1 - 5)2 + (θ2 - 5)2,现我们希望求出参数θ的值使得代价函数J(θ)最小化。

首先,我们创建名为costFunction.m的文件并添加如下代码:

function [jVal, gradient] = costFunction(theta)

jVal = (theat(1) - 5)^2 + (theta(2) - 5)^2;

gradient = zeros(2, 1);
gradient(1) = 2 * (theta(1) - 5);
gradient(2) = 2 * (theta(2) - 5);

代码注释为:

然后,我们在Octave中键入如下命令:

octave:3> options = optimset('GradObj', 'on', 'MaxIter', '100');
octave:4> initialTheta = zeros(2, 1);
octave:5> [optTheta, functionVal, exitFlag] = fminunc(@costFunction, initialTheta, options)
optTheta =

   5.0000
   5.0000

functionVal =   1.5777e-030
exitFlag =  1

其中fminunc函数中@表示指向costFunction.m文件中costFunction函数的指针,exitFlag = 1表示函数已经收敛找到了使得代价函数J(θ)最小化的参数θ的值。

补充说明(此处谢谢Giacche在评论区的补充):

fminunc表示Octave里无约束最小化函数,调用这个函数时,需要传入一个存有配置信息的变量options。

options = optimset('GradObj', 'on', 'MaxIter', '100');

上面的代码中,我们的设置项中’GradObj’, ‘on’,代表设置梯度目标参数为打开状态(on)。’MaxIter’,‘100’代表设置最大迭代次数为100次。

initialTheta = zeros(2,1);

initialTheta代表我们给出的一个θ的猜测初始值。

当我们调用这个fminunc函数时,它会自动的从众多高级优化算法中挑选一个来使用(你也可以把它当做一个可以自动选择合适的学习速率aa的梯度下降算法)。

最终我们会得到三个返回值,分别是满足最小化代价函数J(θ)的θ值optTheta,costFunction中定义的jVal的值functionVal,以及标记是否已经收敛的状态值exitFlag,如果已收敛,标记为1,否则为0。

补充笔记
Advanced Optimization

"Conjugate gradient", "BFGS", and "L-BFGS" are more sophisticated, faster ways to optimize θ that can be used instead of gradient descent. We suggest that you should not write these more sophisticated algorithms yourself (unless you are an expert in numerical computing) but use the libraries instead, as they're already tested and highly optimized. Octave provides them.

We first need to provide a function that evaluates the following two functions for a given input value θ:

We can write a single function that returns both of these:

function [jVal, gradient] = costFunction(theta)
  jVal = [...code to compute J(theta)...];
  gradient = [...code to compute derivative of J(theta)...];
end

Then we can use octave's "fminunc()" optimization algorithm along with the "optimset()" function that creates an object containing the options we want to send to "fminunc()".

options = optimset('GradObj', 'on', 'MaxIter', 100);
initialTheta = zeros(2,1);
   [optTheta, functionVal, exitFlag] = fminunc(@costFunction, initialTheta, options);

We give to the function "fminunc()" our cost function, our initial vector of theta values, and the "options" object that we created beforehand.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容