% Feature sign search
% code by Wang Jinjun @ NEC Research Lab America
% reference
% Efficient sparse coding algorithms
% Honglak Lee Alexis Battle Rajat Raina Andrew Y. Ng
% Computer Science Department
% Stanford University
% Stanford, CA 94305
%=================HOW TO=================
% B, your feature matrix
% y, observation vector
% lambda, a Lagrange multiplier. You can set to 0.00001, try to tune it to optimize the results.
% init_x is the guessed value of x. You can set it to all zero. Also, try to optimize it.
function [x]=feature_sign(B,y,lambda,init_x)
%%%%%%min_x 0.5\|y-Bx\|_2^2+lambda\|x\|_1
nbases=size(B,2);
OptTol = 1e-5;
if nargin < 4,
x=zeros(nbases, 1);
else
x = init_x;
end;
theta=sign(x); %sign flag
a=(x~=0); %active set
optc=0;
By=B'*y;
B_h=B(:,a);
x_h=x(a);
Bx_h=B_h*x_h;
all_d=2*(B'*Bx_h-By);
[ma mi]=max(abs(all_d).*(~a));
while optc==0,
optc=1;
if all_d(mi)>lambda+1e-10,
theta(mi)=-1;
a(mi)=1;
b=B(:,mi);
x(mi)=(lambda-all_d(mi))/(b'*b*2);
elseif all_d(mi)<-lambda-1e-10,
theta(mi)=1;
a(mi)=1;
b=B(:,mi);
x(mi)=(-lambda-all_d(mi))/(b'*b*2);
else
if sum(a)==0,
lambda=ma-2*1e-10;
optc=0;
b=B(:,mi);
x(mi)=By(mi)/(b'*b);
break;
end
end
opts=0;
B_h=B(:,a);
x_h=x(a);
theta_h=theta(a);
while opts==0,
opts=1;
if size(B_h,2)<=length(y),
BB=B_h'*B_h;
x_new=BB\(B_h'*y-lambda*theta_h/2);
o_new=L1_cost(y,B_h,x_new,lambda);
%cost based on changing sign
s=find(sign(x_new)~=theta_h);
x_min=x_new;
o_min=o_new;
for j=1:length(s),
zd=s(j);
x_s=x_h-x_h(zd)*(x_new-x_h)/(x_new(zd)-x_h(zd));
x_s(zd)=0; %make sure it's zero
o_s=L1_cost(y,B_h,x_s,lambda);
if o_s<o_min,
x_min=x_s;
o_min=o_s;
end
end
else
d=x_h-B_h'*((B_h*B_h')\(B_h*x_h));
q=x_h./(d+eps);
x_min=x_h;
o_min=L1_cost(y,B_h,x_h,lambda);
for j=1:length(q),
zd=q(j);
x_s=x_h-zd*d;
x_s(j)=0; %make sure it's zero
o_s=L1_cost(y,B_h,x_s,lambda);
if o_s<o_min,
x_min=x_s;
o_min=o_s;
end
end
end
x(a)=x_min;
a=(x~=0);
theta=sign(x);
B_h=B(:,a);
x_h=x(a);
theta_h=theta(a);
Bx_h=B_h*x_h;
active_d=2*(B_h'*(Bx_h-y))+lambda*theta_h;
if ~isempty(find(abs(active_d)>OptTol)),
opts=0;
end
end
all_d=2*(B'*Bx_h-By);
[ma mi]=max(abs(all_d).*(~a));
if ma>lambda+OptTol,
optc=0;
end
end
return;
function cost=L1_cost(y,B,x,lambda)
tmp = y-B*x;
cost = tmp'*tmp+lambda*norm(x,1);
return
feature_sign.zip_battle_feature sign _sparse coding_稀疏_稀疏编码
版权申诉
100 浏览量
2022-09-24
22:30:23
上传
评论
收藏 1KB ZIP 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+
最新资源
- Qt开发知识、经验总结 包括Qss,数据库,Excel,Model/View等
- IV数据.xlsx
- foldcraftlauncher_262944.apk
- 珍藏多年的基于matlab实现潮流计算程序源代码集合,包含多个潮流计算程序.rar
- 使用FPGA实现串-并型乘法器
- 基于matlab实现针对基于双曲线定位的DV-Hop算法中误差误差出一种基于加权双曲线定位的DV-Hop改进算法.rar
- 基于matlab实现由遗传算法开发的整数规划,车辆调度问题.rar
- 电视家7.0(对电视配置要求高).apk
- 免费计算机毕业设计-基于JavaEE的医院病历管理系统设计与实现(包含论文+源码)
- 手机端 我的世界融合植物大战僵尸版.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