간단한 3층 신경망 구현 코드
가중치와 편항이 하드 코딩된 3층 신경망
graph LR;
x1((x1))--w1_11-->b1((l11));
x2((x2))--w1_21-->b1((l11));
x1--w1_12-->b2((l12));
x2--w1_22-->b2;
x1--w1_13-->b3((l13));
x2--w1_23-->b3;
b1--w2_11-->c1((l21));
b2--w2_21-->c1;
b3--w2_31-->c1;
b1--w2_12-->c2((l22));
b2--w2_22-->c2;
b3--w2_32-->c2;
c1--w3_11-->y1((y1));
c1--w3_12-->y2((y2));
c2--w3_21-->y1((y1));
c2--w3_22-->y2((y2));
python 코드
import numpy as np
from sigmoid import sigmoid
def init_network(): ## 가충치, 편향 값을 세팅한다.
network = {}
network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])
network['b1'] = np.array([0.1, 0.2, 0.3])
network['W2'] = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]])
network['b2'] = np.array([0.1, 0.2])
network['W3'] = np.array([[0.1, 0.3], [0.2, 0.4]])
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']
x1 = np.dot(x, W1) + b1
z1 = sigmoid(x1)
x2 = np.dot(z1, W2) + b2
z2 = sigmoid(x2)
a3 = np.dot(z2, W3) + b3
y = a3 # 출력층 활성함수는 다르다. 여기서는 제외.
return y
network = init_network()
x = np.array([1.0, 0.5])
y = forward(network, x)
print(y)
출력
[0.31682708 0.69627909]
reference : 밑바닥부터 배우는 딥러닝