Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Last active August 29, 2015 14:13
Show Gist options
  • Save jonathanmarvens/cef6ebd30c3c6444a890 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/cef6ebd30c3c6444a890 to your computer and use it in GitHub Desktop.
class BasicPerceptron(object):
def __init__(self, weights, bias):
self.weights = weights
self.threshold = bias * -1
def run(self, inputs):
input_weight_pairs = zip(inputs, self.weights)
sum_ = 0.0
for input_, weight in input_weight_pairs:
sum_ += input_ * weight
return 0 if sum_ <= self.threshold else 1
@jonathanmarvens
Copy link
Author

Example:

nand = BasicPerceptron((-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