转载 Deep learning:六(regularized logistic回归练习)
前言:
在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在logistic回归中的应用,并使用牛顿法来求解模型的参数。参考的网页资料为:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex5/ex5.html。要解决的问题是,给出了具有2个特征的一堆训练数据集,从该数据的分布可以看出它们并不是非常线性可分的,因此很有必要用更高阶的特征来模拟。例如本程序中个就用到了特征值的6次方来求解。
实验基础:
contour:
该函数是绘制轮廓线的,比如程序中的contour(u, v, z, [0, 0], 'LineWidth', 2),指的是在二维平面U-V中绘制曲面z的轮廓,z的值为0,轮廓线宽为2。注意此时的z对应的范围应该与U和V所表达的范围相同。因为contour函数是用来等高线,而本实验中只需画一条等高线,所以第4个参数里面的值都是一样的,这里为[0,0],0指的是函数值z在0和0之间的等高线(很明显,只能是一条)。
在logistic回归中,其表达式为:

在此问题中,将特征x映射到一个28维的空间中,其x向量映射后为:

此时加入了规则项后的系统的损失函数为:

对应的牛顿法参数更新方程为:

其中:

公式中的一些宏观说明(直接截的原网页):

实验结果:
原训练数据点的分布情况:

当lambda=0时所求得的分界曲面:

当lambda=1时所求得的分界曲面:

当lambda=10时所求得的分界曲面:

