[Machine Learning] ์ ๊ฒฝ๋ง ๊ธฐ์ด 3
2023. 2. 8. 00:08
๐ง๐ป๐ป์ฉ์ด ์ ๋ฆฌ
์ฐ์๋ฒ์น - ์กฐ๊ธ ๋ ๋ณต์กํ ์ฐ์๋ฒ์น
์กฐ๊ธ ๋ ๋ณต์กํ ์ฐ์๋ฒ์น
์๋ฐฉ ๊ณ์ฐ
def chain_length_3(chain: Chain,
x: ndarray) -> ndarray:
'''
3๊ฐ์ ํจ์๋ฅผ ์ฐ์์ ์ผ๋ก ํ๊ฐํจ.
'''
assert len(chain) == 3, \
"์ธ์ chain์ ๊ธธ์ด๋ 3์ด์ฌ์ผ ํจ"
f1 = chain[0]
f2 = chain[1]
f3 = chain[2]
return f3(f2(f1(x)))
def chain_deriv_3(chain: Chain,
input_range: ndarray) -> ndarray:
'''
์ธ ํจ์๋ก ๊ตฌ์ฑ๋ ํจ์ฑํจ์์ ๋ํจ์๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด ์ฐ์๋ฒ์น์ ์ฌ์ฉํจ
(f3(f2(f1)))' = f3'(f2(f1(x))) * f2'(f1(x)) * f1'(x)
'''
assert len(chain) == 3, \
"This function requires 'Chain' objects to have length 3"
f1 = chain[0]
f2 = chain[1]
f3 = chain[2]
# f1(x)
f1_of_x = f1(input_range)
# f2(f1(x))
f2_of_x = f2(f1_of_x)
# df3du
df3du = deriv(f3, f2_of_x)
# df2du
df2du = deriv(f2, f1_of_x)
# df1dx
df1dx = deriv(f1, input_range)
# ๊ฐ ์ ๋ผ๋ฆฌ ๊ฐ์ ๊ณฑํจ
return df1dx * df2du * df3du
def square(x: ndarray) -> ndarray:
'''
์ธ์๋ก ๋ฐ์ ndarray ๋ฐฐ์ด์ ๊ฐ ์์๊ฐ์ ์ ๊ณฑํ๋ค.
'''
return np.power(x, 2)
def leaky_relu(x: ndarray) -> ndarray:
'''
ndarry ๋ฐฐ์ด์ ๊ฐ ์์์ 'Leaky ReLU' ํจ์๋ฅผ ์ ์ฉํ๋ค.
'''
return np.maximum(0.2 * x, x)
def sigmoid(x: ndarray) -> ndarray:
'''
์
๋ ฅ์ผ๋ก ๋ฐ์ ndarray์ ๊ฐ ์์์ ๋ํ sigmoid ํจ์ซ๊ฐ์ ๊ณ์ฐํ๋ค.
'''
return 1 / (1 + np.exp(-x))
def plot_chain(ax,
chain: Chain,
input_range: ndarray,
length: int=2) -> None:
'''
์ฐ์๋ฒ์น์ ์ด์ฉํด ํฉ์ฑํจ์์ ๋ํจ์๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ๋ํ๋ฅผ ์๋ํจ.
ax: ์๋์ ์ฌ์ฉํ matplotlib์ ์๋ธํ๋กฏ
'''
assert input_range.ndim == 1, \
"input_range๋ 1์ฐจ์ ndarray์ฌ์ผ ํจ"
if length == 2:
output_range = chain_length_2(chain, input_range)
elif length == 3:
output_range = chain_length_3(chain, input_range)
ax.plot(input_range, output_range)
def plot_chain_deriv(ax,
chain: Chain,
input_range: ndarray,
length: int=2) -> ndarray:
'''
์ฐ์๋ฒ์น์ ์ด์ฉํด ํฉ์ฑํจ์์ ๋ํจ์๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ๋ํ๋ฅผ ์๋ํจ.
ax: ์๋์ ์ฌ์ฉํ matplotlib์ ์๋ธํ๋กฏ
'''
if length == 2:
output_range = chain_deriv_2(chain, input_range)
elif length == 3:
output_range = chain_deriv_3(chain, input_range)
ax.plot(input_range, output_range)
fig, ax = plt.subplots(1, 2, sharey=True, figsize=(16, 8)) # 2 Rows, 1 Col
chain_1 = [leaky_relu, square, sigmoid]
chain_2 = [leaky_relu, sigmoid, square]
PLOT_RANGE = np.arange(-3, 3, 0.01)
plot_chain(ax[0], chain_1, PLOT_RANGE, length=3)
plot_chain_deriv(ax[0], chain_1, PLOT_RANGE, length=3)
ax[0].legend(["$f(x)$", "$\\frac{df}{dx}$"])
ax[0].set_title("$f(x) = sigmoid(square(leakyRrelu(x)))$์ ํจ์์ ๋ํจ์")
plot_chain(ax[1], chain_2, PLOT_RANGE, length=3)
plot_chain_deriv(ax[1], chain_2, PLOT_RANGE, length=3)
ax[1].legend(["$f(x)$", "$\\frac{df}{dx}$"])
ax[1].set_title("$f(x) = square(sigmoid(leakyRelu(x)))$์ ํจ์์ ๋ํจ์")
์ด๋ฅผ ์คํํ๋ฉด ์์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
'Artificial Intelligence > Deep Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Deep Learning] - Neural Networks (0) | 2023.03.26 |
---|---|
[Supervised Learning] ์ง๋ ํ์ต (0) | 2023.02.08 |
[Machine Learning] ์ ๊ฒฝ๋ง ๊ธฐ์ด 2 (2) | 2023.02.06 |
[XAI] ์ค๋ช ๊ฐ๋ฅํ AI (Explainable AI) (0) | 2023.01.26 |
[Convolutional Neural Networks and Image Classification] Part 3 (0) | 2023.01.24 |