辅助函数

牛顿法介绍

 %% 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的更多相关文章

  1. 机器学习方法(五):逻辑回归Logistic Regression,Softmax Regression

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 前面介绍过线性回归的基本知识, ...

  2. Softmax回归(Softmax Regression)

    转载请注明出处:http://www.cnblogs.com/BYRans/ 多分类问题 在一个多分类问题中,因变量y有k个取值,即.例如在邮件分类问题中,我们要把邮件分为垃圾邮件.个人邮件.工作邮件 ...

  3. TensorFlow实战之Softmax Regression识别手写数字

         关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...

  4. R︱Softmax Regression建模 (MNIST 手写体识别和文档多分类应用)

    本文转载自经管之家论坛, R语言中的Softmax Regression建模 (MNIST 手写体识别和文档多分类应用) R中的softmaxreg包,发自2016-09-09,链接:https:// ...

  5. TensorFlow(2)Softmax Regression

    Softmax Regression Chapter Basics generate random Tensors Three usual activation function in Neural ...

  6. 逻辑回归与神经网络还有Softmax regression的关系与区别

    本文讨论的关键词:Logistic Regression(逻辑回归).Neural Networks(神经网络) 之前在学习LR和NN的时候,一直对它们独立学习思考,就简单当做是机器学习中的两个不同的 ...

  7. 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression

    Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...

  8. 2.1、Softmax Regression模型

    Softmax Regression模型 由于Logistics Regression算法复杂度低,容易实现等特点,在工业中的到广泛的使用,但是Logistics Regression算法主要用于处理 ...

  9. 基于MNIST数据的softmax regression

    跟着tensorflow上mnist基本机器学习教程联系 首先了解sklearn接口: sklearn.linear_model.LogisticRegression In the multiclas ...

随机推荐

  1. BroadcastReceiver详解(一)

    今天我们来讲一下Android中BroadcastReceiver的相关知识. BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在And ...

  2. 转: Code Review 程序员的寄望与哀伤

    转自: http://www.cnblogs.com/mindwind/p/5639008.html 一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产 ...

  3. Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf

    Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensi ...

  4. 高速清除winXP系统中explorer.exe病毒

    关于这个explorer.exe病毒.是眼下xp最为常见的一个病毒,会大量的消耗系统资源,造成电脑特别的卡顿. 1.关闭还原(假设没有,则跳过),为的是防止我们改动后,还原之后又回来了. 2.打开注冊 ...

  5. 自己定义控件事实上非常easy1/6

    尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节我们粗略地讲了下怎样去实现我 ...

  6. C# 通过比对哈希码判断两个文件内容是否相同

    1.使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否一致. 2. 在比较文件内容的时候可以采用好几种方法.例如 ...

  7. C#语言 ArrayList集合

  8. Ubuntuserver版安装

          近期因为工作的须要.又一次部署server.安装了Ubuntuserver版本号,依据当时遇到的一些问题,整理了下,为方便以后的使用做个记录.       因为直接安装server端.无法 ...

  9. Xammp修改端口

    How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...

  10. 编程算法 - 数组中出现次数超过一半的数字 代码(C)

    数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...