Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฒ์ž๋ผ๋‚˜๋Š”์ฒญ๋…„

์‹ ๊ฒฝ๋ง๋งŒ๋“ค๊ธฐ + ํ•™์Šต์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

์‹ ๊ฒฝ๋ง๋งŒ๋“ค๊ธฐ + ํ•™์Šต์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งŒ๋“ค๊ธฐ

JihyunLee 2019. 2. 24. 14:47
๋ฐ˜์‘ํ˜•

two layer์ธ ์‹ ๊ฒฝ๋ง์€ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ

์ž…๋ ฅ - hidden- ์ถœ๋ ฅ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.


๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜

1
2
3
4
5
6
7
8
 
    def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01):
        # ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”
        self.params = {}
        self.params['W1'= weight_init_std * np.random.randn(input_size, hidden_size)
        self.params['b1'= np.zeros(hidden_size)
        self.params['W2'= weight_init_std * np.random.randn(hidden_size, output_size)
        self.params['b2'= np.zeros(output_size)
cs

w1์—๋Š” ํฌ๊ธฐ๊ฐ€ (input_size, hidden_size)์ธ random์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

b1์—๋„ ํฌ๊ธฐ๊ฐ€(hidden_size)์ด๊ณ  0์ด ๋“ค์–ด๊ฐ„๋‹ค.


์˜ˆ์ธกํ•˜๋Š” ์ฝ”๋“œ


1
2
3
4
5
6
7
8
9
10
  def predict(self, x):
        W1, W2 = self.params['W1'], self.params['W2']
        b1, b2 = self.params['b1'], self.params['b2']
    
        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)
        
        return y
cs
๋ง์€ ์–ด๋ ค์›Œ๋ณด์ด์ง€๋งŒ ๊ตฌ์กฐ๋Š” ๋ณต์žกํ•˜์ง€ ์•Š๋‹ค.


์•„๋ž˜๋Š” loss ํ•จ์ˆ˜์˜์ฝ”๋“œ์ด๋‹ค.

๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.


# x : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ, t : ์ •๋‹ต ๋ ˆ์ด๋ธ”
def loss(self, x, t):
y = self.predict(x)
return cross_entropy_error(y, t)

์ •ํ™•๋„๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์ฝ”๋“œ
1
2
3
4
5
6
7
8
 def accuracy(self, x, t):
        y = self.predict(x)
        y = np.argmax(y, axis=1)
        t = np.argmax(t, axis=1)
        
        accuracy = np.sum(y == t) / float(x.shape[0])
        return accuracy
        
cs


๊ธฐ์šธ๊ธฐ๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
  def numerical_gradient(self, x, t):
        loss_W = lambda W: self.loss(x, t)
        
        grads = {}
        grads['W1'= numerical_gradient(loss_W, self.params['W1'])
        grads['b1'= numerical_gradient(loss_W, self.params['b1'])
        grads['W2'= numerical_gradient(loss_W, self.params['W2'])
        grads['b2'= numerical_gradient(loss_W, self.params['b2'])
        
        return grads
cs
๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ๋Š” ๊ธฐ์šธ๊ธฐ๋“ค์˜ ๋ฐฐ์—ด์ด ๋‚˜์˜จ๋‹ค.


ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งŒ๋“ค๊ธฐ


network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)


28*28์ธ ์ด๋ฏธ์ง€๋ฅผ input์œผ๋กœ ๋„ฃ๊ธฐ ๋•Œ๋ฌธ์— size๋Š” 784, output์€ 10๊ฐœ์˜ ์ˆซ์ž๊ธฐ ๋•Œ๋ฌธ์— 10์œผ๋กœ ์ง€์ •



# ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ
iters_num = 10000 # ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์ ์ ˆํžˆ ์„ค์ •ํ•œ๋‹ค.
train_size = x_train.shape[0]
batch_size = 100 # ๋ฏธ๋‹ˆ๋ฐฐ์น˜ ํฌ๊ธฐ
learning_rate = 0.1

ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ ์ ˆํžˆ ์ •ํ•ด์•ผ ํ•œ๋‹ค.

์•„๋ž˜๋Š” ํ•™์Šต์„ ํ•˜๋Š” ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 1์—ํญ๋‹น ๋ฐ˜๋ณต ์ˆ˜
iter_per_epoch = max(train_size / batch_size, 1)
 
for i in range(iters_num):
    # ๋ฏธ๋‹ˆ๋ฐฐ์น˜ ํš๋“
    batch_mask = np.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask]
    t_batch = t_train[batch_mask]
    
    # ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ
    grad = network.gradient(x_batch, t_batch)
    
    # ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐฑ์‹ 
    for key in ('W1''b1''W2''b2'):
        network.params[key] -= learning_rate * grad[key]
    
    # ํ•™์Šต ๊ฒฝ๊ณผ ๊ธฐ๋ก
    loss = network.loss(x_batch, t_batch)
    train_loss_list.append(loss)
    
    # 1์—ํญ๋‹น ์ •ํ™•๋„ ๊ณ„์‚ฐ
    if i % iter_per_epoch == 0:
        train_acc = network.accuracy(x_train, t_train)
        test_acc = network.accuracy(x_test, t_test)
        train_acc_list.append(train_acc)
        test_acc_list.append(test_acc)
        print("train acc, test acc | " + str(train_acc) + ", " + str(test_acc))
cs

epoch : ํ•™์Šต๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์†Œ์ง„ํ–ˆ์„๋•Œ์˜ ํšŸ์ˆ˜
ํ›ˆ๋ จ๋ฐ์ดํ„ฐ 10000๊ฐœ๋ฅผ 100๊ฐœ์˜ ๋ฏธ๋‹ˆ๋ฐฐ์น˜๋กœ ํ•™์Šตํ•  ๊ฒฝ์šฐ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ 100ํšŒ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ชจ๋“  ํ›ˆ๋ จ๋ฐ์ดํ„ฐ๋ฅผ ์†Œ์ง„ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๊ฒฝ์šฐ 100ํšŒ๊ฐ€ 1์—ํญ์ด ๋œ๋‹ค.


๊ฒฐ๊ณผ :




๋ฐ˜์‘ํ˜•