-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCh02.Perceptron.Rmd
200 lines (158 loc) · 2.64 KB
/
Ch02.Perceptron.Rmd
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
---
title: "Ch2.Perceptron"
author: "Soo-Heang EO, PhD"
date: '2017 1 10'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## 2.3 Perceptron 구현하기
### 2.3.1. 간단한 구현부터
#### AND게이트 구현
변수가 2개인 경우, AND게이트의 퍼셉트론 표현
* Python version
```{python}
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
```
* R version
```{r}
AND <- function(x1, x2){
w1 = w2 = 0.5
theta = 0.7
tmp = x1*w1 + x2*w2
if(tmp <= theta)
return(0)
else
return(1)
}
```
* Check!!
```{r}
AND(0,0)
AND(1,0)
AND(0,1)
AND(1,1)
```
### 2.3.2. 가중치와 편향도입
$$
y =
\begin{cases}
0, & b + w_1 x_1 + w_2 x_2 \le 0 \\
1, & b + w_1 x_1 + w_2 x_2 > 0
\end{cases}
$$
where $b$ is bias (편향), $w$ is weight(가중치), and $x$ is variable(입력).
* Python version
```{python}
import numpy as np
x = np.array([0,1]) #입력
w = np.array([0.5, 0.5]) #가중치
b = -0.7 #편향
w*x
np.sum(w*x)
np.sum(w*x) + b
```
* R version
```{r}
x = c(0,1)
w = c(0.5, 0.5)
b = -0.7
w*x
sum(w*x)
sum(w*x) + b
```
### 2.3.3. 가중치와 편향 구현하기
가중치와 편향을 도입한 `AND게이트` 구현
* python version
```{python}
import numpy as np
def AND(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.7
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
```
* R version
```{r}
AND <- function(x1, x2){
x = c(x1, x2)
w = c(0.5, 0.5)
b = -0.7
tmp = sum(w*x) + b
if(tmp <= 0) {
return(0)
} else{
return(1)
}
}
# Test
AND(0,1)
```
`NAND게이트` 구현
* Python
```{python}
import numpy as np
def NAND(x1, x2):
x = np.array([x1, x2])
w = np.array([-0.5, -0.5]) # AND와는 가중치 w와 편향 b만 다름
b = 0.7
tmp = np.sum(w*x) + b
if tmp <= 0 :
return 0
else:
return 1
```
```{r}
AND <- function(x1, x2){
x = c(x1, x2)
w = c(-0.5, -0.5)
b = 0.7
tmp = sum(w*x) + b
if(tmp <= 0) {
return(0)
} else{
return(1)
}
}
```
`OR게이트` 구현
* Python
```{python}
import numpy as np
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5]) # AND와는 가중치 w와 편향 b만 다름
b = -0.2
tmp = np.sum(w*x) + b
if tmp <= 0 :
return 0
else:
return 1
```
* R
```{r}
AND <- function(x1, x2){
x = c(x1, x2)
w = c(0.5, 0.5)
b = -0.2
tmp = sum(w*x) + b
if(tmp <= 0) {
return(0)
} else{
return(1)
}
}
```
- `AND`, `NAND`, `OR`은 모두 같은 구조의 퍼셉트론
- 차이는 가충치와 편향에 있음