hugo markdown의 latex 사용 예제 모음

Latex 사용 예제를 정리합니다. Latex를 Hugo markdown에서 사용한 사례입니다.

  • 벡터 내적

$$ \begin{align} \vec{a} \cdot \vec{b} &= |\vec{a}||\vec{b}|cos\theta \\
&= a_1b_1+a_2b_2 \end{align} $$

$$
\begin{align}
\vec{a} \cdot \vec{b} &= |\vec{a}||\vec{b}|cos\theta \\\\\
&= a_1b_1+a_2b_2
\end{align}
$$

  • Forward Propagation 주요 공식

$$ \begin{align} A^{[0]} & = Input Feature & (1) \\
Z^{[1]} & = W^{[1]}A^{[0]} & (2) \\
A^{[1]} & = g(Z^{[1]}) & (3) \\
Z^{[2]} & = W^{[2]}A^{[1]} & (4) \\
A^{[2]} & = g(Z^{[2]}) & (5) \end{align} $$

$$
\begin{align}
A^{\[0\]} & = Input Feature & (1) \\\\\
Z^{\[1\]} & = W^{\[1\]}A^{\[0\]} & (2) \\\\\
A^{\[1\]} & = g(Z^{\[1\]}) & (3) \\\\\
Z^{\[2\]} & = W^{\[2\]}A^{\[1\]} & (4) \\\\\
A^{\[2\]} & = g(Z^{\[2\]}) & (5)
\end{align}
$$

  • cost 미분을 위한 공식 정리

$$ \begin{align} Cost & = \frac{1}{2}(\hat{Y}-Y)^2 & (1)\\
\frac{\partial Cost}{\partial A^{[2]}} & = \hat{Y}-Y & (2)\\\
sigmoid(Z^{[l]]}) & = sigmoid(Z^{[l]]})(1-sigmoid(Z^{[l]]})) & (3)\\
& = A^{[l]}(1-A^{[l]}) \\
\frac{\partial Z^{[l]}}{\partial W^{[2]}} & = \frac{\partial A^{[l-1]}W^{[l]}}{\partial W^{[l]}} = A^{[l-1]} & (4) \\\
\end{align} $$

$$
\begin{align}
Cost & = \frac{1}{2}(\hat{Y}-Y)^2  & (1)\\\\\
\frac{\partial Cost}{\partial A^{\[2\]}} & = \hat{Y}-Y & (2)\\\\\  
sigmoid(Z^{\[l\]]}) & = sigmoid(Z^{\[l\]]})(1-sigmoid(Z^{\[l\]]})) & (3)\\\\\
& = A^{\[l\]}(1-A^{\[l\]}) \\\\\
\frac{\partial Z^{\[l\]}}{\partial W^{\[2\]}} & = \frac{\partial A^{\[l-1\]}W^{\[l\]}}{\partial W^{\[l\]}} = A^{\[l-1\]} & (4) \\\\\  
\end{align}
$$

  • weight 업데이트 식 유도

$$ \begin{align} \Delta W^{[2]} & = \frac{\partial Cost}{\partial W^{[2]}} \\
& = \frac{\partial Cost}{\partial A^{[2]}}\frac{\partial A^{[2]}}{\partial W^{[2]}} \\
& = \frac{\partial Cost}{\partial A^{[2]}}\frac{\partial A^{[2]}}{\partial Z^{[2]}}\frac{\partial Z^{[2]}}{\partial W^{[2]}} \\
& = (\hat{y}-y)\frac{\partial A^{[2]}}{\partial Z^{[2]}}\frac{\partial Z^{[2]}}{\partial W^{[2]}} \\
& = (\hat{y}-y)g’(z^{[2]})\frac{\partial Z^{[2]}}{\partial W^{[2]}} \\
& = (\hat{y}-y)sigmoid’(z^{[2]})\frac{\partial Z^{[2]}}{\partial W^{[2]}} \\
& = (\hat{y}-y)A^{[2]}(1-A^{[2]})\frac{\partial Z^{[2]}}{\partial W^{[2]}} \\
& = (\hat{y}-y)A^{[2]}(1-A^{[2]})A^{[1]} \end{align} $$

