Skip to content

Instantly share code, notes, and snippets.

@yanatan16
Last active January 2, 2016 18:49
Show Gist options
  • Save yanatan16/8346184 to your computer and use it in GitHub Desktop.
Save yanatan16/8346184 to your computer and use it in GitHub Desktop.
Walkthrough of haskell-spsa
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)
standardAk :: Double -> Int -> Double -> [Double]
standardAk a aA alpha = map (\k -> a / (k + 1 + (fromIntegral aA)) ** alpha) [1..]
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