Last active
January 2, 2016 18:49
-
-
Save yanatan16/8346184 to your computer and use it in GitHub Desktop.
Walkthrough of haskell-spsa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
optimize :: SPSA -> Int -> Vector Double -> Vector Double | |
optimize spsa rounds t0 = foldl opt t0 (take rounds $ zip3 (ak spsa) (ck spsa) (delta spsa)) | |
where | |
constrainF = constraint spsa | |
lossF = loss spsa | |
opt t (a,c,d) = constrainF (t - a * g) | |
where | |
cd = c * d | |
ya = lossF (t + cd) | |
yb = lossF (t - cd) | |
g = (ya - yb) / (2 * cd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
standardAk :: Double -> Int -> Double -> [Double] | |
standardAk a aA alpha = map (\k -> a / (k + 1 + (fromIntegral aA)) ** alpha) [1..] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Numeric.LinearAlgebra (Vector) | |
type LossFn = Vector Double -> Double | |
type ConstraintFn = Vector Double -> Vector Double | |
-- An instance of the SPSA optimization algorithm. | |
-- Initialize with all the parameters as object instantiation. | |
data SPSA = SPSA { | |
loss :: LossFn, | |
constraint :: ConstraintFn, | |
ak, ck :: [Double], | |
delta :: [Vector Double] -- a stream of random perturbation vectors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment