-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogisticRegression.hs
37 lines (33 loc) · 1.09 KB
/
LogisticRegression.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Examples.LogisticRegression where
import HashedExpression
import HashedExpression.Modeling.Typed
import System.FilePath ((</>))
import Prelude hiding ((**), (^))
sigmoid :: (ToShape d) => TypedExpr d R -> TypedExpr d R
sigmoid x = 1.0 / (1.0 + exp (- x))
ex2_logisticRegression :: OptimizationProblem
ex2_logisticRegression =
let -- variables
theta = variable1D @28 "theta"
-- parameters
x = param2D @118 @28 "x"
y = param1D @118 "y"
hypothesis = sigmoid (x ** theta)
-- regularization
lambda = 1
regTheta = project (range @1 @27) theta
regularization = (lambda / 2) * (regTheta <.> regTheta)
in OptimizationProblem
{ objective = sumElements ((- y) * log hypothesis - (1 - y) * log (1 - hypothesis)) + regularization,
constraints = [],
values =
[ x :-> VFile (TXT "x_expanded.txt"),
y :-> VFile (TXT "y.txt")
]
}
ex2 :: IO ()
ex2 =
proceed
ex2_logisticRegression
CSimpleConfig {output = OutputText, maxIteration = Nothing}
("examples" </> "LogisticRegression")