Skip to content

Instantly share code, notes, and snippets.

@Javran
Created January 15, 2014 05:37
Show Gist options
  • Save Javran/8431352 to your computer and use it in GitHub Desktop.
Save Javran/8431352 to your computer and use it in GitHub Desktop.
gradient descent in haskell
realworldFunc x = x * 0.5 + 1.23
dataSize = 100
dataSet = take dataSize $ map (\x -> (x,realworldFunc x)) [1..]
gdAlgo alpha (initT0, initT1) = (newT0,newT1)
where
hypo x = x * initT1 + initT0
newT0 = initT0 - theConst * (sum theDiffs)
newT1 = initT1 - theConst * (sum $ zipWith (*) theDiffs (map fst dataSet))
theDiffs = map (\(x,y) -> hypo x - y) dataSet
theConst = alpha / (fromIntegral dataSize)
theStream = iterate (gdAlgo 0.0004) (0,0)
main = print $ theStream !! 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment