Skip to content

Commit

Permalink
Add initial modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rounakdatta committed Apr 9, 2024
1 parent 9bebca2 commit 6f584f9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
smolgrad/_build/*
smolgrad/_opam/*
_build/*
_opam/*
.vscode/*
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 2.0)
(name smolgrad.ml)
(name smolgrad-ml)
3 changes: 3 additions & 0 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name nn)
(modules Neuron Network))
Empty file added lib/network.ml
Empty file.
29 changes: 29 additions & 0 deletions lib/neuron.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Neuron = struct
type t = {
mutable data : float;
mutable grad : float;
mutable backward : unit -> unit;
op : string;
prev : t list;
}

let create data operator = { data; grad = 0.;
backward = (fun () -> ()); op = operator; prev = []
}

let add a b =
let out = create (a.data +. b.data) "+" in
out.backward <- (fun () ->
a.grad <- a.grad +. out.grad;
b.grad <- b.grad +. out.grad;
);
out

let mul a b =
let out = create (a.data *. b.data) "*" in
out.backward <- (fun () ->
a.grad <- a.grad +. b.data *. out.grad;
b.grad <- b.grad +. a.data *. out.grad;
);
out
end
12 changes: 12 additions & 0 deletions lib/neuron.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Neuron : sig
type t

(* Constructor; constructs a unit neuron of a value and an operator. *)
val create : float -> string -> t

(* Adds two values, resulting in a new value. *)
val add : t -> t -> t

(* Multiplies two values, resulting in a new value. *)
val mul : t -> t -> t
end
4 changes: 0 additions & 4 deletions src/core/dune

This file was deleted.

0 comments on commit 6f584f9

Please sign in to comment.