大家好,又见面了,我是你们的朋友全栈君。
今天在看Michael Elad 大牛的论文《On the Role of Sparse and Redundant Representations in Image Processing》中,看到了soft thresholding 和hard thresholding 这两个概念,很是不明白,所以上网查了一些资料,先把网上查的东西贴出来讨论下。
网上资料大体是说这是指两类的函数,分别是软阈值函数和硬阈值函数,在matlab中soft thresholding function 的形式是
% Function to solve soft thresholding problem
%
% arg min_{x} ||x – b||_{2}^{2} + lambda*||x||_{1}
%
% Usage:- x = soft_thresh
%
% where:- <in>
%
%
%
%
%
% Written by Simon Lucey 2012
function x = soft_thresh(b,lambda)
% Set the threshold
th = lambda/2;
% First find elements that are larger than the threshold
k = find(b > th);
x(k) = b(k) – th;
% Next find elements that are less than abs
k = find(abs(b) <= th);
x(k) = 0;
% Finally find elements that are less than -th
k = find(b < -th);
x(k) = b(k) + th;
x = x(:);
或者上述函数也可以更简洁的用一句matlab代码表示:
soft_thresh = @(b,lambda) sign(b).*max(abs(b) – lambda/2,0); %对matlab比较精通的同学可以解释下。
下面进行对比:
b = [-0.8487
lambda=2;
soft_thresh(b, lambda)
ans =
soft_thresh_cvx(b, lambda)
ans =
可以看到,两者产生的结果完全一样,我自己也在自己的电脑上试验了一下,前提是需要安装斯坦福大学的凸优化解决包CVX,试验结果和simon的完全一样,CVX的解决时间为0.263894秒,而soft thresholding的时间仅仅为0.002016秒,快了130倍!!!