-
๋ณธ ๊ธ์ '๋ชจ๋๋ฅผ ์ํ ๋ฅ๋ฌ๋ ์์ฆ 2'์ 'pytorch๋ก ์์ํ๋ ๋ฅ ๋ฌ๋ ์ ๋ฌธ'์ ๋ณด๋ฉฐ ๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
ํ์์ ์๊ฒฌ์ด ์์ฌ ๋ค์ด๊ฐ ๋ถ์ ํํ ๋ด์ฉ์ด ์กด์ฌํ ์ ์์ต๋๋ค.
0. ํผ์ ํธ๋ก (Perceptron)
๋ค์์ ์ ๋ ฅ์ผ๋ก๋ถํฐ ํ๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ณด๋ด๋ ์๊ณ ๋ฆฌ์ฆ. ๋ด๋ฐ์ ๋์ ๋ฐฉ์๊ณผ ๋งค์ฐ ์ ์ฌํ๋ค.
๊ฐ๊ฐ์ ์ ๋ ฅ ๊ฐ์ ๊ฐ์ค์น์ ๊ณฑํด์ y์ ์ ๋ฌ ๋๋ค. ์ด๋, ์ ๋ฌ๋ ๊ฐ์ด ์๊ณ์น๋ฅผ ๋ ธ๋๊ฐ ํ์ฑํ๋๊ณ , ์๋๋ผ๋ฉด ํ์ฑํ ๋์ง ์๋๋ค. ์ด๋ ๊ฒ ํ์ฑํ๋ฅผ ๊ฒฐ์ ํ๋ ํจ์๋ฅผ ํ์ฑํ ํจ์(active function)๋ผ๊ณ ํ๊ณ , ๊ทธ ์ข ๋ฅ์๋ ๊ณ๋จํจ์, ์๊ทธ๋ชจ์ด๋, ReLU ๋ฑ์ด ์๋ค.
1. ๋จ์ธต ํผ์ ํธ๋ก
๋จ์ธต ํผ์ ํธ๋ก ์ ๊ฐ์ ๋ณด๋ด๋ ๋จ๊ณ๊ณผ ๊ฐ์ ๋ฐ์์ ์ถ๋ ฅํ๋ ๋ ๋จ๊ณ๋ก๋ง ์ด๋ฃจ์ด์ก๋ค. ์ฆ ์ ๋ ฅ์ธต๊ณผ ๊ฒฐ๊ณผ์ธต์ผ๋ก๋ง ์ด๋ค์ง๋ค. ์ธต์ด ํ๋๋ผ ์ ํ ์์ญ์ ๋ํด์๋ง ๋ถ๋ฅ๊ฐ ๊ฐ๋ฅํ๋ค. ๊ทธ๋์ ๊ฐ๋ฅํ ์ฐ์ฐ์ AND์ฐ์ฐ, OR์ฐ์ฐ, NAND์ฐ์ฐ์ด ์๋ค. XOR ์ฐ์ฐ์ ๋ถ๊ฐ๋ฅ ํ๋ฐ, ์๋์ ๊ทธ๋ํ๋ฅผ ๋ณด๋ฉด ๋ ์ฝ๊ฒ ์ดํด๊ฐ ๊ฐ๋ฅํ๋ค.
AND GATE OR, NAND GATE and, or, nand gate๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด, ๋ชจ๋ ์ง์ ํ๋๋ก ๊ฒฝ์ฐ๋ค์ ๋ถ๋ฅํ ์ ์๋ค.
XOR GATE ๊ทธ๋ฌ๋, XOR gate๋ ์ง์ ํ๋๋ก๋ ๋ถ๋ฅ๋ฅผ ํ ์๊ฐ ์๋ค. ๊ทธ๋ฌ๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น? ์ง์ ์ ๊ณก์ ์ผ๋ก ๋ง๋ค๋ฉด ๋ ๊น?
๊ณก์ ์ผ๋ก ํ๋, ๋ถ๋ฅ๊ฐ ๋๋ค!
2. ๋ค์ธต ํผ์ ํธ๋ก
๋ค์ธต ํผ์ ํธ๋ก ์ ์ ๋ ฅ์ธต, ์ถ๋ ฅ์ธต ์ธ์ '์๋์ธต(hidden layer)'์ด๋ผ๋ ๋ ๋ค๋ฅธ ์ธต์ด ์๋ ํผ์ ํธ๋ก ์ ์๋ฏธํ๋ค. ๋ค์ธต ํผ์ ํธ๋ก ์์๋ XOR์ฐ์ฐ์ด ๊ฐ๋ฅํ๋ค. Multylayer perceptron์ด๋ผ๊ณ ๋ ํ๋ฉฐ, ์ค์ฌ์ MLP ๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค.
์ ๋ ๊ฒ ์๋์ธต์ด 2๊ฐ ์ด์์ธ ์ ๊ฒฝ๋ง์ ์ฌ์ธต ์ ๊ฒฝ๋ง(Deep Neural Network, DNN)๋ผ๊ณ ํ๋ค.
๊ทธ๋ฐ๋ฐ ๋ง์ ๋๋ค, ๋จ์ผ ํผ์ ํธ๋ก ์ ๊ฒฝ์ฐ ์ค์ฐจ๋ฅผ ๋ฐ๋ก๋ฐ๋ก ๊ฐ์ ์ํฌ ์ ์๋ค. (h(x) = xw + b ์์ w๊ฐ์ ๊ฒฝ์ฐ, ๋ฐ๋ก๋ฐ๋ก ๊ฒฝ์ฌํ๊ฐ๋ฒ์ ์ ์ฉํ๋ค) ๊ทธ๋ฐ๋ฐ ๋ค์ธต ํผ์ ํธ๋ก ์ ๊ฒฝ์ฐ, ๊ฐ๊ฐ์ ๊ฐ์ค์น์ ์ค์ฐจ๋ฅผ ์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?์ด์ , ์ธ๊ณต ์ ๊ฒฝ๋ง์ด ์์ ํ ๊ณผ์ ์ ์งํํ์ฌ ์์ธก๊ฐ๊ณผ ์ค์ ๊ฐ์ ์ค์ฐจ๋ฅผ ๊ณ์ฐํ์์ ๋ ์ด๋ป๊ฒ ์ญ์ ํ ๊ณผ์ ์์ ๊ฒฝ์ฌ ํ๊ฐ๋ฒ์ ์ฌ์ฉํ์ฌ ๊ฐ์ค์น๋ฅผ ์ ๋ฐ์ดํธํ๋์ง ์์๋ณด์. (์ธ๊ณต ์ ๊ฒฝ๋ง์ ํ์ต์ ์ค์ฐจ๋ฅผ ์ต์ํํ๋ ๊ฐ์ค์น๋ฅผ ์ฐพ๋ ๋ชฉ์ ์ผ๋ก ์์ ํ์ ์ญ์ ํ๋ฅผ ๋ฐ๋ณตํ๋ ๊ฒ์ ๋งํ๋ค.)
3. ์์ ํ (Forward propagation)
์ ๋ ฅ์ธต -> ์๋์ธต -> ์ถ๋ ฅ์ธต ๋ฐฉํฅ์ผ๋ก ํฅํ๋ฉด์, ๊ฐ์ ๋ด์ด๋๋ ํ์. ์ด ๊ณผ์ ์ผ๋ก ์์๋ธ ์์ธก๊ฐ์ผ๋ก ์ค์ ๊ฐ๊ณผ์ ์ค์ฐจ๋ฅผ ๊ณ์ฐํ๋ค.
4. ์ญ์ ํ (Backpropagation)
์ถ๋ ฅ์ธต -> ์๋์ธต -> ์ ๋ ฅ์ธต ๋ฐฉํฅ์ผ๋ก ํฅํ๋ฉด์ ๊ฐ์ค์น๋ฅผ ์ ๋ฐ์ดํธํ๋ค. ์์ ํ๋ฅผ ํตํด ์ป์ ์ค์ฐจ๋ฅผ ์ด์ฉํด์ ๊ฐ์ค์น๋ฅผ ์ ๋ฐ์ดํธ ํ๊ณ ์ค์ฐจ๋ฅผ ์ค์ฌ๋๊ฐ๋ค. ๊ณ์ฐ์๋ ๋ฏธ๋ถ์ ์ฐ์๋ฒ์น์ ์ด์ฉํ๋ค.
์ฆ, ๊ฐ๊ฐ์ ๊ฐ์ค์น๋ค์ด ๊ฒฐ๊ณผ๊ฐ์ ์ํฅ์ ๋ฏธ์น๋ ๋น์จ(๋ฏธ๋ถ๊ฐ)์ ๊ตฌํ ๋ค, ์ค์ฐจ๋ฅผ ์ค์ฌ๋๊ฐ ์ ์๋ ๋ฐฉํฅ์ผ๋ก ๋นผ ์ฃผ๋ ๊ฒ์ด๋ค.
๊ฒฐ๊ตญ ๊ฐ์ค์น์ ๋ํ ๋น์ฉํจ์์ ๋ณํ์จ = ๊ฐ์ค์น์ ๋ํ ๊ฐ์ค์ ๋ณํ์จ x ๊ฐ์ค์ ๋ํ ํ์ฑํ ํจ์์ ๋ณํ์จ x ํ์ฑํ ํจ์์ ๋ํ ๋น์ฉํจ์์ ๋ณํ์จ์ด๋ค. ์ฌ๊ธฐ์ ๋ฏธ๋ถ์ ์ฐ์๋ฒ์น์ด ์ด์ฉ๋๋ค. (A์ ๋ํ B ์ ๋ณํ์จ => B๋ถ์ A)
5. XOR code (MLP)
import torchdevice = 'cuda' if torch.cuda.is_available() else 'cpu'# for reproducibilitytorch.manual_seed(777)if device == 'cuda':torch.cuda.manual_seed_all(777)X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)#๋ ์ด์ด ์ ์ธ (w์ b๋ฅผ ์ง์ ์ค์ )# nn.Linear๋ฅผ 2๊ฐ ์ฌ์ฉํ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์งw1 = torch.Tensor(2, 2).to(device) # 2->2b1 = torch.Tensor(2).to(device)w2 = torch.Tensor(2, 1).to(device) #2->1b2 = torch.Tensor(1).to(device)################################## (์ถ๊ฐ๋ ๋ถ๋ถ)torch.nn.init.normal_(w1) #์ ๊ท๋ถํฌ์์ ๊ฐ์ ธ์จ ๊ฐ์ผ๋ก ํ ์๋ฅผ ์ฑ์ด๋ค.torch.nn.init.normal_(b1)torch.nn.init.normal_(w2)torch.nn.init.normal_(b2)###################################def sigmoid(x):return 1.0/(1.0 + torch.exp(-x))def sigmoid_prime(x): #์๊ทธ๋ชจ์ด๋์ ๋ฏธ๋ถ.return sigmoid(x)*(1-sigmoid(x))learning_rate = 1for step in range(10001):#forwardl1 = torch.add(torch.matmul(X, w1), b1) #linear๋๋ก ๊ณ์ฐ(Wx + b) (4*2)a1 = sigmoid(l1) #ํ์ฑํl2 = torch.add(torch.matmul(a1, w2), b2) #(4*1)y_pred = sigmoid(l2) #ํ์ฑํ#BCE Loss์ฌ์ฉcost = -torch.mean(Y * torch.log(y_pred) + (1 - Y) * torch.log(1 - y_pred))#back prop (chain rule)d_y_pred = (y_pred - Y)/(y_pred * (1.0 - y_pred) + 1e-7) #bce๋ฅผ ๋ฏธ๋ถํ ์#์ถ๋ ฅ์ธต -> ์๋์ธตd_l2 = d_y_pred * sigmoid_prime(l2) #(4*1)d_b2 = d_l2d_w2 = torch.matmul(torch.transpose(a1, 0, 1), d_b2) #(2*1)#a๊ฐ 4*2๋ผ์ 2*4๋ก ๋ฐ๊ฟ์ ํ๋ ฌ๊ณฑ(2*4 x 4*1)์ ํ ์ ์๊ฒ ๋ฐ๊ฟ์ค.#์๋์ธต -> ์ ๋ ฅ์ธตd_a1 = torch.matmul(d_b2, torch.transpose(w2, 0, 1)) #(4*1) x (1*2) = (4*2)d_l1 = d_a1 * sigmoid_prime(l1) # 4*2d_b1 = d_l1 # 4*2d_w1 = torch.matmul(torch.transpose(X, 0, 1), d_b1) #(2*4) * (4*2) = (2*2)# weight updatew1 = w1 - learning_rate * d_w1 #๋ฏธ๋ถ๊ฐ(๊ธฐ์ธ๊ธฐ)๋นผ์ฃผ๊ธฐ(๋ฐฉํฅ์ด ์ค์!)b1 = b1 - learning_rate * torch.mean(d_b1, 0)#ํธ์ฐจ๋ ์ค์นผ๋ผ์ธ๋ฐ ๋ฏธ๋ถํ ํธ์ฐจ๊ฐ์ด ๋ฒกํฐ๋ผ์ ๋ง์ถฐ์ค์ผํจ??w2 = w2 - learning_rate * d_w2b2 = b2 - learning_rate * torch.mean(d_b2, 0)if step%100==0:print(step, cost.item())l1 = torch.add(torch.matmul(X, w1), b1) #linear๋๋ก ๊ณ์ฐ(Wx + b) (4*2)a1 = sigmoid(l1) #ํ์ฑํl2 = torch.add(torch.matmul(a1, w2), b2) #(4*1)y_pred = sigmoid(l2) #ํ์ฑํpredicted = (y_pred > 0.5).float()accuracy = (predicted == Y).float().mean()print('Hypothesis:', y_pred.detach().cpu().numpy(), '\nCorrect: ', Y.detach().cpu().numpy(), '\nAccuracy: ', accuracy.item())5-1. XOR code (MLP) with nn.Module
import torchdevice = 'cuda' if torch.cuda.is_available() else 'cpu'# for reproducibilitytorch.manual_seed(777)if device == 'cuda':torch.cuda.manual_seed_all(777)X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)# nn layerslinear1 = torch.nn.Linear(2, 10, bias=True)linear2 = torch.nn.Linear(10, 10, bias=True)linear3 = torch.nn.Linear(10, 10, bias=True)linear4 = torch.nn.Linear(10, 1, bias=True)sigmoid = torch.nn.Sigmoid()# modelmodel = torch.nn.Sequential(linear1, sigmoid, linear2, sigmoid, linear3, sigmoid, linear4, sigmoid).to(device)# define cost/loss & optimizercriterion = torch.nn.BCELoss().to(device)optimizer = torch.optim.SGD(model.parameters(), lr=3) # modified learning rate from 0.1 to 1for step in range(10001):optimizer.zero_grad()hypothesis = model(X)# cost/loss functioncost = criterion(hypothesis, Y)cost.backward()optimizer.step()if step % 100 == 0:print(step, cost.item())# Accuracy computation# True if hypothesis>0.5 else Falsewith torch.no_grad():hypothesis = model(X)predicted = (hypothesis > 0.5).float()accuracy = (predicted == Y).float().mean()print('\nHypothesis: ', hypothesis.detach().cpu().numpy(), '\nCorrect: ', predicted.detach().cpu().numpy(), '\nAccuracy: ', accuracy.item())<๋์ถฉ ์ ๋ฆฌํ๋ ๋จธ์ ๋ฌ๋ ํ์ต ๊ณผ์ >
- ํ์ต์ ์ ํฉํ ์์ ๊ฐ์ง ๋ ์ด์ด ์์ฑ( n๊ฐ)
- model์ ์ (์ด๋ค์์๋ก ๋๋ฆฌ๋์ง? ๋ ์ด์ด ์์? + ํ์ฑํํจ์)
- ๋น์ฉํจ์์ ์ต์ ํ ํจ์ ์ ์
- ๋๋ฆฐ๋ค (ํฌ์๋ + ๋ฐฑ์๋)
<Reference>
https://deeplearningzerotoall.github.io/season2/lec_pytorch.html
https://ko.wikipedia.org/wiki/%ED%8D%BC%EC%85%89%ED%8A%B8%EB%A1%A0
ํด๋์๊ธฐ๋ก์ ๋จ๊ธฐ๋ ค๊ณ ๋ ธ๋ ฅํฉ๋๋ค
'๐STUDY > ๐ฅPytorch ML&DL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
09-2. Weight initialization (0) 2020.03.10 09-1. ํ์ฑํ ํจ์(Activation function) (0) 2020.03.07 07. Tips and MNIST data (0) 2020.03.01 06. softmax classification (0) 2020.02.28 05. Logistic Regression (0) 2020.02.28