playdata
Deep Learning(0903_day1)
_JAEJAE_
2021. 9. 3. 17:14
퍼셉트론(간단한 논리회로)¶
- AND 게이트
In [126]:
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Image
In [1]:
# option 1
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
# option 2
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
print(AND(0, 0), AND(0, 1), AND(1, 0), AND(1, 1))
0 0 0 1
- NAND 게이트
In [2]:
def NAND(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
print(NAND(0, 0), NAND(0, 1), NAND(1, 0), NAND(1, 1))
1 1 1 0
- OR 게이트
In [3]:
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
print(OR(0, 0), OR(0, 1), OR(1, 0), OR(1, 1))
0 1 1 1
In [4]:
def XOR(x1, x2):
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
print(XOR(0, 0), XOR(0, 1), XOR(1, 0), XOR(1, 1))
0 1 1 0
신경망¶
활성화 함수¶
- 계단 함수
In [10]:
def step_function(x):
return np.array(x > 0, dtype=np.int32)
In [12]:
X = np.arange(-5.0, 5.0, 0.1)
Y = step_function(X)
plt.plot(X, Y)
plt.ylim(-0.1, 1.1) # y limit 설정
Out[12]:
(-0.1, 1.1)
- 시그모이드 함수
In [13]:
def sigmoid(x):
return 1 / (1 + np.exp(-x))
In [14]:
X = np.arange(-5.0, 5.0, 0.1)
Y = sigmoid(X)
plt.plot(X, Y)
plt.ylim(-0.1, 1.1)
Out[14]:
(-0.1, 1.1)
- ReLU 함수
In [15]:
def relu(x):
return np.maximum(0, x)
In [20]:
X = np.arange(-5.0, 5.0, 0.1)
Y = relu(X)
plt.plot(X, Y)
plt.ylim(-1.0, 6.0)
Out[20]:
(-1.0, 6.0)
- 다차원 배열 계산
In [127]:
Image("fig 3-11.png", width=400)
Out[127]:
In [25]:
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
np.dot(A, B)
Out[25]:
array([[19, 22], [43, 50]])
In [24]:
A@B
Out[24]:
array([[19, 22], [43, 50]])
In [128]:
Image("fig 3-12.png", width=400)
Out[128]:
In [28]:
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
C = np.dot(A, B)
print(C)
print(A.shape, B.shape, C.shape)
[[11 14 17 20] [23 30 37 44] [35 46 57 68]] (3, 2) (2, 4) (3, 4)
In [129]:
Image("fig 3-13.png", width=400)
Out[129]:
In [31]:
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([1, 2])
C = np.dot(A, B)
print(C)
print(A.shape, B.shape, C.shape)
[ 5 11 17] (3, 2) (2,) (3,)
신경망에서의 행렬곱¶
In [130]:
Image("fig 3-14.png", width=400)
Out[130]:
In [33]:
X = np.array([1, 2])
W = np.array([[1, 3, 5], [2, 4, 6]])
Y = np.dot(X, W)
print(Y)
print(X.shape, W.shape, Y.shape)
[ 5 11 17] (2,) (2, 3) (3,)
In [134]:
Image("fig 3-17.png", width=400)
Out[134]:
In [135]:
Image("e 3.9.png", width=200)
Out[135]:
In [138]:
X = np.array([1.0, 0.5])
W1 = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])
B1 = np.array([0.1, 0.2, 0.3])
A1 = np.dot(X, W1) + B1
A1
Out[138]:
array([0.3, 0.7, 1.1])
In [136]:
Image("fig 3-18.png", width=400)
Out[136]:
In [139]:
Z1 = sigmoid(A1)
Z1, Z1.shape
Out[139]:
(array([0.57444252, 0.66818777, 0.75026011]), (3,))
In [140]:
Image("fig 3-19.png", width=400)
Out[140]:
In [44]:
W2 = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]])
B2 = np.array([0.1, 0.2])
A2 = np.dot(Z1, W2) + B2
Z2 = sigmoid(A2)
Z2, Z2.shape
Out[44]:
(array([0.62624937, 0.7710107 ]), (2,))
In [141]:
Image("fig 3-20.png", width=400)
Out[141]:
In [47]:
def identity_function(x):
return x
In [49]:
W3 = np.array([[0.1, 0.3], [0.2, 0.4]])
B3 = np.array([0.1, 0.2])
A3 = np.dot(Z2, W3) + B3
Y = identity_function(A3)
Y, Y.shape
Out[49]:
(array([0.31682708, 0.69627909]), (2,))
3층 신경망 구현¶
In [62]:
def init_network():
network = {}
network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])
network['W2'] = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]])
network['W3'] = np.array([[0.1, 0.3], [0.2, 0.4]])
network['B1'] = np.array([0.1, 0.2, 0.3])
network['B2'] = np.array([0.1, 0.2])
network['B3'] = np.array([0.1, 0.2])
return network
def forward(network, X):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
B1, B2, B3 = network['B1'], network['B2'], network['B3']
A1 = np.dot(X, W1) + B1
Z1 = sigmoid(A1)
A2 = np.dot(Z1, W2) + B2
Z2 = sigmoid(A2)
A3 = np.dot(Z2, W3) + B3
Y = identity_function(A3)
return Y
In [65]:
network = init_network()
X = np.array([1.0, 0.5])
Y = forward(network, X)
Y
Out[65]:
array([0.31682708, 0.69627909])
출력층(소프트맥스 함수)¶
In [71]:
def softmax(a):
c = np.max(a)
exp_a = np.exp(a-c) # overflow 방지
sum_exp_a = np.sum(exp_a)
y = exp_a / sum_exp_a
return y
In [72]:
a = np.array([0.3, 2.9, 4.0])
softmax(a)
Out[72]:
array([0.01821127, 0.24519181, 0.73659691])
손글씨 숫자(MNIST)¶
In [74]:
import pickle
from dataset.mnist import load_mnist
- normalize=True: 0~1사이 값으로 나옴
- flatten=True: 한줄로 펴져서 나옴
- one_hot_label=True: label에 대해 원핫인코딩되어서 나옴
In [109]:
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
return x_test, t_test
def init_network():
with open('sample_weight.pkl', 'rb') as f:
network = pickle.load(f)
return network
def predict(network, X):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
A1 = np.dot(X, W1) + b1
Z1 = sigmoid(A1)
A2 = np.dot(Z1, W2) + b2
Z2 = sigmoid(A2)
A3 = np.dot(Z2, W3) + b3
Y = softmax(A3)
return Y
In [110]:
x, t = get_data()
network = init_network()
y = forward(network, x)
In [111]:
plt.imshow(x[0].reshape(28, -1), cmap='binary')
Out[111]:
<matplotlib.image.AxesImage at 0x1d839c44eb0>
In [145]:
keys = sorted(network.keys())
for key in keys:
print("{} : {}".format(key, network[key].shape))
W1 : (784, 50) W2 : (50, 100) W3 : (100, 10) b1 : (50,) b2 : (100,) b3 : (10,)
In [146]:
Image("fig 3-26.png", width=400)
Out[146]:
In [122]:
accuracy_cnt = 0
for i in range(len(x)):
y = predict(network, x[i])
p = np.argmax(y)
if p == t[i]:
accuracy_cnt += 1
print("Accuracy : ", accuracy_cnt / len(x))
Accuracy : 0.9352
배치처리¶
In [147]:
Image("fig 3-27.png", width=400)
Out[147]:
In [125]:
accuracy_cnt = 0
batch_size = 100
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]
y_batch = predict(network, x_batch)
p = np.argmax(y_batch, axis=1)
accuracy_cnt += np.sum(p == t[i:i+batch_size])
print("Accuracy : ", accuracy_cnt / len(x))
Accuracy : 0.9352