๋”ฅ๋Ÿฌ๋‹/Today I learned :

๋„˜ํŒŒ์ด๋กœ 3์ธต ์‹ ๊ฒฝ๋ง์˜ ์ˆœ๋ฐฉํ–ฅ ๊ณ„์‚ฐ ๊ตฌํ˜„

์ฃผ์˜ ๐Ÿฑ 2022. 12. 10. 12:56
728x90
๋ฐ˜์‘ํ˜•

๊ตฌํ˜„ํ•ด ๋ณผ 3์ธต ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์ธต(0์ธต) : 2๊ฐœ, ์€๋‹‰์ธต(1์ธต): 3๊ฐœ, ๋‘๋ฒˆ์งธ ์€๋‹‰์ธต(2์ธต): 2๊ฐœ, ์ธจ๋ ฅ์ธต(3์ธต)์€ 2๊ฐœ์˜ ๋‰ด๋Ÿฐ(๋…ธ๋“œ)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ์‹ ๊ฒฝ๋ง์—์„œ ์‹ ํ˜ธ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์‚ดํŽด๋ณด์ž

์ž์„ธํžˆ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ํ‘œ๊ธฐ๋ฒ•๋ถ€ํ„ฐ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋ณด์ž, ๊ฐ€์ค‘์น˜์™€ ์€๋Š‘์ธต์˜ ๋‰ด๋Ÿฐ ์œ„์—๋Š” (1)์ด ์ž‘๊ฒŒ ๋ถ™์–ด์žˆ๋‹ค. ์ด๋Š” 1์ธต์˜ ๊ฐ€์ค‘์น˜, 1์ธต์˜ ๋‰ด๋Ÿฐ์„ ๋œปํ•˜๋Š” ๋ฒˆํ˜ธ์ด๋‹ค. ๋˜ ๊ฐ€์ค‘์น˜์˜ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋‘ ์ˆซ์ž (1 2)๋Š” ์ฐจ๋ก€๋กœ ๋‹ค์Œ ์ธต ๋‰ด๋Ÿฐ๊ณผ ์•ž์ธต ๋‰ด๋Ÿฐ์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์ด๋‹ค. 1 2๋Š” ์•ž์ธต์˜ 2๋ฒˆ์งธ ๋‰ด๋Ÿฐ(x2)์—์„œ ๋‹ค์Œ์ธต์˜ 1๋ฒˆ์งธ ๋‰ด๋Ÿฐ (a1(1)๋กœ ํ–ฅํ•  ๋•Œ์˜ ๊ฐ€์ค‘์น˜๋ผ๋Š” ๋œป์ด๋‹ค. ์ˆœ์„œ๊ฐ€ '๋‹ค์Œ ์ธต ๋ฒˆํ˜ธ, ์•ž ์ธต ๋ฒˆํ˜ธ'์ž„์„ ์žŠ์ง€ ๋ง์ž!

 


๋‹ค์‹œ ์‹ ํ˜ธ ์ „๋‹ฌ ๊ณผ์ •์„ ๋ณด๋ฉด, ์šฐ์„  ์ถ”๊ฐ€๋œ 1์€ ํŽธํ–ฅ ๋‰ด๋Ÿฐ์ด๋‹ค. ํŽธํ–ฅ์€ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ์ธ๋ฑ์Šค๊ฐ€ ํ•˜๋‚˜๋ฐ–์— ์—†๋‹ค. ์ด๋Š” ์•ž ์ธต์˜ ํŽธํ–ฅ ๋‰ด๋Ÿฐ์ด ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

a1(1)์„ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ณด๋ฉด, 

์ด๋ฅผ ๊ฐ„์†Œํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

์œ„ ์‹์„ ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ด…์‹œ๋‹ค

import numpy as np
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์„ ๊ณ„์‚ฐํ–ˆ์œผ๋ฉด ํ™œ์„ฑํ™”ํ•จ์ˆ˜์˜ ์ฒ˜๋ฆฌ๋„ ์‚ดํŽด๋ด…์‹œ๋‹ค

๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์€๋‹‰์ธต์—์„œ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ(๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ์ดํ•ฉ)์€ a๋กœ ํ‘œ๊ธฐํ•˜๊ณ  ํ™œ์„ฑํ™” ํ•จ์ˆ˜h()์œผ๋กœ ๋ณ€ํ™˜๋œ ์‹ ํ˜ธ๋Š” z()๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™”ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ(์ €๋ฒˆ์‹œ๊ฐ„์—์„œ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค)๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

Z1=sigmoid(A1)

์ด์–ด์„œ 2์ธต์œผ๋กœ ๊ฐ€๋Š” ๊ณผ์ •์„ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

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)

 

๋งˆ์ง€๋ง‰์œผ๋กœ 2์ธต์—์„œ ์ถœ๋ ฅ์ธต์œผ๋กœ ์‹ ํ˜ธ ์ „๋‹ฌ์ž…๋‹ˆ๋‹ค. ์•ž์„  ๊ณผ์ •๊ณผ ๋‹ค๋ฅธ์ ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ธ๋ฐ, ์—ฌ๊ธฐ๋”๋Š” ํ•ญ๋“ฑํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋„ ๋‹ค๋ฆ„์„ ๋ช…์‹œํ•˜๊ธฐ์œ„ํ•ด h๊ฐ€ ์•„๋‹Œ ์‹œ๊ทธ๋งˆ๋กœ ๊ธฐํ˜ธํ‘œ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. 

def identity_func(X):
	return x

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_func(A3) # OR Y = A3

์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™”ํ•จ์ˆ˜๋Š” ํ’€๊ณ ์žํ•˜๋Š” ๋ฌธ์ œ์˜ ์„ฑ์งˆ์— ๋งž๊ฒŒ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํšŒ๊ท€์—๋Š” ํ•ญ๋“ฑํ•จ์ˆ˜, ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ถ„๋ฅ˜์—๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ, , ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์ถœ๋ ฅ์ธต ๊ตฌํ˜„์€ ๋‹ค์Œ์‹œ๊ฐ„์—์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

์ตœ์ข… ์ฝ”๋“œ ์ •๋ฆฌ

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']
    
    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

network = init_network()
x = np.array([1.0, 0.5])
y = forward(network, x)
print(y) # [0.31682708  0.69627909]
  • ์‹ ๊ฒฝ๋ง ๊ตฌํ˜„์˜ ๊ด€๋ก€์— ๋”ฐ๋ผ ๊ฐ€์ค‘์น˜๋งŒ ๋Œ€๋ฌธ์ž, ๊ทธ ์™ธ ํŽธํ–ฅ๊ณผ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ ๋ชจ๋‘ ์†Œ๋ฌธ์ž
  • init_network() : ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ดˆ๊ธฐํ™”, ์ด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜์ธ network ์— ์ €์žฅ
  • ๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜ network : ๊ฐ ์ธต์— ํ•„์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜(๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ) ์ €์žฅ
  • forward() : ์ž…๋ ฅ ์‹ ํ˜ธ๋ฅผ ์ถœ๋ ฅ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ฒ˜๋ฆฌ ๊ณผ์ • ๊ตฌํ˜„, ์‹ ํ˜ธ๊ฐ€ ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ ์ „๋‹ฌ(์ˆœ์ „ํŒŒ)
๋ฐ˜์‘ํ˜•