모두의 딥러닝/강의자료 정리

Lab 08-2: Multi Layer Perceptron

cvlab_김준수 2025. 2. 4. 10:07
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)

    
X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device)
Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device)

# 레이어 생성
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() # 활성화 함수

model = torch.nn.Sequential(linear1, sigmoid, linear2, sigmoid, linear3, sigmoid, linear4, sigmoid).to(device) # 모델 생성

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())