[딥러닝]FeedForWard Networks

chaeyoung·2023년 4월 21일
0

DeepLearning

목록 보기
1/4

Neural newtwork(NN, 신경망)

사람의 신경망을 구성하는 신경세포 뉴런은 각각의 입력 신호에 최적화된 가중치를 곱한 모든 합이 어느 임계값(Threshold)에 도달해야만 다음 뉴런으로 출력신호를 주는 구조이다. (여기서 함수가 사용되는데 활성화 함수라고 한다)

Deep Feedforward Networks

  • Feedforward는 only forward direction(순방향)으로 정보가 움직인다.
  • model은 여러 계층으로 구성되어있다.
  • 딥러닝 구조에서 hidden layers(은닉층)이 많이 사용될 수록 정확도가 높아진다.

Perceptron(퍼셉트론)

퍼셉트론은 인공 신경망(Artifical Neural Network, ANN)의 구성요소로 다수의 입력 값을 받아 하나의 값으로 출력하는 알고리즘

  • 단일 퍼셉트론 계산법

  • 다중 퍼셉트론 계산법

ForwardPass

입력층으로부터 출력층으로 데이터를 전달하면서 오차 값을 계산하는 과정

  • Neuron
    • 하나의 뉴런 model엔 파라미터 함수가 존재(parameter의 w,b 값을 구하는게 목적)
    • k개의 뉴런: k개의 hidden units들이 존재할 뿐 결과적으론 param을 구하는 것
      • Multi-layer Neural Network
        • 하나의 layer일 때 계산법을 여러번 사용하는 것
        • r개의 layer일 때, parameters의 개수와 같다(W도 r개,b도 r개)
        • hidden units(뉴런의 개수)는 r-1개 존재한다.

파이썬 이용 FeedForward

  • 표기법:

  • 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

0개의 댓글