[KT AIVLE] 9. 딥러닝(3)

onlyJoon·2023년 3월 2일
0

KT AIVLE

목록 보기
3/31
post-thumbnail

요약

  • Functional API: 레이어들을 체인처럼 엮은 다음, 모델에게 (시작, 끝)을 전달하는 방식
  • Flatten: 데이터를 reshape하여 전달하는 대신 reshape 역할을 해주는 층을 만들 때 사용
  • Fashion MNIST: MNIST보다 복잡해진 데이터셋
  • CIFAR-10: MNIST들보다 더 복잡해진 데이터셋

Functional API

정의

  • 각 층을 일종의 함수(function)로서 정의하는 방식

특징

  • Sequential API만으로는 구현할 수 없는 복잡한 신경망을 구현 가능
  • 이전층을 다음층 함수의 입력으로 사용

코드 구현

  • 세션 클리어, 컴파일 등은 Sequential API 때와 같음
  • 모델 선언, 레이어 구성 과정이 다름
input_layer = keras.layers.Input(shape = (a, ))
hidden_layer1 = keras.layers.Dense(노드 수, activation = 활성화 함수)(input_layer)
hidden_layer2 = keras.layers.Dense(노드 수, activation = 활성화 함수)(hidden_layer1)
ouput_layer = keras.layers.Dense(노드 수, activation = 활성화 함수)(hidden_layer2)

model = keras.models.Model(input_layer, output_layer) # .Model(입력층, 출력층)

ANN Fashion MNIST

  • ANN: Artificial Neural Network
  • MNIST: Modified National Institute of Standards and Technology database

구성

  • 10가지로 분류되는 의류 이미지 모음 데이터셋
  • 28×28 픽셀의 이미지 70,000개로 이루어져 있음

특징

  • MNIST와 마찬가지로 전처리 과정 필요
    • x: Min-Max Scaling, reshape
    • y: One-Hot Encoding

reshape과 Flatten

  • reshape 사용 시: x_train = x_train.reshape([60000, -1])
  • 위 방법은 번거로울 수 있음 -> Flatten Layer를 추가하여 처리
model.add(keras.layers.Input(shape = (28, 28))) # (28, 28): 데이터 구조
model.add(keras.layers.Flatten())

불러오기

(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()

# Input Layer(Sequential API)
model.add(keras.layers.Input(shape = (28, 28)))
model.add(keras.layers.Flatten())

# Input Layer(Functional API)
input_layer = keras.layers.Input(shape = (28, 28))
flatten_layer = keras.layers.Flatten()(input_layer)
  • x_train.shape : (60000, 28, 28) / x_test.shape : (10000, 28, 28)
  • y_train.shape : (60000, ) / y_test.shape : (10000, )

ANN CIFAR-10

  • CIFAR: Canadian Institute For Advanced Research

구성

  • 10가지로 분류되는 컬러 이미지 모음 데이터셋
  • 32×32 픽셀의 이미지 60,000개로 이루어져 있음

특징

  • 컬러이미지로서 흑백이미지와 달리 차원이 하나 더 추가된 형태
  • 전처리 과정이 필요:
    • x: Min-Max Scaling, reshape
    • y: One-Hot Encoding

불러오기

(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()

# Input Layer(Sequential API)
model.add(keras.layers.Input(shape = (32, 32, 3)))
model.add(keras.layers.Flatten())

# Input Layer(Functional API)
input_layer = keras.layers.Input(shape = (32, 32, 3))
flatten_layer = keras.layers.Flatten()(input_layer)
  • x_train.shape : (50000, 32, 32, 3) / x_test.shape : (10000, 32, 32, 3)
  • y_train.shape : (50000, 1) / y_test.shape : (10000, 1)

마무리

  • 레이어의 Depth: 얼마나 High-level의 특징을 추출할 것인가?
  • 레이어 Node의 수: 얼마나 많은 특징을 추출할 것인가?
  • 새로운 활성화 함수 Swish (Swish function 자세한 내용)
    f(x)=xσ(x),    where σ(x)=11+exf(x) = x \cdot \sigma(x), \ \ \ \ where\ \sigma(x)= \frac{1}{1+e^{-x}}
profile
A smooth sea never made a skilled sailor

0개의 댓글