slimming icon indicating copy to clipboard operation
slimming copied to clipboard

local subgradient = S*torch.sign(weight)

Open MrLinNing opened this issue 6 years ago • 3 comments

L1 sparsity should be torch.abs(weight), can you detail more about it? local subgradient = S*torch.sign(weight)

MrLinNing avatar May 03 '18 02:05 MrLinNing

The (sub)gradient of absolute value function (L1 sparsity loss) is the sign function. Here we compute the subgradient directly without defining loss.

liuzhuang13 avatar May 03 '18 20:05 liuzhuang13

thank you! @liuzhuang13
Why you used subgradient? did you try directly defining loss ?

MrLinNing avatar May 04 '18 02:05 MrLinNing

Because absolute value function is not differentiable at point x=0, so it is subgradient instead of gradient. But in practice, the weight x never becomes 0 so it is actually equivalent to gradient.

Unlike Pytorch, in Torch there is no automatic differentiation, so I found this to be the most convenient way to do the thing we wanted, and we just used it.

liuzhuang13 avatar May 04 '18 04:05 liuzhuang13