Perceptron
Perceptron
•
인공신경망의 한 종류
인공신경망
•
동물의 신경계, 그중에서 특히 뇌에 있는 뉴런이라는 동작방식을 본따 만든 코드
•
뉴런 → 신호를 전달하는 역할을 수행 → 이 모델은 본 따 만든 것 → 인공신경망
퍼셉트론 (Perceptron)
→ 1950년 대에 퍼셉트론이 개발 되었는데, AND, OR 문제를 해결하기 위해 만들어졌다
AND, OR
AND
A | B | |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
OR
A | B | |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
XOR
XOR
A | B | |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
→ Linear 한 방식으로는 Perceptron은 불가능 → 암흑기로
Code : xor
X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)
Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)
## nn Layers
linear = torch.nn.Linear(2, 1, bias=True)
sigmoid = torch.nn.Sigmoid()
model = torch.nn.Sequential(linear, sigmoid).to(device)
## define cost/Loss & optimizer
criterion = torch.nn.BCELoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=1)
for step in range(10001):
optimizer.zero_grad()
hypothesis = model(X)
# cost/Loss function
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
if step % 100 == 0:
print(step, cost.item())
Python
복사
Multi Layer Perceptron
XOR을 처리하기 위해서는 Multi Layer Perceptron(MLP)가 필요하다
Backpropagation
→ 입력 X가 들어왔을 때, 뉴럴 네트워크를 통해서, Y를 구하게 되는데
→ Output과 원래 정답인 G간의 차이를 cost/loss 라 부르는데
→ loss에 대해서 뉴럴 네트워크에 있는 weight들에 대한 미분값을 계산하게 되고
→ 이 gradient를 가지고 뒤쪽의 weigth들 부터 Loss값을 최소화 시킬 수 있도록, weight를 업데이트하는 방식
→ Backpropagation 알고리즘이라 한다
Backpropagation code
X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)
Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)
## nn Layers
w1 = torch.Tensor(2, 2).to(device)
b1 = torch.Tensor(2).to(device)
w2 = torch.Tensor(2, 1).to(device)
b2 = torch.Tensor(1).to(device)
def sigmoid(x):
# sigmoid function
return 1.0 / (1.0 + torch.exp(-x))
# return torch.div(torch.tensor(1), torch.add(torch.tensor(1.0), torch.exp(-x)))
def sigmoid_prime(x):
# derivative of the sigmoid function
return sigmoid(x) * (1 - sigmoid(x))
for step in range(10001):
# forward
l1 = torch.add(torch.matmul(X, w1), b1)
a1 = sigmoid(l1)
l2 = torch.add(torch.matmul(a1, w2), b2)
a2 = sigmoid(l2)
# binary cross entropy loss
cost = -torch.mean(Y * torch.log(Y_pred) + (1 - Y) * torch.log(1 - Y_pred))
# Back prop (chain rule)
Python
복사
Code : xor-nn
X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)
Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)
## nn Layers, MLP
linear1 = torch.nn.Linear(2, 2, bias=True)
linear2 = torch.nn.Linear(2, 1, bias=True)
sigmoid = torch.nn.Sigmoid()
model = torch.nn.Sequential(linear1, sigmoid, linear2, sigmoid).to(device)
## define cost/Loss & optimizer
criterion = torch.nn.BCELoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=1)
for step in range(10001):
optimizer.zero_grad()
hypothesis = model(X)
# cost/Loss function
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
if step % 100 == 0:
print(step, cost.item())
Python
복사
Code : xor-nn-wide-deep
X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)
Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)
## nn Layers, MLP
linear1 = 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()
## define cost/Loss & optimizer
criterion = torch.nn.BCELoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=1)
for step in range(10001):
optimizer.zero_grad()
hypothesis = model(X)
# cost/Loss function
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
if step % 100 == 0:
print(step, cost.item())
Python
복사