Skip to content

Instantly share code, notes, and snippets.

@darkf darkf/ANN.hs
Created Apr 3, 2016

Embed
What would you like to do?
type Weight = Float
type Bias = Float
-- type Neuron = Bias -> [Weight] -> [Float] -> Float
-- Artificial neuron, given some activation function f.
-- A neuron is simply a function from a vector of weights, and a vector of values, to an activation value.
neuron :: (Float -> Float) -> Bias -> [Weight] -> [Float] -> Float
--neuron :: (Float -> Float) -> Neuron
neuron f b ws xs = f (sum (zipWith (*) ws xs) + b)
-- A perceptron is a neuron with the activation function being the step function (a simple threshold).
perceptron :: Bias -> [Weight] -> [Float] -> Float
--perceptron :: Neuron
perceptron = neuron (\z -> if z > 0 then 1 else 0)
sigmoid :: Float -> Float
sigmoid z = 1 / (1 + exp (-z))
-- A sigmoid neurdon is a neuron with the activation function being the sigmoid function, resulting in a smoother,
-- more continuous version of the perceptron, allowing more minute changes in the bias or weights to result
-- in small changes in the output.
sigmoidN :: Bias -> [Weight] -> [Float] -> Float
--sigmoidN :: Neuron
sigmoidN = neuron sigmoid
-- OR gate
or_p = perceptron 0 [1, 1]
-- AND gate
and_p = perceptron (-1) [1, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.