$$
\begin{align}
\Delta W^{\[2\]} & = \frac{\partial Cost}{\partial W^{\[2\]}} \\\\\
& = \frac{\partial Cost}{\partial A^{\[2\]}}\frac{\partial A^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  \frac{\partial Cost}{\partial A^{\[2\]}}\frac{\partial A^{\[2\]}}{\partial Z^{\[2\]}}\frac{\partial Z^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  (\hat{y}-y)\frac{\partial A^{\[2\]}}{\partial Z^{\[2\]}}\frac{\partial Z^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  (\hat{y}-y)g'(z^{\[2\]})\frac{\partial Z^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  (\hat{y}-y)sigmoid'(z^{\[2\]})\frac{\partial Z^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  (\hat{y}-y)A^{\[2\]}(1-A^{\[2\]})\frac{\partial Z^{\[2\]}}{\partial W^{\[2\]}} \\\\\
& =  (\hat{y}-y)A^{\[2\]}(1-A^{\[2\]})A^{\[1\]}
\end{align}
$$

  • Delta W 공식

$$ \begin{align} \Delta W^{[2]} & = \begin{bmatrix} \Delta w^{[1]}11 & \Delta w^{[1]}12 \\
\end{bmatrix} \\
& =\begin{bmatrix} (\hat{y}-y)a^{[2]}_1(1-a^{[2]}_1)a^{[1]}_1 & (\hat{y}-y)a^{[2]}_1(1-a^{[2]}_1)a^{[1]}_2 \\
\end{bmatrix} \\
& =(\hat{y}-y)a^{[2]}_1(1-a^{[2]}_1) \begin{bmatrix} a^{[1]}_1 & a^{[1]}_2 \\
\end{bmatrix} \end{align} $$

$$
\begin{align}
\Delta W^{\[2\]} & =
  \begin{bmatrix}
    \Delta w^{\[1\]}11 & \Delta w^{\[1\]}12   \\\\\
  \end{bmatrix} \\\\\
  & =\begin{bmatrix}
    (\hat{y}-y)a^{\[2\]}_1(1-a^{\[2\]}_1)a^{\[1\]}_1 & (\hat{y}-y)a^{\[2\]}_1(1-a^{\[2\]}_1)a^{\[1\]}_2   \\\\\
  \end{bmatrix} \\\\\
  & =(\hat{y}-y)a^{\[2\]}_1(1-a^{\[2\]}_1) \begin{bmatrix}
    a^{\[1\]}_1 & a^{\[1\]}_2   \\\\\
  \end{bmatrix}
\end{align}
$$

  • 출력 데이터 사이즈 계산

$$ \begin{align} OutputRowSize & = \frac{InputRowSize}{PoolingSize} \\
OutputColumnSize & = \frac{InputColumnSize}{PoolingSize} \end{align} $$

$$
\begin{align}
OutputRowSize & = \frac{InputRowSize}{PoolingSize} \\\\\
OutputColumnSize & = \frac{InputColumnSize}{PoolingSize}
\end{align}
$$

  • Max Pooling 출력 사이즈 계산

$$ \begin{align} Row Size & = \frac{36}{2} = 18 \\
Column Size & = \frac{28}{2} = 14
\end{align} $$

$$
\begin{align}
Row Size & = \frac{36}{2} = 18 \\\\\
Column Size & =  \frac{28}{2} = 14  
\end{align}
$$

  • Convolution Layer 2의 Activation Map 크기 계산

$$ \begin{align} Row Size & = \frac{N-F}{Strid} + 1 = \frac{18-3}{1} + 1 = 16 \\
Column Size & = \frac{N-F}{Strid} + 1 = \frac{14-3}{1} + 1 = 12
\end{align} $$

$$
\begin{align}
Row Size & = \frac{N-F}{Strid} + 1 = \frac{18-3}{1} + 1 = 16 \\\\\
Column Size & =  \frac{N-F}{Strid} + 1 = \frac{14-3}{1} + 1 = 12  
\end{align}
$$

  • Regularization에서는 Lose 함수

$$ cost(W, b) = \frac{1}{m}\sum_i^m{L(\hat{y^i}, y^i)} + \lambda\frac{1}{2}||w||^2 $$

$$
cost(W, b) = \frac{1}{m}\sum_i^m{L(\hat{y^i}, y^i)} + \lambda\frac{1}{2}||w||^2
$$
</font>

  • 원점에서 벡터 좌표까지의 거리

$$ L_p = (\sum_i^n |x_i|^p)^{\frac{1}{p}} $$

$$
L_p = (\sum_i^n |x_i|^p)^{\frac{1}{p}}
$$
</font>

  • L1 Norm

$$ \begin{align} L_1 & = (\sum_i^n |x_i|) \\
& = |x_1| + |x_2| + |x_3| + …. + |x_n|
\end{align} $$

$$
\begin{align}
L_1 & = (\sum_i^n |x_i|) \\\\\
& = |x_1| + |x_2| + |x_3| + .... + |x_n|  
\end{align}
$$

  • L1 Norm 계산

$$ \begin{align} x & = [1, 2, 3, 4, 5] \\
||x||_1 & = (|1| +|2| +|3| +|4| +|5|) \\
& = 15 \end{align} $$

$$
\begin{align}
x & = [1, 2, 3, 4, 5] \\\\\
||x||_1 & = (|1| +|2| +|3| +|4| +|5|) \\\\\
& = 15
\end{align}
$$

  • tanh

$$ \begin{align} tanh(x) & = \frac{1-e^{-x}}{1+e^{-x}} \\
& = \frac{2}{1+e^{-2x}} -1 \end{align} $$

>$$
\begin{align}
tanh(x) & = \frac{1-e^{-x}}{1+e^{-x}} \\\\\
& = \frac{2}{1+e^{-2x}} -1
\end{align}
$$

  • tanh의 sigmoid 표현

Hyperbolic Tangent(tanh)은 Sigmoid와 유사한 특징을 갖습니다. Sigmoid 형태로 표현할 수 있습니다.

$$ \begin{align} sigmoid(x) & = \frac{1}{1+e^{-x}} \\
tanh(x) & = 2sigmoid(2x) -1 \end{align} $$

>$$
\begin{align}
sigmoid(x) & = \frac{1}{1+e^{-x}}  \\\\\
tanh(x) & = 2sigmoid(2x) -1
\end{align}
$$

  • 주요 미분 공식

$$ \begin{align} (e^{x})’ &= e^{x} \\
(e^{-x})’ &= -e^{-x} \\
\big[ \frac{f(x)}{g(x)}\big]’ & = \frac{f’(x)g(x)-f(x)g’(x)}{g^2(x)} \end{align} $$

$$
\begin{align}
(e^{x})' &= e^{x} \\\\\
(e^{-x})' &= -e^{-x} \\\\\
\big[ \frac{f(x)}{g(x)}\big]' & = \frac{f'(x)g(x)-f(x)g'(x)}{g^2(x)}
\end{align}
$$

  • tanh 함수 미분

$$ \begin{align} f(x) & = e^{x}-e^{-x} \\
g(x) & = e^{x}+e^{-x} \\
\frac{d}{dx}tanh(x) & = \big[\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}\big]’ \\
& = \big[\frac{f(x)}{g(x)}\big]’ \\
& = \frac{f’(x)g(x)-f(x)g’(x)}{g^2(x)} \\
& = \frac{(e^{x}-(-e^{-x}))(e^{x}+e^{-x}) - (e^{x}-e^{-x})(e^{x}-e^{-x} )}{(1+e^{-x})^2} \\
& = \frac{(e^{x}+e^{-x})^2 - (e^{x}-e^{-x})^2}{(e^{x}+e^{-x})^2} \\
& = \frac{(e^{x}+e^{-x})^2}{(e^{x}+e^{-x})^2} - \frac{(e^{x}-e^{-x})^2}{(e^{x}+e^{-x})^2} \\
& = 1 - \big[\frac{(e^{x}-e^{-x})}{(e^{x}+e^{-x})}\big]^2 \\
& = 1 - tanh^2(x) \\
& = (1 - tanh(x))(1 + tanh(x)) \end{align} $$

>$$
\begin{align}
f(x) & = e^{x}-e^{-x} \\\\\
g(x) & = e^{x}+e^{-x} \\\\\
\frac{d}{dx}tanh(x) & = \big[\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}\big]' \\\\\
 & =  \big[\frac{f(x)}{g(x)}\big]' \\\\\
 & = \frac{f'(x)g(x)-f(x)g'(x)}{g^2(x)} \\\\\
 & = \frac{(e^{x}-(-e^{-x}))(e^{x}+e^{-x}) - (e^{x}-e^{-x})(e^{x}-e^{-x} )}{(1+e^{-x})^2} \\\\\
 & = \frac{(e^{x}+e^{-x})^2 - (e^{x}-e^{-x})^2}{(e^{x}+e^{-x})^2} \\\\\
 & = \frac{(e^{x}+e^{-x})^2}{(e^{x}+e^{-x})^2} - \frac{(e^{x}-e^{-x})^2}{(e^{x}+e^{-x})^2} \\\\\
 & = 1 - \big[\frac{(e^{x}-e^{-x})}{(e^{x}+e^{-x})}\big]^2 \\\\\
 & = 1 - tanh^2(x) \\\\\
 & = (1 - tanh(x))(1 + tanh(x))
\end{align}
$$

  • tanh 미분 결과

$$ \begin{align} \frac{d}{dx}tanh(x) & = 1- tanh^2(x) \\
& = (1-tanh(x))(1+tanh(x)) \\
\end{align} $$

>$$
\begin{align}
\frac{d}{dx}tanh(x) & = 1- tanh^2(x) \\\\\
 & = (1-tanh(x))(1+tanh(x)) \\\\\
\end{align}
$$