DNN์์ Convolutional ๋ฐ FC Layers์ ๋ํ ๊ฐ์ค์น(Weight)๊ฐ ํน์ ๋ฐฉ์์ผ๋ก ์ด๊ธฐํ(Initializing).
ResNet ๋คํธ์ํฌ(https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py)
๊ฐ์ค์น๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ PyTorch ์ฝ๋
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
- ๊ฐ์ค์น๋ ํ๊ท ์ด 0์ธ ์ ๊ท ๋ถํฌ์ ํํฐ ์ปค๋ ์ฐจ์์ ํจ์์ธ ํ์ค ํธ์ฐจ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๊ธฐํ๋ฉ๋๋ค.
- ์ด๊ฒ์ ๋คํธ์ํฌ ๊ณ์ธต์ ์ถ๋ ฅ ๋ถ์ฐ์ด ์ฌ๋ผ์ง๊ฑฐ๋ ํญ๋ฐํ๋ ๊ฒ, ์ฆ ๋งค์ฐ ์ปค์ง๋ ๋์ ํฉ๋ฆฌ์ ์ธ ํ๊ณ ๋ด์์ ๊ฒฝ๊ณ๋ฅผ ์ ์งํ๋๋ก ํ๊ธฐ ์ํด ์ํ๋ฉ๋๋ค.
- ์ด ์ด๊ธฐํ ๋ฐฉ๋ฒ์ Kaiming He et al.์ ๋ค์ ๋ ผ๋ฌธ์ ์์ธํ ์ค๋ช ๋์ด ์์ต๋๋ค.
Section 1: Convolution์ Matrix Multiplication์ผ๋ก ๊ตฌํ
Section 2: Forward Pass (Without Bias)
Section 3: Forward Pass (With Bias)
Section 4: Other Rectifiers
- tanh ๋ฐ Sigmoid ํจ์์ ๊ฐ์ด ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ธฐํ rectifiers(์ ๋ฅ๊ธฐ: ๋ฐ๊ฟ์ฃผ๋ ์ฅ์น)๋ฅผ ๊ณ ๋ คํฉ๋๋ค.
Section 1: Convolution์ Matrix Multiplication์ผ๋ก ๊ตฌํ
์ปจ๋ณผ๋ฃจ์ ๊ณผ ํ๋ ฌ ๊ณฑ์ ์ ๋ค๋ฅธ ์ฐ์ฐ์ ๋๋ค. ํ๋ ฌ ๊ณฑ์ ์ ์ฌ์ฉํ์ฌ ์ปจ๋ณผ๋ฃจ์ ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ ๋ ฅ ํ๋ ฌ(๋๋ ์ปค๋ ํ๋ ฌ)์ ์ ์ ํ๊ฒ ํผ์น๋ฉด ํ๋ ฌ ๊ณฑ์ ์ผ๋ก ์ปจ๋ณผ๋ฃจ์ ์ ๊ตฌํํ ์ ์์ต๋๋ค.
์ ๋ ฅ ํ๋ ฌ์ ํผ์น๊ณ ํ๋ ฌ ๊ณฑ์ ์ผ๋ก ์๊ด ๊ด๊ณ๋ฅผ ๊ตฌํํ๋ ํ์ด์ฌ ์ฝ๋
from scipy import signal
from scipy import misc
import numpy as np
from numpy import zeros
def unfold_matrix(X, k):
n, m = X.shape[0:2]
xx = zeros(((n - k + 1) * (m - k + 1), k**2))
row_num = 0
def make_row(x):
return x.flatten()
for i in range(n- k+ 1):
for j in range(m - k + 1):
#collect block of m*m elements and convert to row
xx[row_num,:] = make_row(X[i:i+k, j:j+k])
row_num = row_num + 1
return xx
w = np.array([[1, 2, 3], [4, 5, 6], [-1, -2, -3]], np.float32)
#x = np.random.randn(5,5)
x = np.array([[-0.21556299, -0.11002319, -0.3499612, 1.49290769, -0.50435978],
[ 0.06348409, 0.66873375, 0.14251138, -1.6414004 , -0.91561852],
[-2.52451962, -1.97544675, -0.24609529, -1.11489934, -1.44793437],
[ 1.26260575, -0.62047366, 0.12274525, 0.25200227, -0.83925847],
[-1.54336488, -0.05100702, 0.36608208, 0.51712927, -0.97133877],
[-1.54336488, -0.05100702, 0.36608208, 0.51712927, -0.97133877]])
n, m = x.shape[0:2]
k = w.shape[0]
y = signal.correlate2d(x, w, mode='valid')
x_unfolded = unfold_matrix(x, k)
w_flat = w.flatten()
yy = np.matmul(x_unfolded, w_flat)
yy = yy.reshape((n-k+1, m-k+1))
print(yy)
# verify yy = y
Section 2: Forward Pass (Without Bias)
๋คํธ์ํฌ๊ฐ ๊น์ด์ง์ ๋ฐ๋ผ ๋คํธ์ํฌ ์ถ๋ ฅ์ ๋ถ์ฐ์ด ์ฌ๋ผ์ง๊ฑฐ๋ ๊ณผ๋ํ๊ฒ ์ปค์ง๋ ๋์ ๊ฒฝ๊ณ๋ฅผ ์ ์งํ๋๋ก ๊ฐ์ค์น์ ๋ํ ์ ์ ํ ๋ถ์ฐ์ ์ ํํ๋ ๊ฒ
# number of layers
num_layers = 10
class layer(object):
def __init__(self, _m, _n):
#n: filter size (width)
#m: filter size (height)
self.m = _m
self.n = _n
self.activation = 'relu'
#self.activation = 'tanh'
#self.activation = 'sigmoid'
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, x, use_bias = False):
#x is a row vector
self.W = np.random.normal(0, np.sqrt(2.0/self.m), (self.m,self.n))
self.b = np.random.normal(0, np.sqrt(2.0/num_layers), self.n)
# self.b = 1. - 2*np.random.rand(1, 5)
self.y = np.dot(x, self.W)
if (use_bias):
self.y = self.y + self.b
if (self.activation == 'relu'):
self.a = np.maximum(0., self.y)
if (self.activation == 'tanh'):
self.a = np.tanh(self.y)
if (self.activation == 'sigmoid'):
self.a = self.sigmoid(self.y)
return self.a, self.y
layers = []
# even numbered layers have a 5*10 weight matrix
# odd numbered layers have a 10*5 weight matrix
for i in range(num_layers):
layers.append(layer(5 if(i % 2 == 0) else 10, 10 if(i % 2 == 0) else 5))
num_trials = 100000
# records the network output (activations of the last layer)
a = np.zeros((num_trials, 5))
# records the network input
i = np.zeros((num_trials, 5))
# record the activations
y = np.zeros((num_trials, 5))
for trial in range(0,num_trials):
# input to the network is uniformly distributed numbers in (0,1). E(x) != 0.
# Note that the distribution of the input is different from the distribution of the weights.
x = 3*np.random.rand(1, 5)
i[trial, :] = x
for layer_no in range(0,num_layers):
x, y_ = layers[layer_no].forward(x, False)
a[trial, :] = x
y[trial, :] = y_
#E(x^2) (expected value of the square of the input)
E_x2 = np.mean(np.multiply(i,i), 0)
# E(a^2) (expected value of the square of the activations of the last layer)
E_a2 = np.mean(np.multiply(a,a), 0)
# verify E_a2 ~ E_x2
# var(y): Variance of the output before applying activation function
Var_y = np.var(y,0)
# verify Var_y ~ 2*E_a2
Section 3: Forward Pass (With Bias)
๊ฐ์ค์น์ ํธํฅ์ ๋ค์๊ณผ ๊ฐ์ด ์ด๊ธฐํํฉ๋๋ค.
self.W = np.random.normal(0, np.sqrt(2.0/self.m), (self.m,self.n))
self.b = np.random.normal(0, np.sqrt(2.0/num_layers), self.n)
๋คํธ์ํฌ๋ฅผ ์คํํ๋ ๋์ use_bias = True๋ก ์ค์ ํฉ๋๋ค.
for layer_no in range(0,num_layers):
x, y_ = layers[layer_no].forward(x, True)
Backward Pass
Backward Pass๋ฅผ ๊ณ ๋ คํ ๋ ์ด๊ธฐํ ๋ฐฉ๋ฒ์ ์์ ํ ํ์๊ฐ ์๋ค๋ ๊ฒ์ด ๋ฐํ์ก์ต๋๋ค. ์ด๋ ์ญ๋ฐฉํฅ ํจ์ค ๋์ ์์ ํ ์ฐ๊ฒฐ๋ ์ปจ๋ณผ๋ฃจ์ ๋ ์ด์ด๋ฅผ ํตํด ๊ทธ๋ผ๋์ธํธ๋ฅผ ์ ํํ๋ฉด ์ฐจ์์ด ์ฝ๊ฐ ๋ค๋ฅธ ํ๋ ฌ ๊ณฑ์ ๊ณผ ์ปจ๋ณผ๋ฃจ์ ์ด ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
Section 4: Other Rectifiers
์๊ทธ๋ชจ์ด๋ ํ์ฑํ ํจ์์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๊ณ ํธํฅ์ ์ถ๊ฐํ์ฌ ์์ ํ ์ ์์ต๋๋ค.
ํดํด์ผ ํ ํต์ฌ์ ์ ๊ท ๋ถํฌ๋ฅผ ์ํ๋งํ์ฌ ๊ฐ์ค์น๋ฅผ ์ด๊ธฐํํ๋ ํ์ค ๋ฐฉ๋ฒ u=0์ด๋ค.
ReLU ํ์ฑํ ํจ์๋ฅผ ์ํด ์ค๊ณ๋์์ผ๋ฉฐ tanh ํ์ฑํ์๋ ์ ์๋ํ์ง๋ง S์ํ์๋ ์ ์๋ํ์ง ์์ต๋๋ค.
์ฐธ๊ณ : TELESENS. Ankur
'๐ AI & Bigdata > AI & ML & DL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DL] TensorFlow- ์ฌ์ธต์ ๊ฒฝ๋ง ๋ชจ๋ธ ์ฝ๋ ๊ตฌํ (0) | 2022.08.19 |
---|---|
[DL] Faster R-CNN ๋คํธ์ํฌ ์ธ๋ถ ๊ตฌ์ฑ (0) | 2022.08.18 |
[DL] CNN ๋ถ๋ฅ ์ฑ๋ฅ ๋์ด๊ธฐ/ ๋ฐ์ดํฐ ์ฆ๊ฐ, Mix Image (0) | 2022.08.09 |
[DL] YOLO- ๋ผ์ฆ๋ฒ ๋ฆฌ ํ์ด๋ฅผ ์ด์ฉํ Object Recognition, Text Recognition -๋ฅ๋ฌ๋ ๊ธฐ์ YOLO ํ์ฉ (0) | 2022.08.09 |
[DL] Faster R-CNN (0) | 2022.08.02 |