본문 바로가기
기계학습/[밑바닥부터 시작하는 딥러닝]

2장 퍼셉트론 코드들 모음

by tryotto 2019. 8. 6.

1) AND 구현


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def AND(x1, x2):
    w1, w2, theta = 0.50.50.7
    tmp=x1*w1+x2*w2;
 
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1
 
= AND(0,0)
print(a)
= AND(0,1)
print(a)
= AND(1,0)
print(a)
= AND(1,1)
print(a)
 
 
cs



2) numpy 가져와서 퍼셉트론 기본계산 구현하기 


1
2
3
4
5
6
7
8
9
import numpy as np
 
= np.array([01])
= np.array([0.50.5])
= -0.7;
 
print(w*x)
print(np.sum(w*x))
print(np.sum(w*x)+b)
cs


3) numpy 이용 퍼셉트론 구현 - 논리연산


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
 
def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.50.5])
    b = -0.7
 
    tmp = np.sum(x*w)+b
 
    if  tmp > 0:
        print("1")
    elif tmp <= 0:
        print("0")
 
AND(1,1)
 
 
def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5-0.5])
    b = 0.7
 
    tmp = np.sum(x * w) + b
 
    if tmp <= 0 :
        print("0")
    elif tmp>0 :
        print("1")
 
print(NAND(1,1))
 
 
def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.50.5])
    b = -0.7
 
    tmp = np.sum(x*w)+b
 
    if tmp <= 0:
        print("0")
    elif tmp > 0:
        print("1")
 
print(OR(1,1))
cs



4) numpy 이용해서 XOR 게이트 출력하기 - 다층퍼셉트론의 대두


1
2
3
4
5
6
7
8
9
10
11
def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
 
    print(y)
 
XOR(0,0)
XOR(1,0)
XOR(0,1)
XOR(1,1)
cs