深度学习 Deep LearningUFLDL 最新Tutorial 学习笔记 2:Logistic Regression
1 Logistic Regression 简述
Specifically, we will try to learn a function of the form:
The function σ(z)≡11+exp(−z) is often called the “sigmoid” or “logistic” function
我们仅仅须要计算y=1的概率就ok了。其Cost Function例如以下:
J(θ)=−∑i(y(i)log(hθ(x(i)))+(1−y(i))log(1−hθ(x(i)))).
除了方程不一样,其它的计算和Linear Regression是全然一样的。
OK,接下来我们来看看练习怎么做。
2 exercise1B 解答
addpath ../common
addpath ../common/minFunc_2012/minFunc
addpath ../common/minFunc_2012/minFunc/compiled % Load the MNIST data for this exercise.
% train.X and test.X will contain the training and testing images.
% Each matrix has size [n,m] where:
% m is the number of examples.
% n is the number of pixels in each image.
% train.y and test.y will contain the corresponding labels (0 or 1).
binary_digits = true;
[train,test] = ex1_load_mnist(binary_digits); % Add row of 1s to the dataset to act as an intercept term.
train.X = [ones(1,size(train.X,2)); train.X];
test.X = [ones(1,size(test.X,2)); test.X]; % Training set dimensions
m=size(train.X,2);
n=size(train.X,1); % Train logistic regression classifier using minFunc
options = struct('MaxIter', 100); % First, we initialize theta to some small random values.
theta = rand(n,1)*0.001; % Call minFunc with the logistic_regression.m file as the objective function.
%
% TODO: Implement batch logistic regression in the logistic_regression.m file!
%
%tic;
%theta=minFunc(@logistic_regression, theta, options, train.X, train.y);
%fprintf('Optimization took %f seconds.\n', toc); % Now, call minFunc again with logistic_regression_vec.m as objective.
%
% TODO: Implement batch logistic regression in logistic_regression_vec.m using
% MATLAB's vectorization features to speed up your code. Compare the running
% time for your logistic_regression.m and logistic_regression_vec.m implementations.
%
% Uncomment the lines below to run your vectorized code.
%theta = rand(n,1)*0.001;
tic;
theta=minFunc(@logistic_regression_vec, theta, options, train.X, train.y);
fprintf('Optimization took %f seconds.\n', toc); % Print out training accuracy.
tic;
accuracy = binary_classifier_accuracy(theta,train.X,train.y);
fprintf('Training accuracy: %2.1f%%\n', 100*accuracy); % Print out accuracy on the test set.
accuracy = binary_classifier_accuracy(theta,test.X,test.y);
fprintf('Test accuracy: %2.1f%%\n', 100*accuracy);
function [f,g] = logistic_regression(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);
n=size(X,1); % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); %
% TODO: Compute the objective function by looping over the dataset and summing
% up the objective values for each example. Store the result in 'f'.
%
% TODO: Compute the gradient of the objective by looping over the dataset and summing
% up the gradients (df/dtheta) for each example. Store the result in 'g'.
%
%%% YOUR CODE HERE %%% % Step 1?Compute Cost Function for i = 1:m
f = f - (y(i)*log(sigmoid(theta' * X(:,i))) + (1-y(i))*log(1-...
sigmoid(theta' * X(:,1))));
end for j = 1:n
for i = 1:m
g(j) = g(j) + X(j,i)*(sigmoid(theta' * X(:,i)) - y(i));
end end
function [train, test] = ex1_load_mnist(binary_digits) % Load the training data
X=loadMNISTImages('train-images-idx3-ubyte'); % 784x60000 60000张图片28x28pixel
y=loadMNISTLabels('train-labels-idx1-ubyte')'; % 1*60000 if (binary_digits)
% Take only the 0 and 1 digits
X = [ X(:,y==0), X(:,y==1) ]; %通过y==0和y==1直接得到y=0和1的index
y = [ y(y==0), y(y==1) ];
end % Randomly shuffle the data
I = randperm(length(y));
y=y(I); % labels in range 1 to 10
X=X(:,I); % We standardize the data so that each pixel will have roughly zero mean and unit variance.
s=std(X,[],2); %?? std??X??? m=mean(X,2);
X=bsxfun(@minus, X, m);
X=bsxfun(@rdivide, X, s+.1); % 就是计算(x-m)/s 加0.1是为了防止分母为0 % Place these in the training set
train.X = X;
train.y = y; % Load the testing data
X=loadMNISTImages('t10k-images-idx3-ubyte');
y=loadMNISTLabels('t10k-labels-idx1-ubyte')'; if (binary_digits)
% Take only the 0 and 1 digits
X = [ X(:,y==0), X(:,y==1) ];
y = [ y(y==0), y(y==1) ];
end % Randomly shuffle the data
I = randperm(length(y));
y=y(I); % labels in range 1 to 10
X=X(:,I); % Standardize using the same mean and scale as the training data.
X=bsxfun(@minus, X, m);
X=bsxfun(@rdivide, X, s+.1); % Place these in the testing set
test.X=X;
test.y=y;
【说明:本文为原创文章,转载请注明出处:blog.csdn.net/songrotek 欢迎交流QQ:363523441】
深度学习 Deep LearningUFLDL 最新Tutorial 学习笔记 2:Logistic Regression的更多相关文章
- (转) 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ
特别棒的一篇文章,仍不住转一下,留着以后需要时阅读 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...
- Stanford机器学习笔记-2.Logistic Regression
Content: 2 Logistic Regression. 2.1 Classification. 2.2 Hypothesis representation. 2.2.1 Interpretin ...
- 深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression
1 前言 Andrew Ng的UFLDL在2014年9月底更新了. 对于開始研究Deep Learning的童鞋们来说这真的是极大的好消息! 新的Tutorial相比旧的Tutorial添加了Conv ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
1 Vectorization 简述 Vectorization 翻译过来就是向量化,各简单的理解就是实现矩阵计算. 为什么MATLAB叫MATLAB?大概就是Matrix Lab,最根本的差别于其它 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
1 Gradient Checking 说明 前面我们已经实现了Linear Regression和Logistic Regression.关键在于代价函数Cost Function和其梯度Gradi ...
- 吴恩达深度学习:2.9逻辑回归梯度下降法(Logistic Regression Gradient descent)
1.回顾logistic回归,下式中a是逻辑回归的输出,y是样本的真值标签值 . (1)现在写出该样本的偏导数流程图.假设这个样本只有两个特征x1和x2, 为了计算z,我们需要输入参数w1.w2和b还 ...
- Coursera台大机器学习课程笔记9 -- Logistic Regression
如果只想得到某种概率,而不是简单的分类,那么该如何做呢?在误差衡量问题上,如何选取误差函数这段很有意思. 接下来是如何最小化Ein,由于Ein是可凸优化的,所以采用的是梯度下降法:只要达到谷底,就找到 ...
- Coursera台大机器学习技法课程笔记05-Kernel Logistic Regression
这一节主要讲的是如何将Kernel trick 用到 logistic regression上. 从另一个角度来看soft-margin SVM,将其与 logistic regression进行对比 ...
随机推荐
- OpenJDK源码研究笔记(六)--观察者模式工具类(Observer和Observable)和应用示例
本文主要讲解OpenJDK观察者模式的2个工具类,java.util.Observer观察者接口,java.util.Observable被观察者基类. 然后,给出了一个常见的观察者应用示例. Obs ...
- 【Henu ACM Round#19 A】 Vasya the Hipster
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题. 两个一起用->min(a,b); 剩下的除2加上去就好 [代码] #include <bits/stdc++. ...
- POJ——T 1988 Cube Stacking
http://poj.org/problem?id=1988 Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 25865 ...
- COGS——T 2478. [HZOI 2016]简单的最近公共祖先
http://www.cogs.pro/cogs/problem/problem.php?pid=2478 ★☆ 输入文件:easy_LCA.in 输出文件:easy_LCA.out 简单 ...
- CSS浏览器兼容问题集(一)
CSS对浏览器的兼容性有时让人非常头疼,也许当你了解其中的技巧跟原理,就会认为也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理方法并整理了一下.对于web2.0的过度,请尽量用xhtml ...
- 热门游戏<开心消消乐>的“加壳”诡计!!
好久没搞游戏了,前几天看了又又一次看了看<开心消消乐>的1.29最新版..于是故事開始了: 1.反编译分析 首先使用Androidkiller进行反编译,得到两个Smali代码目录:mal ...
- 20.发送http请求服务 ($http)
转自:https://www.cnblogs.com/best/tag/Angular/ 服务从代码直接与服务器进行交互,底层是通过实现,与中http服务从AngularJS代码直接与Web服务器进行 ...
- C++中explicit关键字作用
explicit是c++中不太常用的一个关键字,其作用是用于修饰构造函数,告诉编译器定义对象时不做隐式转换. 举例说明: include <iostream> include <st ...
- 轻量级记事本工具:CintaNotes
本片文章介绍CintaNotes小工具 功能介绍: 方便.快捷的记录笔记: 快捷地从任何地方等截取内容生成笔记: 高效的记事本内容查询: 轻松的标签管理 支持移动设备和电脑同步(估计要收费) 官网下载 ...
- ps切图时常用的操作与快捷键
一:两种切片方法 第一种: 1.使用切片工具划分好你要切的模块 2.点击'存储为web所有格式',在存储之前可以修改图片的品质来改变文件的大小. 3.在存储时切片有三种选择方式,按照自己的需要选择. ...