a b y
0 0 0
0 1 0
1 0 1
1 1 1
def activation(out, threshold): if out > threshold: return 1 else:
return 0
def perceptron(and_input): a = [0, 0, 1, 1] b = [0, 1, 0, 1] y = [0, 0,
1, 1] w = [0.2, 0.3] threshold = 0.1 learning_rate = 0.1
i = 0
print("perceptron training")
print("########################")
print()
while i < 4:
summation = a[i]*w[0] + b[i]*w[1]
o = activation(summation, threshold)
print("input: " + str(a[i]) + " , " + str(b[i]))
print("weights: " + str(w[0]) + " , " + str(w[1]))
print("Summation: " + str(summation) + ' threshold: ' + str(threshold))
print("predicted output: " + str(o))
print("Actual output: " + str(y[i]))
if o != y[i]:
print(" updating weights")
w[0] = w[0] + learning_rate * (y[i] - o) * a[i]
w[1] = w[1] + learning_rate * (y[i] - o) * b[i]
print(" updated weights: " + str(w[0]) + ' , ' + str(w[1]))
i += 1
print("weights update Training Again")
print("########################")
print("########################")
print()
summation = and_input[0]*w[0] + and_input[1]*w[1]
return activation(summation, threshold)
or_input = [0, 1] print(“or gate output for” + str(or_input) + “:” +
str(perceptron(or_input)))