11# HashedExpression
22[ ![ wercker status] ( https://app.wercker.com/status/fce29884fa47e4258f62240000f1e368/m/master " wercker status ")] ( https://app.wercker.com/project/byKey/fce29884fa47e4258f62240000f1e368 )
33
4- Type-safe modelling DSL , symbolic transformation, and code generation for solving optimization problems.
4+ Haskell-embeded Algebraic Modeling Language with type-safety , symbolic transformation and C- code generation
55
66
77## Features
@@ -86,12 +86,8 @@ ex1_linearRegression =
8686 values =
8787 [ x :-> VFile (TXT " x.txt" ),
8888 y :-> VFile (TXT " y.txt" )
89- ],
90- workingDir = " examples" </> " LinearRegression"
89+ ]
9190 }
92-
93- ex1 :: IO ()
94- ex1 = proceed ex1_linearRegression CSimpleConfig {output = OutputText }
9591```
9692(` (*.) ` is scaling )
9793
@@ -126,12 +122,8 @@ ex2_logisticRegression =
126122 values =
127123 [ x :-> VFile (TXT " x_expanded.txt" ),
128124 y :-> VFile (TXT " y.txt" )
129- ],
130- workingDir = " examples" </> " LogisticRegression"
125+ ]
131126 }
132-
133- ex2 :: IO ()
134- ex2 = proceed ex2_logisticRegression CSimpleConfig {output = OutputText }
135127```
136128
137129( ` (**) ` is matrix multiplication, ` (<.>) ` is dot product, ` project (range @1 @27, at @0) theta ` is the typed version of ` theta[1:27,0] ` )
@@ -144,33 +136,36 @@ ex2 = proceed ex2_logisticRegression CSimpleConfig {output = OutputText}
144136Model is in [ app/Examples/Brain.hs] ( app/Examples/Brain.hs ) , data is in [ examples/Brain] ( examples/Brain )
145137
146138``` haskell
147- brain_reconstructFromMRI :: OptimizationProblem
148- brain_reconstructFromMRI =
139+ brainReconstructFromMRI :: OptimizationProblem
140+ brainReconstructFromMRI =
149141 let -- variables
150142 x = variable2D @ 128 @ 128 " x"
143+ --- bound
144+ xLowerBound = bound2D @ 128 @ 128 " x_lb"
145+ xUpperBound = bound2D @ 128 @ 128 " x_ub"
151146 -- parameters
152147 im = param2D @ 128 @ 128 " im"
153148 re = param2D @ 128 @ 128 " re"
154149 mask = param2D @ 128 @ 128 " mask"
155150 -- regularization
151+ regularization = norm2square (rotate (0 , 1 ) x - x) + norm2square (rotate (1 , 0 ) x - x)
156152 lambda = 3000
157- regularization = lambda * (norm2square (rotate (0 , 1 ) x - x) + norm2square (rotate (1 , 0 ) x - x))
158153 in OptimizationProblem
159- { objective = norm2square ((mask +: 0 ) * (ft (x +: 0 ) - (re +: im))) + regularization,
154+ { objective =
155+ norm2square ((mask +: 0 ) * (ft (x +: 0 ) - (re +: im)))
156+ + lambda * regularization,
160157 constraints =
161- [ x .<= VFile ( HDF5 " bound.h5 " " ub " ) ,
162- x .>= VFile ( HDF5 " bound.h5 " " lb " )
158+ [ x .<= xUpperBound ,
159+ x .>= xLowerBound
163160 ],
164161 values =
165162 [ im :-> VFile (HDF5 " kspace.h5" " im" ),
166163 re :-> VFile (HDF5 " kspace.h5" " re" ),
167- mask :-> VFile (HDF5 " mask.h5" " mask" )
168- ],
169- workingDir = " examples" </> " Brain"
164+ mask :-> VFile (HDF5 " mask.h5" " mask" ),
165+ xLowerBound :-> VFile (HDF5 " bound.h5" " lb" ),
166+ xUpperBound :-> VFile (HDF5 " bound.h5" " ub" )
167+ ]
170168 }
171-
172- brain :: IO ()
173- brain = proceed brain_reconstructFromMRI CSimpleConfig {output = OutputHDF5 }
174169```
175170
176171<img src =" docs/images/brain_before.png " width =" 450px " />
@@ -190,7 +185,7 @@ prependColumn ::
190185 (Injectable 0 (m - 1 ) m m , Injectable 1 n n (n + 1 )) =>
191186 Double ->
192187 TypedExpr '[m , n ] R ->
193- TypedExpr ( D2 m ( n + 1 )) R
188+ TypedExpr '[ m , n + 1 ] R
194189prependColumn v exp = inject (range @ 0 @ (m - 1 ), range @ 1 @ n ) exp (constant2D @ m @ (n + 1 ) v)
195190
196191ex4_neuralNetwork :: OptimizationProblem
@@ -216,12 +211,8 @@ ex4_neuralNetwork =
216211 values =
217212 [ x :-> VFile (HDF5 " data.h5" " x" ),
218213 y :-> VFile (HDF5 " data.h5" " y" )
219- ],
220- workingDir = " examples" </> " NeuralNetwork"
214+ ]
221215 }
222-
223- ex4 :: IO ()
224- ex4 = proceed ex4_neuralNetwork CSimpleConfig {output = OutputHDF5 , maxIteration = Just 400 }
225216```
226217
227218<img src =" docs/images/nn_before.png " width =" 450px " />
0 commit comments