Message Boards Message Boards

Directly emulate Ridge Regression and LASSO with a neural network?

I would like for pedagogic purposes to show how Ridge Regression and LASSO (and presumably other forms of regularized regression such as ElasticNet) can be emulated using a neural network in the same way that one can emulate conventional regression. But for the life of me I can't figure out how to get the coefficients out of the LinearLayer and feed them into some sort of penalty function. I suspect I am just being dense. Surely there is some sort of NetGraph and some sort of Layer that can do this. Can anyone help? (Yes, I know there are many other ways of preventing overfitting, but this is the one I want to use).

POSTED BY: Seth Chandler
6 Replies
Posted 2 years ago

On the topic "Loss function with regularization" on the Mathematica stack exchange website there is a reply that shows how to make a layer that implements L2 regularization with NetArray.

POSTED BY: user 1235
Posted 3 years ago

One way to get the weights out from the linear layer is simply to pass unit vectors as input together with the actual features. To make this work we need to have weight sharing of the LinearLayer weights across these multiple inputs. This is achieved by the NetMapOperator as mentioned in the documentation.

In[371]:= With[{w = {{1, 2}}},
   net = NetMapOperator[
      LinearLayer[1, "Input" -> 2, "Weights" -> w, "Biases" -> None]];
   ]

  In[372]:= net[{{2, 3}, {1, 0}, {0, 1}}]

  Out[372]= {{8.}, {1.}, {2.}}

PartLayer[1] and PartLayer[2;;3] acting downstream of above in a NetGraph will let you siphon out the relevant pieces from which you can build your error function. Let me know if you need more help.

POSTED BY: areez mody

Thanks. I am aware of the upgrade to Fit to permit regularization. It works just fine. Two wishes:

(a) regularization would also be added to the more modern LinearModelFit, LogitModelFit, etc so I could take advantage of their greater capabilities (b) someone could give me some hints on how to emulate LASSO or Ridge using the Neural Network architecture. As mentioned earlier, I have some belief that it can be done in 12.2 using NetArray inside a linear layer (http://reference.wolfram.com/language/ref/NetArray.html) and/or NetArrayLayer but, as the documentation for that Experimental function is not fully developed, I have not yet figured out how to do so.

POSTED BY: Seth Chandler

Just FYI, LASSO and many other regularizations are now available in Fit, which were added in Wolfram Language version 12.0 as an application of convex optimization. Ridge regression is there listed as Tikhonov regularization.

http://reference.wolfram.com/language/ref/Fit.html

enter image description here

POSTED BY: Vitaliy Kaurov
Posted 3 years ago

For ridge regression I would suggest using the lmridge package in R (which you already know you can do from Mathematica). There are so many options for choosing the scaling parameter that a home-grown program in Mathematica would likely be fragile at best especially if you need credibility in the results.

POSTED BY: Jim Baldwin

If it helps, I have some sense that NetArray and NetArrayLayer, introduced in 12.2, might be involved. But how? The documentation for those two experimental functions are somewhat underdeveloped.

POSTED BY: Seth Chandler
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract