Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@darkf
Created April 3, 2016 11:43
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 darkf/a9c962778431713cd219dfe5ff74402b to your computer and use it in GitHub Desktop.
Save darkf/a9c962778431713cd219dfe5ff74402b to your computer and use it in GitHub Desktop.
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