곰퓨타의 SW 이야기

Lab 08-1 Perceptron 본문

인공지능/부스트코스_파이토치로 시작하는 딥러닝 기초

Lab 08-1 Perceptron

곰퓨타 2021. 2. 22. 18:30

이 강의를 참고하였다!!⭐️

www.boostcourse.org/ai214/lecture/43757/

 

파이토치로 시작하는 딥러닝 기초

부스트코스 무료 강의

www.boostcourse.org

 

Perceptron

인공 신경망의 한 종류이다.

Neuron : 인간의 뇌에서 신호를 전달하는 역할을 수행한다. 이는 동작방법이 아주 단순하다.

입력 신호가 들어왔을 때, 활성화가 되어 특정 임계치를 넘어가면 다음 뉴런에게 전파가 된다.

이런 인공신경망의 예 중 하나가 Perceptron이다.

 

가중치들의합 + bias를 합하여 activation function(ex. sigmoid)를 통해 output을 만든다.

이 예는 linear classification의 예이다. 2개의 클래스가 있을 때 linear하게 분류해낸다.

 

 

 

AND, OR

 

이러한 and gate, or gate는 그래프로 나타내어 분류하면 다음과 같다.

 

XOR

multilayer를 학습시키기 위한 방법인 backpropogation이 개발되기 전, 이는 트레인닌ㅇ 할 수 없음이 증명되었었다..

linear한 방법으로는 XOR을 분류할 수 있는 그래프를 그릴 수 없다..!

 

Code : xor

# Lab 9 XOR
import torch

device = 'cuda' if torch.cuda.is_available() else 'cpu'

# for reproducibility
torch.manual_seed(777)
if device == 'cuda':
    torch.cuda.manual_seed_all(777)
    
    
# XOR X(x,y)에 대한 output Y
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() # activation function : sigmoid

# model
model = torch.nn.Sequential(linear, sigmoid).to(device)

# define cost/loss & optimizer
# binary cross entropy loss
criterion = torch.nn.BCELoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=1)

# 10000번 정도 진행
for step in range(10001):
    optimizer.zero_grad()
    hypothesis = model(X)

    # cost/loss function
    cost = criterion(hypothesis, Y)
    cost.backward()
    optimizer.step()
    
    # 100번째마다 loss값 찍음
    if step % 100 == 0:
        print(step, cost.item())
        

# Accuracy computation
# True if hypothesis>0.5 else False
# 모든 값을 0.5로 예측하게 되어 정확도가 0.5밖에 되지 않는다.
with 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())

'인공지능 > 부스트코스_파이토치로 시작하는 딥러닝 기초' 카테고리의 다른 글

Lab 09-1 ReLU  (0) 2021.02.25
Lab 08-2 Multi layer Perceptron  (0) 2021.02.22
Lab 07-2 MNIST Introduction  (0) 2021.02.22
Lab 07-1 Tips  (0) 2021.02.17
Lab-06 Softmax Classification  (0) 2021.02.17
Comments