UFLDL學(xué)習(xí)筆記:Logistic Regression

導(dǎo)師介紹的課程UFLDL(Unsupervised Feature Learning and Deep Learning)。前一段時間寫了很多tensorflow方面的代碼,但感覺在基礎(chǔ)知識方面還是有很多不足,因此重新?lián)炱疬@個課程來補(bǔ)補(bǔ)。

第一課講的是Linear Regression,沒有做筆記就暫且挑過了。

本節(jié)課講的是Logistic Regression。Logistic Regression其實(shí)是softmax 的一種。

在Logistic Regression中,使用sigmoid函數(shù),將feature的值約束到0,1之間。

cost function

image.png

很容易可以化成矩陣的形式。

cost function對theta求導(dǎo),可以表示為:

image.png

化為矩陣容易表示的形式:

image.png

接下來介紹下作業(yè)的答案。
作業(yè)是對MNIST數(shù)據(jù)集總手寫的0和1進(jìn)行識別。

ex1\logistic_regression_vec.m

function [f,g] = logistic_regression_vec(theta, X,y)
  %
  % Arguments:
  %   theta - A column vector containing the parameter values to optimize.
  %   X - The examples stored in a matrix.  
  %       X(i,j) is the i'th coordinate of the j'th example.
  %   y - The label for each example.  y(j) is the j'th example's label.
  %
  m=size(X,2);
  
  % initialize objective value and gradient.
  f = 0;
  g = zeros(size(theta));
  
prob = sigmoid(theta'*X);

f =  -log(prob) * y' - log(1-prob) * (1-y)';
g = X * (prob-y)';
  %
  % TODO:  Compute the logistic regression objective function and gradient 
  %        using vectorized code.  (It will be just a few lines of code!)
  %        Store the objective function value in 'f', and the gradient in 'g'.
  %
%%% YOUR CODE HERE %%%

其中比較能夠體現(xiàn)矩陣運(yùn)算好處的部分就是y'或者(1-y)',直接將sum的功能包括在了矩陣運(yùn)算中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容