Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/johannesmessner/lambdagrad
Automatic gradients in ~70 lines of Haskell
https://github.com/johannesmessner/lambdagrad
Last synced: 6 days ago
JSON representation
Automatic gradients in ~70 lines of Haskell
- Host: GitHub
- URL: https://github.com/johannesmessner/lambdagrad
- Owner: JohannesMessner
- Created: 2023-05-22T20:09:34.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-05-22T20:20:26.000Z (over 1 year ago)
- Last Synced: 2025-01-07T22:06:11.129Z (13 days ago)
- Language: Haskell
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lambdagrad
Automatic gradients in ~70 lines of HaskellLoad the `Engine` module:
```terminal
$ ghci
GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help
Prelude> :l Engine
[1 of 2] Compiling Data ( Data.hs, interpreted )
[2 of 2] Compiling Engine ( Engine.hs, interpreted )
Ok, two modules loaded.
```Use `val` to creat computational graphs:
```bash
*Engine> expression = ((val 0.5) + (val 3)) ** 2
*Engine> expression
InnerNode Pow 12.25 0.0 (InnerNode Plus 3.5 0.0 (Leaf 0.5 0.0) (Leaf 3.0 0.0)) (Leaf 2.0 0.0)
```Use `backward` to compute gradients:
```bash
*Engine> backward expression
InnerNode Pow 12.25 1.0 (InnerNode Plus 3.5 7.0 (Leaf 0.5 7.0) (Leaf 3.0 7.0)) (Leaf 2.0 0.0)
```Use `getLeafs` to get leaf node of the computational graph:
```bash
*Engine> getLeafs $ backward expression
[Leaf 0.5 7.0,Leaf 3.0 7.0,Leaf 2.0 0.0]
```And `gradient` to extract their gradients:
```bash
*Engine> map gradient (getLeafs $ backward expression)
[7.0,7.0,0.0]
```