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,...