728x90
SMALL
NN 모델 정의
퍼셉트론
import torch
torch.manual_seed(777)
x=torch.FloatTensor([0,0],[0,1],[1,0],[1,1]])
y=torch.FloatTensor([[0],[1],[1],[0]])
# NN모델 정의
#layer 정의
linear=torch.nn.Linear(2,1,bias=True)
sigmoid=torch.nn.Sigmoid()
model=torch.nn.Sequential(linear,sigmoid)
#모델 학습환경 설정
loss=torch.nn.BCEloss()
optimizer=torch.optim.SGD(model.parameters(),lr=1)
for stop in range(10000):
#그래디언트 초기화
optimizer.zero_grad()
#Forward계산
hypothesis=model(x)
#Error 계산
cost= loss(hypothesis,Y)
#backward 계산
cost.backward()
#가중치 계산 optimizer.step()
if stop%100==0:
print(stop,cost.item())
#w,b 평가
with torch.no_grad():
hypothsis=model(x)
predicted=(hyporhesis>0.5).float()
accuracy=(predicted==Y).float().mean()
print('\n Hypothesis: ', hypothesis.numpy(), '\n Correct: ', predicted.numpy(), '\n Accuracy: ', accuracy.item())
NN 모델 정의
NN모델
import torch
torch.manual_seed(777)
x=torch.FloatTensor([0,0],[0,1],[1,0],[1,1]])
y=torch.FloatTensor([[0],[1],[1],[0]])
# NN모델 정의
#layer 정의
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)
#모델 학습환경 설정
loss=torch.nn.BCEloss()
optimizer=torch.optim.SGD(model.parameters(),lr=1)
for stop in range(10000):
#그래디언트 초기화
optimizer.zero_grad()
#Forward계산
hypothesis=model(x)
#Error 계산
cost= loss(hypothesis,Y)
#backward 계산
cost.backward()
#가중치 계산 optimizer.step()
if stop%100==0:
print(stop,cost.item())
#w,b 평가
with torch.no_grad():
hypothsis=model(x)
predicted=(hyporhesis>0.5).float()
accuracy=(predicted==Y).float().mean()
print('\n Hypothesis: ', hypothesis.numpy(), '\n Correct: ', predicted.numpy(), '\n Accuracy: ', accuracy.item())
NN 모델 정의
- 더 깊고, 넓게 만들기
- 깊게 => 은닉층 늘리기
- 넓게 => feature 수 늘리기
import torch
torch.manual_seed(777)
x=torch.FloatTensor([0,0],[0,1],[1,0],[1,1]])
y=torch.FloatTensor([[0],[1],[1],[0]])
# NN모델 정의
#layer 정의
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)
#모델 학습환경 설정
loss=torch.nn.BCEloss()
optimizer=torch.optim.SGD(model.parameters(),lr=1)
for stop in range(10000):
#그래디언트 초기화
optimizer.zero_grad()
#Forward계산
hypothesis=model(x)
#Error 계산
cost= loss(hypothesis,Y)
#backward 계산
cost.backward()
#가중치 계산 optimizer.step()
if stop%100==0:
print(stop,cost.item())
#w,b 평가
with torch.no_grad():
hypothsis=model(x)
predicted=(hyporhesis>0.5).float()
accuracy=(predicted==Y).float().mean()
print('\n Hypothesis: ', hypothesis.numpy(), '\n Correct: ', predicted.numpy(), '\n Accuracy: ', accuracy.item())
728x90
LIST
'AI' 카테고리의 다른 글
[심층신경망2] 이론 (0) | 2022.10.15 |
---|---|
심층신경망-실습2 (0) | 2022.10.12 |
심층신경망(Deep Neural Network(DNN)) (0) | 2022.10.05 |
실습- MINST (0) | 2022.10.03 |
다중 퍼셉트론(이론) (0) | 2022.10.03 |