[Machine Learning] ์‹ ๊ฒฝ๋ง ๊ธฐ์ดˆ 1

2023. 1. 17. 23:29
๐Ÿง‘๐Ÿป‍๐Ÿ’ป์šฉ์–ด ์ •๋ฆฌ

ํ•ฉ์„ฑํ•จ์ˆ˜ - composite function
ํ•ฉ์„ฑํ•จ์ˆ˜์˜ ๋„ํ•จ์ˆ˜ - derivative
์—ฐ์‡„ - chain
์‹ ๊ฒฝ๋ง์˜ ๋™์ž‘ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ธฐ์ดˆ์ ์ธ ๋ฉ˜ํƒˆ ๋ชจ๋ธ์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹ ๊ฒฝ๋ง์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์„ฑ ์š”์†Œ
  • ์ˆ˜์‹
  • ์ฝ”๋“œ
  • ๋‹ค์ด์–ด๊ทธ๋žจ

 

ํ•จ์ˆ˜

 - ํ”„๋ž‘์Šค์˜ ์ฒ ํ•™์ž ๋ฅด๋„ค ๋ฐ์นด๋ฅดํŠธ๊ฐ€ ์ฒ˜์Œ ์ œ์•ˆํ•œ ๋ฐฉ๋ฒ•. ์ขŒํ‘œ์— ๋‚˜ํƒ€๋‚ด๊ธฐ.

 

 

1. ์ˆ˜์‹

 

 

2. ๋‹ค์ด์–ด๊ทธ๋žจ

 

ํ•จ์ˆ˜ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋ฐฉ๋ฒ• 1)

 

ํ•จ์ˆ˜ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋ฐฉ๋ฒ• 2)

 

 

 

3. ์ฝ”๋“œ

 

python์˜ Numpy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

 

์‹ ๊ฒฝ๋ง์—์„œ ๋‹ค๋ฃฐ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์— ๋‹ด๊ธด๋‹ค.

 

import numpy as np
print("ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ์—ฐ์‚ฐ")
a = [1, 2, 3]
b = [4, 5, 6]

print("a+b:", a+b)
try:
    print(a*b)
except TypeError:
    print("a*b ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด a*b์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์—†์Œ")
print()
print("๋„˜ํŒŒ์ด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ ์—ฐ์‚ฐ")
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print("a+b:",a+b)
print("a*b:",a*b)

ndarray class๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

import numpy as np
a = [[1, 2],[3, 4]]
print("a:")
a = np.array(a)
print(a)
print('a.sum(axis=0):', a.sum(axis=0))
print('a.sum(axis=1):', a.sum(axis=1))

 

a = np.array([[1,2,3],[4,5,6]])
b = np.array([10,20,30])
print("a+b:")
print(a+b)

 

 

๋„˜ํŒŒ์ด๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

def square(x:ndarray) -> ndarray:
    '''
    ์ธ์ž๋กœ ๋ฐ›์€ ndarray ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Ÿ๊ฐ’์„ ์ œ๊ณฑํ•œ๋‹ค.
    '''
    
    return np.power(x,2)
def leaky_relu(x:ndarray) -> ndarray:
    '''
    ndarray ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— 'Leaky ReLU' ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค.
    '''
    return np.maximum(0.2 * x, x)

 

 

 

 

 

๋„ํ•จ์ˆ˜

-> ํ•จ์ˆซ๊ฐ’์˜ ๋ณ€ํ™”์œจ

 

 

1. ์ˆ˜์‹

 

๋„ํ•จ์ˆ˜ ๋ณ€ํ™”์œจ

 

2. ๋‹ค์ด์–ด๊ทธ๋žจ

๋„ํ•จ์ˆ˜ ๋‹ค์ด์–ด๊ทธ๋žจ

 

3. ์ฝ”๋“œ

from typing import Callable

def deriv(func : Callable[[ndarray], ndarray],
         input_:ndarray,
         delta: float = 0.001) -> ndarray:
    '''
    ๋ฐฐ์—ด input์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ํ•จ์ˆ˜ func์˜ ๋„ํ•จ์ˆซ๊ฐ’ ๊ณ„์‚ฐ
    '''
    return (func(input_ + delta) - func(input_ - delta)) / (2 * delta)

 

 

ํ•ฉ์„ฑํ•จ์ˆ˜

-> ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์•ˆ๋Š”๋‹ค.

 

1. ๋‹ค์ด์–ด๊ทธ๋žจ

 

ํ•ฉ์„ฑํ•จ์ˆ˜ ๋‹ค์ด์–ด๊ทธ๋žจ

2. ์ˆ˜์‹

 

ํ•ฉ์„ฑํ•จ์ˆ˜ ์ˆ˜์‹

3. ์ฝ”๋“œ

from typing import List

# ndarray๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ  ndarray๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
Array_Function = Callable[[ndarray], ndarray]

# Chain์€ ํ•จ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋‹ค.
Chain = List[Array_Function]

 

 

def chain_length_2(chain: Chain,
                  a: ndarray) -> ndarray:
    '''
    ๋‘ ํ•จ์ˆ˜๋ฅผ ์—ฐ์‡„(chain)์ ์œผ๋กœ ํ‰๊ฐ€
    '''
    assert len(chain) == 2, \
    "์ธ์ž chain์˜ ๊ธธ์ด๋Š” 2์—ฌ์•ผ ํ•จ"
    
    f1 = chain[0]
    f2 = chain[1]
    
    return f2(f1(x))

 

 

 

์ถœ์ฒ˜ : ํ•ด๋‹น ํฌ์ŠคํŒ…์€ [Deep Learning from Scratch] ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋”ฅ๋Ÿฌ๋‹์˜ ์ฑ… ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.

'Artificial Intelligence > Deep Learning' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Training Neural Networks] part 2  (0) 2023.01.24
[Deep Neural Network] part 1 - 2  (0) 2023.01.22
[Deep Neural Network] part 1 - 1  (0) 2023.01.22
more clever function  (0) 2021.12.19
Supervised learning examples && more clever  (0) 2021.12.16

BELATED ARTICLES

more