0% found this document useful (0 votes)
21 views1 page

Perceptron Training for AND Logic

The document describes a simple perceptron model for an AND gate using Python code. It includes the implementation of an activation function, training of the perceptron with input data, and weight updates based on the predicted and actual outputs. Finally, it demonstrates how to use the trained perceptron to predict the output for a given input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Perceptron Training for AND Logic

The document describes a simple perceptron model for an AND gate using Python code. It includes the implementation of an activation function, training of the perceptron with input data, and weight updates based on the predicted and actual outputs. Finally, it demonstrates how to use the trained perceptron to predict the output for a given input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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)))

You might also like