참고) 해당 포스팅은 패스트 캠퍼스 100% 환급 챌린지 미션 기록용 포스팅이며, 강좌 내용에 맞추어 포스팅함을 언급
클립명 : PART4)딥러닝의 3 STEP의 기초
가장 단순한 신경망을 통한 작동원리 05. (STEP 1) 이진 분류 문제,
가장 단순한 신경망을 통한 작동원리 06. (STEP 2) 다중 분류 문제,
가장 단순한 신경망을 통한 작동원리 07. (STEP 2) 얕은 신경망 구현
이번 포스팅에서는 코드 구현을 통해 얕은 신경망이 어떻게 작동하는지 파악해본다
import numpy as np
import matplotlib.pyplot as plt
# Sigmoid 함수
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Softmax함수
def softmax(x):
e_x = np.exp(x)
return e_x / np.sum(e_x)
# Define newtwork architecture
class ShallowNN:
def __init__(self, num_input, num_hidden, num_output):
self.W_h = np.zeros((num_hidden, num_input), dtype = np.float32)
self.b_h = np.zeros((num_hidden,), dtype = np.float32)
self.W_o = np.zeros((num_output, num_hidden), dtype = np.float32)
self.b_o = np.zeros((num_output,), dtype = np.float32)
def __call__(self, x):
h = sigmoid(np.matmul(self.W_h, x) + self.b_h)
return softmax(np.matmul(self.W_o, h) + self.b_o)
# 참고 __call__함수에서 np.matmul 은 np.dot과 같은 의미며, 내적을 의미함
# 내가 알고있는데로 두 벡터의 내적은 sum of elements이고, 행렬의 내적은
# 내가 알고있는 행렬의 곱
# Import and organize dataset
dataset = np.load('ch2_dataset.npz')
inputs = dataset['inputs']
labels = dataset['labels']
# Create Model
model = ShallowNN(2, 128, 10)
weights = np.load('ch2_parameters.npz')
model.W_h = weights['W_h']
model.b_h = weights['b_h']
model.W_o = weights['W_o']
model.b_o = weights['b_o']
outputs = list()
for pt, label in zip(inputs, labels):
output = model(pt)
#print("output", output)
outputs.append(np.argmax(output))
print(np.argmax(output), label)
outputs = np.stack(outputs, axis = 0)
plt.figure()
for idx in range(10):
mask = labels == idx
plt.scatter(inputs[mask, 0], inputs[mask, 1])
plt.title('true_label')
plt.show()
plt.figure()
for idx in range(10):
mask = outputs == idx
plt.scatter(inputs[mask, 0], inputs[mask, 1])
plt.title('model_output')
plt.show()
(글자수 : 1500자 이상)
강의 소개 링크 : bit.ly/3cx6kMd
딥러닝/인공지능 올인원 패키지 Online. | 패스트캠퍼스
Tensorflow2.0부터 Pytorch까지 딥러닝 대표 프레임워크를 정복하기. 생활 깊숙이 침투한 인공지능, 그 중심엔 딥러닝이 있습니다. 가장 강력한 머신러닝의 툴로서 주목받는 딥러닝은 생각보다 어려
www.fastcampus.co.kr
'Deep Learning & AI > AI' 카테고리의 다른 글
얕은 신경망에서 경사하강 학습법 실습 (0) | 2021.03.25 |
---|---|
쉽게 배우는 경사하강 학습법 4 ~ 6 (0) | 2021.03.24 |
딥러닝의 3 STEP의 기초 (7 ~ 9강) (0) | 2021.03.15 |
딥러닝의 3 STEP의 기초 (4~6강) (0) | 2021.03.09 |
딥러닝의 3 STEP의 기초 (1~3강) (0) | 2021.03.08 |