实验程序代码:
%载入数据
clc,clear,close all;
x = load('ex5Logx.dat');
y = load('ex5Logy.dat'); %画出数据的分布图
plot(x(find(y),1),x(find(y),2),'o','MarkerFaceColor','b')
hold on;
plot(x(find(y==0),1),x(find(y==0),2),'r+')
legend('y=1','y=0') % Add polynomial features to x by
% calling the feature mapping function
% provided in separate m-file
x = map_feature(x(:,1), x(:,2)); [m, n] = size(x); % Initialize fitting parameters
theta = zeros(n, 1); % Define the sigmoid function
g = inline('1.0 ./ (1.0 + exp(-z))'); % setup for Newton's method
MAX_ITR = 15;
J = zeros(MAX_ITR, 1); % Lambda is the regularization parameter
lambda = 1;%lambda=0,1,10,修改这个地方,运行3次可以得到3种结果。 % Newton's Method
for i = 1:MAX_ITR
% Calculate the hypothesis function
z = x * theta;
h = g(z); % Calculate J (for testing convergence)
J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h))+ ...
(lambda/(2*m))*norm(theta([2:end]))^2; % Calculate gradient and hessian.
G = (lambda/m).*theta; G(1) = 0; % extra term for gradient
L = (lambda/m).*eye(n); L(1) = 0;% extra term for Hessian
grad = ((1/m).*x' * (h-y)) + G;
H = ((1/m).*x' * diag(h) * diag(1-h) * x) + L; % Here is the actual update
theta = theta - H\grad; end
% Show J to determine if algorithm has converged
J
% display the norm of our parameters
norm_theta = norm(theta) % Plot the results
% We will evaluate theta*x over a
% grid of features and plot the contour
% where theta*x equals zero % Here is the grid range
u = linspace(-1, 1.5, 200);
v = linspace(-1, 1.5, 200); z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = 1:length(u)
for j = 1:length(v)
z(i,j) = map_feature(u(i), v(j))*theta;%这里绘制的并不是损失函数与迭代次数之间的曲线,而是线性变换后的值
end
end
z = z'; % important to transpose z before calling contour % Plot z = 0
% Notice you need to specify the range [0, 0]
contour(u, v, z, [0, 0], 'LineWidth', 2)%在z上画出为0值时的界面,因为为0时刚好概率为0.5,符合要求
legend('y = 1', 'y = 0', 'Decision boundary')
title(sprintf('\\lambda = %g', lambda), 'FontSize', 14) hold off % Uncomment to plot J
% figure
% plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
% xlabel('Iteration'); ylabel('J')
参考文献:
Deep learning:五(regularized线性回归练习)
作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 欢迎转载或分享,但请务必声明文章出处。
转载 Deep learning:六(regularized logistic回归练习)的更多相关文章
- 转载 Deep learning:四(logistic regression练习)
前言: 本节来练习下logistic regression相关内容,参考的资料为网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage ...
- 转载 Deep learning:一(基础知识_1)
前言: 最近打算稍微系统的学习下deep learing的一些理论知识,打算采用Andrew Ng的网页教程UFLDL Tutorial,据说这个教程写得浅显易懂,也不太长.不过在这这之前还是复习下m ...
- [转载]Deep Learning(深度学习)学习笔记整理
转载自:http://blog.csdn.net/zouxy09/article/details/8775360 感谢原作者:zouxy09@qq.com 八.Deep learning训练过程 8. ...
- 转载 deep learning:八(SparseCoding稀疏编码)
转载 http://blog.sina.com.cn/s/blog_4a1853330102v0mr.html Sparse coding: 本节将简单介绍下sparse coding(稀疏编码),因 ...
- 转载 Deep learning:三(Multivariance Linear Regression练习)
前言: 本文主要是来练习多变量线性回归问题(其实本文也就3个变量),参考资料见网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage. ...
- 转载 Deep learning:七(基础知识_2)
前面的文章已经介绍过了2种经典的机器学习算法:线性回归和logistic回归,并且在后面的练习中也能够感觉到这2种方法在一些问题的求解中能够取得很好的效果.现在开始来看看另一种机器学习算法--神经网络 ...
- 机器学习(六)— logistic回归
最近一直在看机器学习相关的算法,今天学习logistic回归,在对算法进行了简单分析编程实现之后,通过实例进行验证. 一 logistic概述 个人理解的回归就是发现变量之间的关系,也就是求回归系数, ...
- 转载 Deep learning:五(regularized线性回归练习)
前言: 本节主要是练习regularization项的使用原则.因为在机器学习的一些模型中,如果模型的参数太多,而训练样本又太少的话,这样训练出来的模型很容易产生过拟合现象.因此在模型的损失函数中,需 ...
- 转载 Deep learning:二(linear regression练习)
前言 本文是多元线性回归的练习,这里练习的是最简单的二元线性回归,参考斯坦福大学的教学网http://openclassroom.stanford.edu/MainFolder/DocumentPag ...
随机推荐
- ElasticSearch(5)-Mapping
一.Mapping概述 映射 为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确的字符串值,Elasticsearch需要知道每个字段里面都包含了 ...
- Openjudge-计算概论(A)-DNA排序
描述: 给出一系列基因序列,由A,C,G,T四种字符组成.对于每一个序列,定义其逆序对如下: 序列中任意一对字符X和Y,若Y在X的右边(不一定相邻)且Y < X,则称X和Y为一个逆序对. 例如G ...
- Openjudge-计算概论(A)-短信计费
描述: 用手机发短信,一条短信资费为0.1元,但限定一条短信的内容在70个字以内(包括70个字).如果你一次所发送的短信超过了70个字,则会按照每70个字一条短信的限制把它分割成多条短信发送.假设已经 ...
- URL Scheme吊起app实现另外一种登录方式
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/ ...
- csdn如何转载别人的文章
1.找到要转载的文章,用chrome浏览器打开,右键选择审查元素 2.在chrome中下方的框里找到对应的内容,html脚本中找到对应的节点,选中节点,网页上被选中内容会被高亮显示,然后右键菜单选中 ...
- jspl零散知识点
1. 读取配置文件. index.jsp: <body> <% String charset=config.getInitParameter("charset" ...
- tmux commands
最近在学Linux,用到tmux这个命令,看到很多快捷键的介绍,个人觉得不太好用,因此把几个常用的命令记录下来,以便以后学习和使用. 常用tmux commands: tmux ls ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...
- bzoj1977
1977: [BeiJing2010组队]次小生成树 Tree Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3001 Solved: 751[Su ...
- jfreechart 实例
http://blog.csdn.net/ami121/article/category/394379 jfreechart实例(三)股价K线波动图 package com.ami;/** *@ Em ...