☞ http://ya-n-ds.tistory.com/3231 : Deep Learning from Scratch (2)
Reference : Deep Learning from Scratch ( 사이토 고키, 한빛미디어 )
< Chap 1. 헬로 파이썬 >
# 환경 : Anaconda 배포판(Python 3) + Libary(넘파이, matplotlib)
https://www.continuum.io/downloads
# Python Interpreter : $ python -> 대화 모드
# 종료 : 'CTRL + D' @Linux or Mac, 'CTRL + Z' @Window
1.3.2 Data Type - checked by 'type()' function
e.g. type(10) -> int, typ("hello") -> str
1.3.3 Variable -> defined via alphabet : x, y, etc.
cf. Dynamic language : Type of variable is autonomously determined ( 초기화, 자동 형변환 )
cf. # : Start of comment
1.3.4 List - Array of data
>>> a=[1,2,3,4,5]
>>> print(a) -> [1,2,3,4,5]
>>> lent(a) -> 5
>>> a[0] -> 1 # 1st element
>>> a[4] = 99 # assign to 5th element
>>> print(a) -> [1,2,3,4,99]
>>> a[0:k] -> [1,2] # [0]~[k-1]
cf, -1: Last element, -2: Last-1 element
1.3.5 Dictionary - a pair of 'key' and 'value'
>>> me = {'height':180} # Initially assigned via {} -> Type is set
>>> me['height'] -> 180 # accessed via []
>>> me['weight'] = 70 # Addec via []
>>> print(me) -> {'height':180, 'weight':180}
1.3.6 bool - True, False
available operators : not, and, or
1.3.7 'if' statement
>>> hungry = True
>>> if hungry:
... # 4ea blank characters : following codes are executed when the condition is met
else:
...
1.3.7 'for' statement
>>> for i in[1,2,3]:
print(i)
1.3.8 Function
>>> def hello():
print("Hello World!")
>>> hello() -> Hello World!
>>> def hello(object):
print("Hello " + object + "!")
>>> hello("cat") -> Hello cat!
1.4 파이썬 스크립트 파일
1.4.1 파일로 저장
print("I'm hungry!") in hungry.py
$ python humgry.py -> I'm hungry!
1.4.2 Class -> User-defined data type
1.5 넘파이(numpy)
1.5.1
>>> import numpy as np
1.5.2 Array Generation
>>> x = np.array([1.0, 2.0, 3.0])
>>> print(x)
[1, 2, 3]
>>> type(x)
<class 'numpy.ndarray'>
1.5.3 넘파이 산술 연산
>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([2.0, 4.0, 6.0])
>>> x + y # element-wise addition -> same # of elements
array([3., 6., 9.])
cf. x-y, x*y, x/y
>>> x/2.0 # broadcast for array
array([0.5, 1., 1.5])
1.5.4 N-dimensional Array
>>> A = np.array([[1,2], [3,4]])
>>> B = np.array([[3,0], [0,6]])
>>> print(A)
[[1 2]
[3 4]]
>>> A.shape # shape : element size of each dimension
(2, 2)
>>> A.dtype
dtype('int64')
>>> A+B, A*B, A*10 # Element-wise, Broadcast
1.5.5 Broadcast -> 2nd element is changed to the 1st Array type in case of *
>>> A = np.array([[1,2], [3,4]])
>>> B = np.array([[10,20]])
>>> A * B
[1 2] * [10 20] = [10 40]
[3 4] [10 20] [30 80]
1.5.6 Element Access
>>> X = np.array([[51,55], [14,19], [0,4]])
>>> X[0]
array([51,55])
>>> X[0][1]
55
>>> for row in X:
print(row)
[51,55]
[14,19]
[0,4]
>>> X = X.flatten() # change to 1-dimensional array
>>> print(X)
[51 55 14 19 0 4]
>>> X[np.array([0,2,4])]
array([51, 14, 0])
>>> X > 15
array([True, True, False, True, False, False], dtype=bool)
>>> X[X>15]
array([51, 55, 19])
1.6 matplotlib
1.6.1 Simple Graph
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 6, 0.1) # from 0 to 6, step=0.1
y = np.sin(x)
plt.plot(x,y)
plt.show()
1.6.2 pyplot
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 6, 0.1) # from 0 to 6, step=0.1
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x,y1, label="sin")
plt.plot(x,y2, linestyle="--", label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title('sin & cos')
plt.legend()
plt.show()
1.6.3 Image presentation
import matplotlib.pyplot as plt
from matplotlib.image import imread
# import matplotlib.image as mpimg
img = imread('lena.png')
# img = mpimg.imread('../dataset/lena.png')
plt.imshow(img)
plt.show()
< Chap 2. 퍼셉트론 (Perceptron) >
2.1 Percepton : 다수의 신호를 받아서 하나의 신호를 출력
e.g. x1w1 + x2w2 -> y ( 0 or 1 : with θ(임계값) )
=> (w1, w2, θ)
2.3. Perceptron Implementation
2.3.1 Simple
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
2.3.2 Weight and Bias
y = 0 ( b + w1x1 + w2x2 <= 0 )
1 ( b + w1x1 + w2x2 > 0 )
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
def AND(x1,x2)
x = np.array([x1,x2])
w = np.array([0.5,0.5])
b = -0.7
tmp = x*w + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
cf. NAND : w = np.array([-0.5,-0.5]), b = 0.7
2.4 퍼셉트론의 한계
2.4.2 선형과 비선형
- 비선형 : XOR, etc. / 선형 : AND, NAND, etc.
2.5. Multi-layer Perceptron
2.5.2 XOR Gate 구현
def XOR(x1, x2)
s1 = NAND(x1,x2)
s2 = OR(x1,x2)
y = AND(s1,s2)
return y
# 0th layer(x1,x2), 1st layer(s1,s2), 2nd layer(y)