Skip to content

Instantly share code, notes, and snippets.

@vikene
Created December 20, 2018 02:23
Show Gist options
  • Save vikene/0b2bfd556e401b451d18e6216fdb7832 to your computer and use it in GitHub Desktop.
Save vikene/0b2bfd556e401b451d18e6216fdb7832 to your computer and use it in GitHub Desktop.
Implementation of AND gate using perceptron
import numpy as np
input1 = [0,0,1,1]
input2 = [1,0,1,0]
truth = [0,0,1,0]
weight1 = 1
weight2 = 1
bias = -2
def perceptron(input1, input2):
score = (input1* weight1) + (input2 * weight2) + bias
if score >= 0:
return 1
else:
return 0
def test_perceptron():
assert perceptron(input1[0],input2[0]) == truth[0]
assert perceptron(input1[1],input2[1]) == truth[1]
assert perceptron(input1[2],input2[2]) == truth[2]
assert perceptron(input1[3],input2[3]) == truth[3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment