-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Layer module; Add neuron activation unit test
- Loading branch information
1 parent
adb9fa4
commit bfe93e9
Showing
8 changed files
with
99 additions
and
59 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
{ | ||
description = "Algorithmic (aka automatic) differentiation implementation in OCaml"; | ||
description = "Algorithmic (aka automatic) differentiation implementation in OCaml"; | ||
|
||
inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
flake-utils.url = "github:numtide/flake-utils"; | ||
}; | ||
inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | ||
flake-utils.url = "github:numtide/flake-utils"; | ||
}; | ||
|
||
outputs = { self, nixpkgs, flake-utils, ... }: | ||
flake-utils.lib.eachDefaultSystem (system: | ||
let | ||
pkgs = nixpkgs.legacyPackages.${system}; | ||
isDarwin = pkgs.stdenv.isDarwin; | ||
# thanks to https://github.com/commercialhaskell/stack/issues/1698#issuecomment-178098712 for the idea | ||
darwinFrameworks = pkgs.lib.optionals isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ | ||
Cocoa | ||
CoreServices | ||
]); | ||
ocamlEnv = with pkgs.ocamlPackages; [ | ||
ocaml | ||
utop | ||
dune_3 | ||
findlib | ||
ocaml-lsp | ||
ocamlformat | ||
]; | ||
in | ||
{ | ||
devShell = pkgs.mkShell { | ||
buildInputs = ocamlEnv ++ [ pkgs.opam ] ++ darwinFrameworks; | ||
shellHook = '' | ||
export IN_NIX_DEVELOP_SHELL=1 | ||
outputs = { self, nixpkgs, flake-utils, ... }: | ||
flake-utils.lib.eachDefaultSystem (system: | ||
let | ||
pkgs = nixpkgs.legacyPackages.${system}; | ||
isDarwin = pkgs.stdenv.isDarwin; | ||
# thanks to https://github.com/commercialhaskell/stack/issues/1698#issuecomment-178098712 for the idea | ||
darwinFrameworks = pkgs.lib.optionals isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ | ||
Cocoa | ||
CoreServices | ||
]); | ||
ocamlEnv = with pkgs.ocamlPackages; [ | ||
ocaml | ||
utop | ||
dune_3 | ||
findlib | ||
ocaml-lsp | ||
ocamlformat | ||
]; | ||
in | ||
{ | ||
devShell = pkgs.mkShell { | ||
buildInputs = ocamlEnv ++ [ pkgs.opam ] ++ darwinFrameworks; | ||
shellHook = '' | ||
export IN_NIX_DEVELOP_SHELL=1 | ||
export OPAMROOT=$NIX_BUILD_TOP/.opam | ||
# unsetting the below env var is required for fixing a thorny issue with `num` install | ||
# similar issue & solution thread: https://github.com/ocaml/Zarith/issues/136 | ||
unset OCAMLFIND_DESTDIR | ||
export OPAMROOT=$NIX_BUILD_TOP/.opam | ||
# unsetting the below env var is required for fixing a thorny issue with `num` install | ||
# similar issue & solution thread: https://github.com/ocaml/Zarith/issues/136 | ||
unset OCAMLFIND_DESTDIR | ||
opam init --bare --disable-sandboxing -y --shell-setup -vv | ||
opam option -global depext=false | ||
OCAML_VERSION=$(ocaml --version | awk '{printf $5}') | ||
opam switch create $OCAML_VERSION | ||
eval $(opam env --switch=$OCAML_VERSION) | ||
opam install . --deps-only -y -v | ||
opam init --bare --disable-sandboxing -y --shell-setup -vv | ||
opam option -global depext=false | ||
OCAML_VERSION=$(ocaml --version | awk '{printf $5}') | ||
opam switch create $OCAML_VERSION | ||
eval $(opam env --switch=$OCAML_VERSION) | ||
opam install . --deps-only -y -v | ||
# figure out what the default shell of this computer is and set it | ||
'' + | ||
(if isDarwin then | ||
'' | ||
SHELLY=$(dscl . -read /Users/$USER UserShell | awk '{print $2}') | ||
exec $SHELLY | ||
'' | ||
else | ||
'' | ||
SHELLY=$(getent passwd $USER | awk -F: '{printf $7}') | ||
exec $SHELLY | ||
''); | ||
}; | ||
} | ||
); | ||
# figure out what the default shell of this computer is and set it | ||
'' + | ||
(if isDarwin then | ||
'' | ||
SHELLY=$(dscl . -read /Users/$USER UserShell | awk '{print $2}') | ||
exec $SHELLY | ||
'' | ||
else | ||
'' | ||
SHELLY=$(getent passwd $USER | awk -F: '{printf $7}') | ||
exec $SHELLY | ||
''); | ||
}; | ||
} | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(library | ||
(name smolgrad) | ||
(public_name smolgrad) | ||
(modules variable neuron)) | ||
(modules variable neuron layer)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Layer = struct | ||
type t = { | ||
neurons : Neuron.Neuron.t list | ||
} | ||
|
||
let create number_of_inputs number_of_neurons is_non_linear = | ||
let neurons = List.init number_of_neurons (fun _ -> Neuron.Neuron.create number_of_inputs is_non_linear) in | ||
{ neurons = neurons } | ||
|
||
let propagate_input (layer: t) input_vector = | ||
List.map (fun neuron -> Neuron.Neuron.weigh_input neuron input_vector) layer.neurons | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(* Layer is the aggregator of many neurons parallelly | ||
captures essence of an input by adjusting it's weights and biases *) | ||
module Layer : sig | ||
type t | ||
|
||
(* Constructor; constructs a layer of neurons *) | ||
val create : int -> int -> bool -> t | ||
|
||
(* Propagates the input across all the neurons in the layer *) | ||
val propagate_input : t -> Variable.Variable.t list -> Variable.Variable.t list | ||
end |
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
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
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
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