https://github.com/ocaml-multicore/effects-examples/blob/master/algorithmic_differentiation.ml Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + For + Enterprise + Teams + Startups + Compare all + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this organization All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} ocaml-multicore / effects-examples Public * Notifications * Fork 28 * Star 335 * Code * Issues 3 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights Permalink master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create effects-examples/algorithmic_differentiation.ml Go to file * Go to file T * Go to line L * * Copy path * Copy permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. @patricoferris patricoferris Effect.eff -> Effect.t Latest commit 242e2dc Feb 28, 2022 History 2 contributors Users who have contributed to this file @patricoferris @kayceesrk 84 lines (74 sloc) 2.19 KB Raw Blame Edit this file E Open in GitHub Desktop * Open with Desktop * View raw * * View blame 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 Show hidden characters (* Reverse-mode Algorithmic differentiation using effect handlers. Adapted from https://twitter.com/tiarkrompf/status/ 963314799521222656. See https://openreview.net/forum?id=SJxJtYkPG for more information. *) open Effect open Effect.Deep module F : sig type t val mk : float -> t val (+.) : t -> t -> t val ( *. ) : t -> t -> t val grad : (t -> t) -> float -> float val grad2 : (t * t -> t) -> float * float -> float * float end = struct type t = { v : float; mutable d : float } let mk v = {v; d = 0.0} type _ Effect.t += Add : t * t -> t Effect.t type _ Effect.t += Mult : t * t -> t Effect.t let run f = ignore (match_with f () { retc = (fun r -> r.d <- 1.0; r); exnc = raise; effc = fun (type a) (e : a Effect.t) -> match e with | Add (a, b) -> Some (fun (k : (a, _) continuation) -> let x = {v = a.v +. b.v; d = 0.0} in ignore (continue k x); a.d <- a.d +. x.d; b.d <- b.d +. x.d; x) | Mult(a,b) -> Some (fun k -> let x = {v = a.v *. b.v; d = 0.0} in ignore (continue k x); a.d <- a.d +. (b.v *. x.d); b.d <- b.d +. (a.v *. x.d); x) | _ -> None }) let grad f x = let x = mk x in run (fun () -> f x); x.d let grad2 f (x, y) = let x,y = mk x, mk y in run (fun () -> f (x,y)); x.d, y.d let (+.) a b = perform (Add(a,b)) let ( *. ) a b = perform (Mult(a,b)) end;; (* f = x + x^3 => df/dx = 1 + 3 * x^2 *) for x = 0 to 10 do let x = float_of_int x in assert (F.(grad (fun x -> x +. x *. x *. x) x) = 1.0 +. 3.0 *. x *. x) done;; (* f = x^2 + x^3 => df/dx = 2*x + 3 * x^2 *) for x = 0 to 10 do let x = float_of_int x in assert (F.(grad (fun x -> x *. x +. x *. x *. x) x) = 2.0 *. x +. 3.0 *. x *. x) done;; (* f = x^2 * y^4 => df/dx = 2 * x * y^4 df/dy = 4 * x^2 * y^3 *) for x = 0 to 10 do for y = 0 to 10 do let x = float_of_int x in let y = float_of_int y in assert (F.(grad2 (fun (x,y) -> x *. x *. y *. y *. y *. y) (x,y)) = (2.0 *. x *. y *. y *. y *. y, 4.0 *. x *. x *. y *. y *. y)) done done;; * Copy lines * Copy permalink * View git blame * Reference in new issue [ ] Go Footer (c) 2022 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.