728x90
어제 ADSP를 봤고, 오늘은 코칭스터디 인공지능 기초 다지기 5주차 복습으로
- logistic regression 리마인드 및 code 작성
을 진행했다. 오늘은 조금 쉬어가는 시간을 가져야 할 듯.
logistic regression
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class BinaryClassifier(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
return self.sigmoid(self.linear(x))
# 전체 코드
#imports
if __name__ == "__main__":
# Training Data
x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] # 6, 2
y_data = [[0],[0],[0],[1],[1],[1]]
x_train = torch.FloatTensor(x_data)
y_train = torch.FloatTensor(y_data)
model = BinaryClassifier()
n_epochs = 1000
optimizer = optim.SGD(model.parameters(), lr=1)
for epoch in range(n_epochs+1):
hypothesis = model(x_train)
#cost
cost = F.binary_cross_entropy(hypothesis, y_train)
optimizer.zero_grad()
cost.backward()
optimizer.step()
if epoch % 10 == 0:
prediction = hypothesis >= torch.FloatTensor([0.5])
correct_prediction = prediction.float() == y_train
accuracy = correct_prediction.sum().item() / len(correct_prediction)
print("Epoch {:4d}/{} Cost: {:.6f}, Accuracy: {:2.2f}%".format(
epoch, n_epochs, cost, accuracy * 100
))
728x90
'진행중' 카테고리의 다른 글
[TIL] 2023-03-03 (0) | 2023.03.03 |
---|---|
[TID] 2023-03-02 (0) | 2023.03.02 |
[TIL]2023-02-21 (0) | 2023.02.21 |
[TIL] 2023-02-20 (0) | 2023.02.20 |
[TIL] 2023-02-17 (0) | 2023.02.17 |