fix(run): I had written logistic' wrong, that's what happens when you write code while sleepy

This commit is contained in:
Mahdi Dibaiee 2016-07-20 12:06:19 +04:30
parent b941580273
commit 49606406d1

View File

@ -37,8 +37,8 @@ module Sibe
runLayer input (L !biases !weights) = input <# weights + biases runLayer input (L !biases !weights) = input <# weights + biases
forward :: Input -> Network -> Output forward :: Input -> Network -> Output
forward input (O l) = cmap logistic $ runLayer input l forward input (O l) = logistic $ runLayer input l
forward input (l :- n) = forward (cmap logistic $ runLayer input l) n forward input (l :- n) = forward (logistic $ runLayer input l) n
randomLayer :: Seed -> (Int, Int) -> Layer randomLayer :: Seed -> (Int, Int) -> Layer
randomLayer seed (wr, wc) = randomLayer seed (wr, wc) =
@ -53,11 +53,11 @@ module Sibe
randomLayer seed (input, h) :- randomLayer seed (input, h) :-
randomNetwork (seed + 1) h hs output randomNetwork (seed + 1) h hs output
logistic :: Double -> Double logistic :: Vector Double -> Vector Double
logistic x = 1 / (1 + exp (-x)) logistic x = 1 / (1 + exp (-x))
logistic' :: Double -> Double logistic' :: Vector Double -> Vector Double
logistic' x = logistic x / max 1e-10 (1 - logistic x) logistic' x = logistic x * (1 - logistic x)
train :: Input train :: Input
-> Network -> Network
@ -69,9 +69,9 @@ module Sibe
run :: Input -> Network -> (Network, Vector Double) run :: Input -> Network -> (Network, Vector Double)
run input (O l@(L biases weights)) = run input (O l@(L biases weights)) =
let y = runLayer input l let y = runLayer input l
o = cmap logistic y o = logistic y
delta = o - target delta = o - target
de = delta * cmap logistic' o de = delta * logistic' y
biases' = biases - scale alpha de biases' = biases - scale alpha de
weights' = weights - scale alpha (input `outer` de) -- small inputs learn slowly weights' = weights - scale alpha (input `outer` de) -- small inputs learn slowly
@ -83,10 +83,10 @@ module Sibe
in (O layer, pass) in (O layer, pass)
run input (l@(L biases weights) :- n) = run input (l@(L biases weights) :- n) =
let y = runLayer input l let y = runLayer input l
o = cmap logistic y o = logistic y
(n', delta) = run o n (n', delta) = run o n
de = delta * cmap logistic' o de = delta * logistic' y
biases' = biases - scale alpha de biases' = biases - scale alpha de
weights' = weights - scale alpha (input `outer` de) weights' = weights - scale alpha (input `outer` de)