Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save jonathanmarvens/574e09c6380450ae6b1c to your computer and use it in GitHub Desktop.

Select an option

Save jonathanmarvens/574e09c6380450ae6b1c to your computer and use it in GitHub Desktop.
class BasicPerceptron
def initialize(weights, bias)
@weights = weights
@threshold = bias * -1
end
def run(inputs)
input_weight_pairs = inputs.zip @weights
sum = 0.0
input_weight_pairs.each { |input, weight| sum += input * weight }
sum <= @threshold ? 0 : 1
end
end
@jonathanmarvens
Copy link
Author

Example:

nand = BasicPerceptron.new [-2, -2], 3

nand.run [0, 0] # 1
nand.run [0, 1] # 1
nand.run [1, 0] # 1
nand.run [1, 1] # 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment