module MachineLearning.Regularization
(
Regularization(..)
, costReg
, gradientReg
)
where
import MachineLearning.Types (R, Vector)
import qualified Data.Vector.Storable as V
import qualified Numeric.LinearAlgebra as LA
data Regularization = RegNone
| L2 R
costReg :: Regularization -> Vector -> R
costReg RegNone _ = 0
costReg (L2 lambda) theta = (thetaReg LA.<.> thetaReg) * lambda * 0.5
where thetaReg = V.tail theta
gradientReg :: Regularization -> Vector -> Vector
gradientReg RegNone _ = 0
gradientReg (L2 lambda) theta = (LA.scalar lambda) * thetaReg
where thetaReg = theta V.// [(0, 0)]