Implementing Perceptrons
Easy Implementation
Let's implement the preceding logic circuits with Python. Here, we will define the AND function, which takes x1
and x2
as arguments:
def AND(x1, x2): w1, w2, theta = 0.5, 0.5, 0.7 tmp = x1*w1 + x2*w2 if tmp <= theta: return 0 elif tmp > theta: return 1
The w1
, w2
, and theta
parameters are initialized within the function. When the sum of the weighted inputs exceeds the threshold, it returns 1; otherwise, it returns 0. Let's check that the outputs are the same as the ones shown in Figure 2.2:
AND(0, 0) # 0 (output) AND(1, 0) # 0 (output) AND(0, 1) # 0 (output) AND(1, 1) # 1 (output)
The outputs are as we expected. With that, you have built an AND gate. Although you can use a similar procedure to build a NAND or OR gate,...