import torch
x_train = torch.FloatTensor([[73,80,75],
[93,88,93],
[89,91,80],
[96,98,100],
[73,65,70]])
# x1_train = torch.FloatTensor([[73],[93],[89],[96],[73]])
# x2_train = torch.FloatTensor([[80],[88],[91],[98],[65]])
# x3_train = torch.FloatTensor([[75],[92],[80],[100],[70]])
y_train = torch.FloatTensor([[152],[185],[180],[196],[142]])
W = torch.zeros((3,1), requires_grad=True)
# w1 = torch.zeros((1,1), requires_grad=True)
# w2 = torch.zeros((1,1), requires_grad=True)
# w3 = torch.zeros((1,1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
import torch.optim as optim
optimizer = optim.SGD([W, b], lr=1e-5)# 10의 -5승
for epoch in range(1000): #반복해준다.
optimizer.zero_grad() #값들을 참조해서 초기화해준다.
#예측값 얻어내기
# hypothesis = torch.mm(x1_train, w1) + torch.mm(x2_train, w2) + torch.mm(x3_train, w3) + b
hypothesis = x_train.matmul(W) + b
loss = torch.mean((hypothesis - y_train) ** 2)
loss.backward()
optimizer.step()
#step 은 w의 원래값을 가져오고 그라이던트 값을 가져오고 러닝레이트를 곱하고 빼는 과정을 해주는것
# if epoch % 100 == 0 :
# print('epoch:{}, w1:{:.4f}, w2:{:.4f}, w3:{:.4f}, b:{:.4f}, loss:{:.3f}'.format(
# epoch+1, w1.item(), w2.item(), w3.item(), b.item(), loss.item()
# ))
#배열로 가져올때 넘파이, 스칼라값 하나만 가져올때 item으로
if epoch % 100 == 0:
print('epoch:{}, w1:{:.4f}, w2:{:.4f}, w3:{:.4f}, b:{:.4f}, loss:{:.3f}'.format(
epoch + 1, W[0].item(), W[1].item(), W[2].item(), b.item(), loss.item()
))
좀더 쉽게
model = nn.Linear(3,1)
optimizer = optim.SGD(model.parameters(), lr=1e-5)
hypothesis = x_train.matmul(W) + b
if epoch % 100 == 0:
print(f'epoch:{epoch+1}', end=' ')
for param in model.parameters():
print(param, end=' ')
loss = loss_func(hypothesis, y_train)
if epoch % 100 == 0:
print(f'epoch:{epoch+1}, loss:{loss.item():.4f}', end=' ')
for param in model.parameters():
print(param, end=' ')
import torch
x_train = torch.FloatTensor([[73,80,75],
[93,88,93],
[89,91,80],
[96,98,100],
[73,65,70]])
y_train = torch.FloatTensor([[152],[185],[180],[196],[142]])
import torch.nn as nn
model = nn.Linear(3,1)
#y = w1w1 + w2x2 + w3x3 + b
import torch.optim as optim
optimizer = optim.SGD(model.parameters(), lr=1e-5)
loss_func = nn.MSELoss()
for epoch in range(1000): #반복해준다.
optimizer.zero_grad() #값들을 참조해서 초기화해준다.
#예측값 얻어내기
hypothesis = model(x_train)
#w sum 값이라고 한다.
loss = loss_func(hypothesis, y_train)
# loss = torch.mean((hypothesis - y_train) ** 2)
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print(f'epoch:{epoch+1}, loss:{loss.item():.4f}', end=' ')
for param in model.parameters():
print(param, end=' ')
logistic regression




sigmoid 함수


logisticEx1
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1/ (1+np.exp(-x))
x = np.arange(-10, 10, 0.1)
y = sigmoid(x)
plt.plot([0,0],[1,0], ':')
plt.plot([-10, 10], [0,0], ':')
plt.plot([-10, 10], [1,1], ':')
plt.plot(x, y)
plt.show()

y1 = sigmoid(0.5 * x)
y2 = sigmoid(x)
y3 = sigmoid(2 * x)
plt.plot(x, y1, label='0.5 * x')
plt.plot(x, y2, label='x')
plt.plot(x, y3, label='2 * x')
plt.grid(True)
plt.legend(loc='best')
plt.show()

