LR cost function is given by:
Cost(hθ(x),y)={−log(hθ(x))if y=1−log(1−hθ(x))if y=0
where hθ(x)=11+e−θTx is the logistic function.
Since y∈{0,1} only, we can reduce the cost function to an equivalent, single equation.
Cost(hθ(x),y)=−ylog(hθ(x))−(1−y)log(1−hθ(x))
This leads to the overall cost function for the logistic regression:
J(θ)=−1m[m∑i=1y(i)loghθ(x(i))+(1−y(i))log(1−hθ(x))]
Our goal is to find minθJ(θ). To do so, we use gradient descent, but we first need to find the partial derivatives ∂∂θjJ(θ).
We're going to make use of a neat property of the logistic function:
g′(z)=ddz11+e−z=1(1+e−z)2e−z=1+e−z−1(1+e−z)2=11+e−z−1(1+e−z)2=11+e−z(1−11+e−z)=g(z)(1−g(z))
So for our with our cost function:
∂∂θjJ(θ)=−1m[∂∂θjm∑i=1y(i)loghθ(x(i))+(1−y(i))log(1−hθ(x))]=−1m[m∑i=1y(i)1hθ(x(i))∂∂θjhθ(x(i))+(1−y(i))11−hθ(x(i))(−∂∂θjhθ(x(i)))]
using the chain rule and the logistic regression derivative, we see that
∂∂θjJ(θ)=−1m[m∑i=1y(i)x(i)jhθ(x(i))hθ(x(i))(1−hθ(x(i)))−(1−y(i))x(i)j1−hθ(x(i))hθ(x)(1−hθ(x(i)))]=−1m[m∑i=1y(i)x(i)j(1−hθ(x(i)))−(1−y(i))x(i)jhθ(x(i)))]=−1m[m∑i=1y(i)x(i)j−xjhθ(x(i))]∂∂θjJ(θ)=1mm∑i=1(hθ(x(i))−y(i))x(i)j
∂∂θjJ(θ)=−1m[m∑i=1y(i)x(i)jhθ(x(i))hθ(x(i))(1−hθ(x(i)))−(1−y(i))x(i)j1−hθ(x(i))hθ(x)(1−hθ(x(i)))]=−1m[m∑i=1y(i)x(i)j(1−hθ(x(i)))−(1−y(i))x(i)jhθ(x(i)))]=−1m[m∑i=1y(i)x(i)j−xjhθ(x(i))]∂∂θjJ(θ)=1mm∑i=1(hθ(x(i))−y(i))x(i)j
This formula can now be used in gradient descent.
Thank you! It is very useful for me!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteFuck me, I don't understand the calculus :(
ReplyDeletewhich part of the calculus is unclear?
Deletethe d/dx thing, isn't it 1/x if the d gets canceled out?
DeleteThanks for showing the derivation!
ReplyDeleteits really good work, thank you so much
ReplyDeleteTruly appreciate your effort , well formatted and explained. Thank you!
ReplyDeleteNice description!
ReplyDeleteCan someone explain how chain rule is implemented?
ReplyDeleteAwesome, Thanks you guys. I was confused on this for a while , it's really a great help to me.
ReplyDeleteReally really thank you!!
ReplyDeletehello, Thanks for this post, How did you come about the Xj ?
ReplyDelete