1. 계단 함수
def step_function(x): # x : np array
y = x > 0
return y.astype(np.int)
2. 시그모이드 함수
def sigmoid(x): # x : np array
return 1 / (1 + np.exp(-x))
3. ReLU 함수
def relu(x): # x : np array
return np.maximum(0, x)
4. 항등 함수
def identity_function(x): # x : np array
return x
5. 소프트맥스 함수
def softmax(x): # x : np array
c = np.max(a) # to prevent overflow
exp_a = np.exp(a - c)
sum_exp_a = np.sum(exp_a)
y = exp_a / sum_exp_a
return y
6. 신경망 구현
입력층의 뉴런 수 : 784개
출력층의 뉴런 수 : 10개
첫번째 은닉층의 뉴런 수 : 50개
두번째 은닉층의 뉴런 수 : 100개
모든 가중치와 편향 값은 network라는 이름의 딕셔너리 변수에 저장되어 있음
def predict(network, x): # x : np array (input)
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
7. 배치 처리를 이용한 신경망의 추론 정확도 계산
x, t = get_data() # x : np array (input), t : correct answers
network = init_network()
batch_size = 100
accuracy_cnt = 0
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:" + str(float(accuracy_cnt) / len(x)))
[출처 : 사이토 고키, Deep Learning from Scratch(2017), 개앞맵시 옮김, 한빛미디어]
'Machine Learning' 카테고리의 다른 글
[Deep Learning from Scratch] 5장. 오차역전파 (0) | 2020.01.08 |
---|---|
[Deep Learning from Scratch] 4장. 신경망 학습 (0) | 2020.01.08 |
[Deep Learning from Scratch] 3장. 신경망 - 이론 (0) | 2020.01.08 |
[Deep Learning from Scratch] 2장. 퍼셉트론 (0) | 2020.01.08 |
딥러닝을 통한 Image Segmentation 입문!! (1) | 2019.12.02 |