導(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)算中。