Skip to content

Instantly share code, notes, and snippets.

@IntegerMan
Created September 7, 2019 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IntegerMan/0cc757c4bfc0de80d3b2e5341b06c828 to your computer and use it in GitHub Desktop.
Save IntegerMan/0cc757c4bfc0de80d3b2e5341b06c828 to your computer and use it in GitHub Desktop.
/// Represents a node in a Neural Network
and Neuron ([<Optional>] ?initialValue: decimal) =
let mutable value = defaultArg initialValue 0M;
let mutable inputs: NeuronConnection seq = Seq.empty;
/// Exposes the current calculated amount of the Neuron
member this.Value
with get () = value
and set (newValue) = value <- newValue
/// Incoming connections from other Neurons (if any)
member this.Inputs: NeuronConnection seq = inputs;
/// Adds an incoming connection from another Neuron
member this.AddIncomingConnection c = inputs <- Seq.append this.Inputs [c];
/// Adds all connections together, stores the result in Value, and returns the value
member this.Evaluate(): decimal =
if not (Seq.isEmpty this.Inputs) then do
let numInputs = Seq.length this.Inputs |> decimal
value <- Seq.sumBy (fun (c:NeuronConnection) -> c.Evaluate()) this.Inputs / numInputs;
value;
/// Connects this neuron to another and returns the connection
member this.Connect(target: Neuron) =
let connection = new NeuronConnection(this);
target.AddIncomingConnection(connection);
connection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment