9주차 언어지능 딥러닝 (4)

정지원·2024년 4월 21일
0

에이블 스쿨 복습

목록 보기
39/51
post-thumbnail

Nonlinear Function

ANN (Artificial Neural Networks)

  • 인공신경망은 입력층(Input layer), 은닉층(hidden layer), 출력층(output layer)로 구성되어 있다.

  • AND 게이트(논리곱 게이트)와 OR 게이트(논리합 게이트)는 두 개의 이진 입력을 받아서 하나의 이진 출력을 생성하는 간단한 논리 회로

    • AND gate: 두 입력이 모두 참(1)일 때만 출력이 참(1)이 되는 논리 회로
    • OR gate: 두 입력 중 하나 이상이 참(1)일 때 출력이 참(1)이 되는 논리 회로

Q) 위 그림의 신경망에서 제일 뒤에 코스트 함수가 있고, 경사하강법으로 한 번의 점프를 뛸 때에 몇 개의 신경망 내의 파라미터가 업데이트 되는가?


A) 41개이다.

  • 히든레이어1에 W는 3 by 4로 12개, b=4 로 4개,
  • 히든레이어2에 W는 4 by 4로 16개, b=4 로 4개,
  • output layer는 W= 4 by 1로 4개, b=1 로 1개
  • 총 41개가 된다.

code

import torch
x_train = torch.FloatTensor([[0,0], [0,1], [1,0], [1,1]])   # XOR 규칙
y_train = torch.FloatTensor([[0], [1], [1], [0]])           # XOR 규칙
w_h = torch.randn([2,3], requires_grad=True)
b_h = torch.randn([3], requires_grad=True)
w_o = torch.randn([3,1], requires_grad=True)
b_o = torch.randn([1], requires_grad=True)

optimizer = torch.optim.SGD([w_h, w_o, b_h, b_o], lr=0.01)

# [딥러닝의 1단계]: 모델을 만든다.
def H(x) :
  HL1 = torch.sigmoid(torch.matmul(x, w_h)+b_h)
  return torch.sigmoid(torch.matmul(HL1, w_o) + b_o)  # H(x) = sigmoid(Wx + b)

# [딥러닝의 2단계] 학습을 한다
for stmp in range(50000):
  cost = -torch.mean(y_train * torch.log(H(x_train))+(1-y_train) * torch.log(1-H(x_train)))
  optimizer.zero_grad()
  cost.backward()
  optimizer.step()

# [딥러닝의 3단계] 추론/테스트를 한다.
print(H(x_train))
>
>
tensor([[0.0261],
        [0.9785],
        [0.9406],
        [0.0725]], grad_fn=<SigmoidBackward0>)

MNIST Image Classification

from keras.utils import to_categorical
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense

# MNIST data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape(train_images.shape[0], 784).astype('float32')/255.0
test_images = test_images.reshape(test_images.shape[0], 784).astype('float32')/255.0

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Model
model = Sequential()

model.add( Dense(256, activation='relu') )
model.add( Dense(256, activation='relu') )
model.add( Dense(256, activation='relu') )
model.add( Dense(10, activation='softmax') )

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


# [딥러닝의 2단계] 학습
model.fit(train_images, train_labels, epochs=5, batch_size = 128, verbose=1)


# [딥러닝의 3단계] 추론/테스팅
_, accuracy = model.evaluate(test_images, test_labels)
print("Accuracy: %6.2f%%" %(100*accuracy))

model.summary()
profile
뒤늦게 프로그래밍을 시작한 응애

0개의 댓글