낮아질수록 완만해 진다.
y1 = sigmoid(x - 1.5)
y2 = sigmoid(x)
y3 = sigmoid(x + 1.5)
plt.plot(x, y1, label='x - 1.5')
plt.plot(x, y2, label='x')
plt.plot(x, y3, label='x + 1.5')
plt.grid(True)
plt.legend(loc='best')
plt.show()


정답이 1일때 - (y *log h) +
정답이 0일때 ( (1-y).log(1-h) )
binary cross entropy
logisticEx2
import torch
import torch.optim as optim
#특성 2개
x_data = [[1,2],[2,3],[3,1],[4,2],[5,3],[6,2]]
y_data = [[0],[0],[0],[1],[1],[1]]
x_train = torch.FloatTensor(x_data)
y_train = torch.FloatTensor(y_data)
w = torch.zeros((2,1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
hypothesis = 1 / (1 + torch.exp(-(x_train.matmul(w)+b))) # 이건 시그모이드함수
print(hypothesis)
print()
hypothesis2 = torch.sigmoid(x_train.matmul(w) + b)
print(hypothesis2)
print()
#sigmoid 함수는 x값 자체를 넣는다 -는 아님
losses = -(y_train * torch.log(hypothesis2) + (1 - y_train) * torch.log(1 -hypothesis2))
print(losses)
print()
loss = losses.mean()
print(loss)
print()
import torch.nn.functional as F
loss2 = F.binary_cross_entropy(hypothesis2, y_train)
# 결국 구하고 평균내린게 지금 함수임
print(loss2)
print()
optimizer = optim.SGD([w, b], lr=1)
for epoch in range(1000):
hypothesis = torch.sigmoid(x_train.matmul(w) + b)
loss = F.binary_cross_entropy(hypothesis, y_train)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 100 ==0:
print(f'epoch:{epoch+1} loss:{loss.item():.4f}')
p_porb= torch.sigmoid(x_train.matmul(w) + b)
print(p_porb)
prediction = p_porb > 0.5
# 같은거다 둘다
prediction = p_porb > torch.FloatTensor([0.5])
print(prediction)
modelEx1
import torch
import torch.nn as nn
x = torch.FloatTensor(torch.randn(16,10))
class CustomerLinear(nn.Module):
def __init__(self, input_size, output_size):
super().__init__()
self.linear = nn.Linear(input_size, output_size)
def forward(self, x):
y = self.linear(x)
return y
CModel = CustomerLinear(10, 5)
y = CModel.forward(x)
print(y)
print()
y2 = CModel(x)
# 함수화 시키고 호출하면 __call__ 이 호출된다.
# 여기선 forward가 호출 된다. 왜냐 모듈을 상속받았으니까
print(y2)
epoch => 수학문제집을 한번 다 푼것
batch => 1문제 풀고 1문제 답보고, n문제 풀고 n문제 답보고, 그것에 대한 평균을 가지고 학습
why? 학습의 안정화를 위해서
배치를 사용해 나누는걸 도와주는 라이브러리
modelEx2
import torch
import torch.optim as optim
from day2.linearEx1 import hypothesis
x_train = torch.FloatTensor([[73,80,75],
[93,88,93],
[89,91,80],
[96,98,100],
[73,65,70]])
y_train = torch.FloatTensor([[152],[185],[180],[196],[142]])
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
dataset = TensorDataset(x_train, y_train)
# 쌍으로 만든다.
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
print(dataloader)
#얘는 이터레이터라 for 문장으로 가져올 수 있음
print()
for data in dataloader:
print(data, end='\n\n')
import torch.nn as nn
model = nn.Linear(3, 1)
optimizer = optim.SGD(model.parameters(), lr=1e-5)
import torch.nn.functional as F
for epoch in range(20):
for idx, data in enumerate(dataloader):
batch_x, batch_y = data
hypothesis = model(batch_x)
loss = F.mse_loss(hypothesis, batch_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'epoch:{epoch+1} / {idx + 1 }, loss:{loss.item():.4f}')
'AI' 카테고리의 다른 글
| Day72_Ai(8)_Am (0) | 2025.11.05 |
|---|---|
| Day70_Ai(6)_Pm (0) | 2025.11.03 |
| Day67_Ai(5)_Am (0) | 2025.10.31 |
| Day66_Ai(4)_Pm (0) | 2025.10.30 |
| Day66_Ai(4)_Pm / 딥러닝전 설치 (0) | 2025.10.30 |