%
% Liscense Notification 2013
%
% Redistributions in source and binary forms, with or without
% modification, are totally FREE provided that you keep this keep this
% notification
%
% Descritption: This package demostrates how to use the logistic
% regression for classfication of 2D data.
% Author: Ranch Lai (ranchlai@hotmail.com)
% Release date: Aug 17, 2013
%/
function [w,b,cost] = logistic_train(x,y,tol,max_iter)
fprintf('training started...\n');
n = size(x,1);
N = size(x,2);
w = ones(n,1)/n;
b = 1;
cost = [];
count = 1;
while 1
%find gradient
partial_w = zeros(n,1);
partial_b = 0;
for i=1:N
a = exp(w'*x(:,i)+b);
partial_w = partial_w + (1/(a+1)-y(i))*(-1)/((1+a)*(1+a))*a*x(:,i);
partial_b = partial_b + (1/(a+1)-y(i))*(-1)/((1+a)*(1+a))*a;
end
old_cost = logistic_cost(x,y,w,b);
step = 1;
while step > 1e-12
w1 = w - step*partial_w;
b1 = b - step*partial_b;
new_cost = logistic_cost(x,y,w1,b1);
if new_cost < old_cost
break;
end
step = step * 0.1;
end
if step <= 1e-12
fprintf('finished seraching the step size\n');
break;
end
w = w1;
b = b1;
cost = [cost,new_cost];
if new_cost < tol
fprintf('converged after %d iterates!',count);
break;
end
if count > max_iter
fprintf('training stoped after %d iterates, not converged to desired precision!',count);
break;
end
count = count + 1;
end
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
2d logistic regression demo.tar.gz (8个子文件)
2d logistic regression demo
logistic_cost.m 520B
logistic_predict.m 504B
demo_logistic.m 1KB
logistic_train.m 2KB
figure
convergence.png 4KB
classification.png 14KB
logisticfunction.png 5KB
trainingdata.png 7KB
共 8 条
- 1
消失的苦楝树Ranchlai
- 粉丝: 62
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页