사람의 신경망을 구성하는 신경세포 뉴런은 각각의 입력 신호에 최적화된 가중치를 곱한 모든 합이 어느 임계값(Threshold)에 도달해야만 다음 뉴런으로 출력신호를 주는 구조이다. (여기서 함수가 사용되는데 활성화 함수라고 한다)
퍼셉트론은 인공 신경망(Artifical Neural Network, ANN)의 구성요소로 다수의 입력 값을 받아 하나의 값으로 출력하는 알고리즘
단일 퍼셉트론 계산법
다중 퍼셉트론 계산법
입력층으로부터 출력층으로 데이터를 전달하면서 오차 값을 계산하는 과정
parameter의 w,b 값을 구하는게 목적
) Multi-layer Neural Network
parameters
의 개수와 같다(W도 r개,b도 r개)표기법:
feedforward python으로 구현
import numpy as np
import torch
import time
# 임의의 data set
batch_size = 10
num_feature = 4
torch.manual_seed(0)
X_batch = torch.randn(batch_size, num_feature)
Y_batch = (torch.sum(X_batch, dim=1)>0).type(torch.float).reshape(-1,1)
print(X_batch.shape)
# ReLU함수
def ReLU(x):
return torch.clamp(x, min = 0)
# 문제1: Single Layer Network
# 1개의 layer, k = 5개의 뉴런이 있는 network 구성
W = torch.randn(num_feature,5)
b = torch.ones(1,5)
Z = torch.matmul(X_batch, W) + b
A = ReLU(Z)
# 문제2: Multi-Layer network
# 3개의 layer, 2번째 layer 입력 크기는 k[1] = 16, k[2] = 6, k[3] = 1로 뉴런 구성
W1 = torch.randn(num_feature, 16)
W2 = torch.randn(16,6)
W3 = torch.randn(6,1)
b1 = torch.ones(1,16)
b2 = torch.ones(1,6)
b3 = torch.ones(1,1)
Z1 = torch.matmul(X_batch, W1) + b1
A1 = ReLU(Z1)
Z2 = torch.matmul(A1, W2) + b2
A2 = ReLU(Z2)
Z3 = torch.matmul(A2, W3) + b3
A3 = ReLU(Z3) # - - - - - -> function으로 표현한다면
class my_linear_layer():
def __init__(self, n_input, n_output):
self.W = torch.randn(n_input, n_output)
self.b = torch.ones(1, n_output)
def forward(self, A):
return torch.matmul(A, self.W) + self.b
참고: https://hidemasa.tistory.com/19
https://heytech.tistory.com/332