function w = mylogreg(data, y, reg_type)
% TRAIN function for logistic regression.
% data is the matrix of predictors, with rows corresponding to examples
% and columns the predictors
% y is the column of corresponding class. I presume the values of y are 0
% or 1
% w is the vector of weights. I assume that the caller has added a vector
% of ones to data as a dummy variable
% reg_str is the regularization strength (DO NOT CHANGE)
% To train, I use simple gradient ascend as described in the slides.
% m examples, n predictors
[~, n] = size(data);
% Parameters for the optimization
tolerance = 1e-08;
reg_str = 1e-05;
% Number of iterations
maxIters = 2000;
% w is a column vector
w = zeros(n, 1) + 0.01;
iter = 0;
% Gradient ascent
[gr, LL] = loglikelihood(data, y, w, reg_str, reg_type);
if reg_type == 1
normGr = norm(gr,1);
else
normGr = norm(gr);
end
while normGr > tolerance && iter <= maxIters
iter = iter + 1;
fprintf('iter = %7d, LL = %10f, gr norm = %10f\n', iter, LL, normGr);
w = w + reg_str * gr;
[gr, LL] = loglikelihood(data, y, w, reg_str, reg_type);
if reg_type == 1
normGr = norm(gr,1);
else
normGr = norm(gr);
end
end
end
function [gr, LL] = loglikelihood(data, y, w, reg_str, reg_type)
[~, n] = size(data);
% Computes the gradient and the log likelihood of the data for the
% given w as in slide 9
% notice, this is an inner function and so data and y are variables of the
% main function, displayed in teal
% Inners products, we'll need them
prods = data * w;
% The computation is in slide 9
% Iteratively
% for l = 1:m
% LL(l) = y(l) .* prods(l) - log(1 + exp(prods(l)));
% end
%
% LL = sum(LL)
% Vectorized
LL = sum(y .* prods - log(1 + exp(1).^prods));
% Iteratively
% for l=1:m
% for i=1:n
% gr(l,i) = data(l,i) * (y(l) - 1 / (1+exp(-prods(l))));
% end
% end
%
% gr = sum(gr);
% gr = gr'
% Vectorized
% if reg_type == 0 then no regularization will take place
reg = zeros([n 1]);
if reg_type==1
%%-------L1 regularization------%%
% reg = fill this
%%------------------------------%%
elseif reg_type==2
%%-------L1 regularization------%%
% reg = fill this
%%------------------------------%%
end
%--You should not regularize over the intercept--%
reg(1) = 0;
%------------------------------------------------%
gr = data .* repmat((y - (1+exp(1) .^ (- prods)).^(-1)), 1, n);
gr = sum(gr);
gr = gr'+reg_str*reg;
end
没有合适的资源?快使用搜索试试~ 我知道了~
data_and_functions.zip_And Yet ..._ZEV_matlab
共4个文件
m:2个
mat:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 171 浏览量
2022-09-20
11:11:29
上传
评论
收藏 2.96MB ZIP 举报
温馨提示
yet another matlab still
资源推荐
资源详情
资源评论
收起资源包目录
data_and_functions.zip (4个子文件)
mylogreg.m 3KB
realData.mat 2.96MB
logregtest.m 1KB
randomdata.mat 2KB
共 4 条
- 1
资源评论
钱亚锋
- 粉丝: 89
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功