I just programmed a basic neural network in F# to learn the logical OR function. As I am very new to F# and especially functional programming, I did it the imperative way. And even tho it works, I find it highly unattractive. I would like to improve it to make it as functional-like as possible. I though about overloading operators to make scalar products between weights and neuroninput but i don't think its the classier way to make my program nicer.
I want to make my code functional-like.
module nnbasic
let mutable neuroninput = [0.0;0.0]
let mutable weight = [0.4;0.6]
let rate = 0.2
let threeshold = 2.0
// [input1; input2; desiredoutput]
let matrix = [
[0.0;0.0;0.0];
[0.0;1.0;1.0];
[1.0;0.0;1.0];
[1.0;1.0;1.0]
]
let display output real =
if output = real then printfn "yes"
else printfn "no"
let output (_ni: float list, _wi: float list) =
if threeshold > _ni.[0]*_wi.[0] + _ni.[1]*_wi.[1] then 0.0 else 1.0
let mutable iter = 0
let mutable out = 0.0
while iter < 100 do
for row in matrix do
neuroninput <- [row.[0];row.[1]]
out <- output (neuroninput, weight)
weight <- [weight.[0]+rate*(row.[2]-out);weight.[1]]
display out row.[2]
out <- output (neuroninput, weight)
weight <- [weight.[0];weight.[1]+rate*(row.[2]-out)]
if threeshold > neuroninput.[0]*weight.[0] + neuroninput.[1]*weight.[1] then display 0.0 row.[2] else display 1.0 row.[2]
iter <- iter+1
mutable
huh? :) \$\endgroup\$