Logistic/Softmax Regression
辅助函数




牛顿法介绍




%% Logistic Regression
close all
clear %%load data
x = load('ex4x.dat');
y = load('ex4y.dat'); [m, n] = size(x); % Add intercept term to x
x = [ones(m, ), x]; %%draw picture
% find returns the indices of the
% rows meeting the specified condition
pos = find(y == );
neg = find(y == );
% Assume the features are in the 2nd and 3rd
% columns of x
figure('NumberTitle', 'off', 'Name', 'GD');
plot(x(pos, ), x(pos,), '+');
hold on;
plot(x(neg, ), x(neg, ), 'o'); % Define the sigmoid function
g = inline('1 ./ (1 + exp(-z))'); alpha = 0.001;
theta = [-,,]';
obj_old = 1e10;
tor = 1e-; tic %%Gradient Descent
for time = :
delta = zeros(,);
objective = ; for i = :
z = x(i,:) * theta;
h = g(z);%转换成logistic函数
delta = (/m) .* x(i,:)' * (y(i)-h) + delta;
objective = (/m) .*( -y(i) * log(h) - (-y(i)) * log(-h)) + objective;
end
theta = theta + alpha * delta; fprintf('objective is %.4f\n', objective);
if abs(obj_old - objective) < tor
fprintf('torlerance is samller than %.4f\n', tor);
break;
end
obj_old = objective;
end %%Calculate the decision boundary line
plot_x = [min(x(:,)), max(x(:,))];
plot_y = (-./theta()).*(theta().*plot_x +theta());
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off
toc
pause();
%%SGD figure('NumberTitle', 'off', 'Name', 'SGD');
plot(x(pos, ), x(pos,), '+');
hold on;
plot(x(neg, ), x(neg, ), 'o'); alpha = 0.001;
theta = [-,,]';
obj_old = 1e10;
tor = 1e-;
k=;
U=ceil(m/k); for time = :
delta = zeros(,);
rand('twister',time*);
idx=randperm(m);
objective = ; subidx=idx(:k);
for i=:length(subidx)
z = x(subidx(i),:) * theta;
h = g(z);%转换成logistic函数
delta = (/k) .* x(subidx(i),:)' * (y(subidx(i))-h) + delta;
objective = (/k) .*( -y(subidx(i)) * log(h) - (-y(subidx(i))) * log(-h)) + objective;
end
theta = theta + alpha * delta; fprintf('objective is %.4f\n', objective);
if abs(obj_old - objective) < tor
fprintf('torlerance is samller than %.4f\n', tor);
break;
end
obj_old = objective;
end %%Calculate the decision boundary line
plot_x = [min(x(:,)), max(x(:,))];
plot_y = (-./theta()).*(theta().*plot_x +theta());
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off
toc
pause() %%Newton's method figure('NumberTitle', 'off', 'Name', 'Newton');
plot(x(pos, ), x(pos,), '+');
hold on;
plot(x(neg, ), x(neg, ), 'o'); alpha = 0.001;
theta = zeros(, );
obj_old = 1e10;
tor = 1e-; for i = :
delta = zeros(,);
delta_H = zeros(,);
objective = ;
% Calculate the hypothesis function
for i = :
z = x(i,:) * theta;
h = g(z);%转换成logistic函数
delta = (/m) .* x(i,:)' * (h-y(i)) + delta;
delta_H = (/m).* x(i,:)' * h * (1-h) * x(i,:) + delta_H;
objective = (/m) .*( -y(i) * log(h) - (-y(i)) * log(-h)) + objective;
end
theta = theta - delta_H\delta;
fprintf('objective is %.4f\n', objective);
if abs(obj_old - objective) < tor
fprintf('torlerance is samller than %.4f\n', tor);
break;
end
obj_old = objective;
end %%Calculate the decision boundary line
plot_x = [min(x(:,)), max(x(:,))];
plot_y = (-./theta()).*(theta().*plot_x +theta());
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off
toc
%% Softmax Regression
close all
clear %%load data
load('my_ex4x.mat');
load('my_ex4y.mat'); [m, n] = size(x); % Add intercept term to x
x = [ones(m, ), x];
y = y + ; class_num = max(y);
n = n + ; %%draw picture
% find returns the indices of the
% rows meeting the specified condition
class2 = find(y == );
class1 = find(y == );
class3 = find(y == );
% Assume the features are in the 2nd and 3rd
% columns of x
figure('NumberTitle', 'off', 'Name', 'GD');
plot(x(class2, ), x(class2,), '+');
hold on;
plot(x(class1, ), x(class1, ), 'o');
hold on;
plot(x(class3, ), x(class3, ), '*');
hold on; % Define the sigmoid function
g = inline('exp(z) ./ sumz','z','sumz'); alpha = 0.0001;
theta = [-,0.15,0.14;-,,-]';
obj_old = 1e10;
tor = 1e-; %%Gradient Descent
for time = :
delta = zeros(,);
objective = ; for i = :
for j = :
z = x(i,:) * theta(:,j);
sumz = exp(x(i,:) * theta(:,)) + exp(x(i,:) * theta(:,)) + ;
h = g(z,sumz);%转换成logistic函数
if y(i)==j
delta = (/m) .* x(i,:)' * (1-h);
theta(:,j) = theta(:,j) + alpha * delta;
objective = (/m) .*(-y(i) * log(h)) + objective;
else
delta = (/m) .* x(i,:)' * (-h);
theta(:,j) = theta(:,j) + alpha * delta;
objective = (/m) .*(-(-y(i)) * log(-h)) + objective;
end
end
end fprintf('objective is %.4f\n', objective);
if abs(obj_old - objective) < tor
fprintf('torlerance is samller than %.4f\n', tor);
break;
end
obj_old = objective;
end %%Calculate the decision boundary line
plot_x = [min(x(:,)), max(x(:,))];
plot_y = (-./theta(,)).*(theta(,).*plot_x +theta(,));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold on plot_y = (-./theta(,)).*(theta(,).*plot_x +theta(,));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off
Logistic/Softmax Regression的更多相关文章
- 机器学习方法(五):逻辑回归Logistic Regression,Softmax Regression
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 前面介绍过线性回归的基本知识, ...
- Softmax回归(Softmax Regression)
转载请注明出处:http://www.cnblogs.com/BYRans/ 多分类问题 在一个多分类问题中,因变量y有k个取值,即.例如在邮件分类问题中,我们要把邮件分为垃圾邮件.个人邮件.工作邮件 ...
- TensorFlow实战之Softmax Regression识别手写数字
关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...
- R︱Softmax Regression建模 (MNIST 手写体识别和文档多分类应用)
本文转载自经管之家论坛, R语言中的Softmax Regression建模 (MNIST 手写体识别和文档多分类应用) R中的softmaxreg包,发自2016-09-09,链接:https:// ...
- TensorFlow(2)Softmax Regression
Softmax Regression Chapter Basics generate random Tensors Three usual activation function in Neural ...
- 逻辑回归与神经网络还有Softmax regression的关系与区别
本文讨论的关键词:Logistic Regression(逻辑回归).Neural Networks(神经网络) 之前在学习LR和NN的时候,一直对它们独立学习思考,就简单当做是机器学习中的两个不同的 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...
- 2.1、Softmax Regression模型
Softmax Regression模型 由于Logistics Regression算法复杂度低,容易实现等特点,在工业中的到广泛的使用,但是Logistics Regression算法主要用于处理 ...
- 基于MNIST数据的softmax regression
跟着tensorflow上mnist基本机器学习教程联系 首先了解sklearn接口: sklearn.linear_model.LogisticRegression In the multiclas ...
随机推荐
- Docker 的CMD与ENTRYPOINT区别
我们在构建一个docker镜像的时候,Dockerfile里面有两个命令会引起我们的注意,它们就是 CMD 和 ENTRYPOINT,看起来很相似,实际上并非如此. 一.CMD 顾名思义就是允许用户指 ...
- 常用Git命令手册
常用Git命令手册 此文只是对Git有一定基础的人当记忆使用,比较简略,初级学员强烈推荐廖雪峰老师的Git系列教程,通俗易懂,戳此处即可开始学习 1.安装Git Linux sudo apt-get ...
- outlook 2010 自动密送Email
以下功能请勿非法使用: 密抄到多人这个需要用到宏 方法一: 1.在Outlook里面键入ALT+F11打开VBA编辑器 2.展开“Project (VbaProject.OTM)/Microsoft ...
- RecyclerView的万能切割线
效果图: 用法: 加入默认切割线:高度为2px.颜色为灰色 mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, Linea ...
- 【基础练习】【线性DP】codevs3027 线段覆盖2题解
文章被盗还是非常严重,加版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看看 这道题目是线性动归 可是思想和背包有些类似 事实上线性动 ...
- HTML--比较实用的小例子
常用的前端实例: 1略 2.在网页商城中的图片当我们把鼠标放上去之后,图片会显示一个有颜色的外边框,图片某一部分的字体的颜色并发生改变 鼠标放上去之前 鼠标放上去之后: 实现的代码: <!DOC ...
- weex 项目开发(六)weexpack 项目 打包、签名、发布
一. weexpack build android 和 weexpack run android 的 区别. (1)单纯打包 weexpack build android (2)打包并运行 wee ...
- 阿里云 ubuntu 14.04 模板上安装 docker
ubuntu 14.04 的内核是 3.13 ,所以内核不用升级. 安装过程例如以下: # apt-get update # apt-get install apt-transport-https # ...
- FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS(转)
转载请注明来自:诗商·柳惊鸿CSDN博客,原文链接:FastDFS的配置.部署与API使用解读(1)入门使用教程 1.背景 FastDFS是一款开源的.分布式文件系统(Distributed File ...
- 【dotnet跨平台】"dotnet restore"和"dotnet run"都做了些什么?
[dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...