본문 바로가기
인공지능/Deep Learning

[Deep Learning] Flatten Library

by coding_su 2022. 12. 28.

📝딥러닝 플래튼 라이브러리

이미지의 가로 세로를 전부 일렬로 만들어 인풋레이어 지정

# 함수로 만들때 인풋레이어에 사용해준다
def build_model() :
  model = Sequential()
  model.add( Flatten() )
  model.add( Dense( units= 128, activation= 'relu') )
  model.add( Dense( units= 64, activation= 'relu') )
  model.add( Dense( units=10, activation= 'softmax' ) )
  model.compile( optimizer= 'adam', loss= 'sparse_categorical_crossentropy', metrics= [ 'accuracy' ])
  return model
이미지의 가로 세로를 전부 일렬로 만드는 작업이 flatten이다
※ 이미지 인풋레이어 갯수 = 행 * 열 갯수

 

※ Flatten 라이브러리 없이 평탄화 하는 방법

# 아래 코드의 결과값은 (60000, 28, 28) 이다
X_train.shape

# 28 * 28 = 784이므로 열을 784로 변환해준다
X_train = X_train.reshape(60000, 784)

# X_test의 값도 변환해준다
X_test = X_test.reshape(10000, 784)

# 데이터 타입을 변경한 후 모델링할때 입력 input_shape=(784, )
def build_model() :
  model = Sequential()
  model.add( Dense( units= 128, activation= 'relu', input_shape=(784, ) ) )
  model.add( Dense( units= 64, activation= 'relu') )
  model.add( Dense( units=10, activation= 'softmax' ) )
  model.compile( optimizer= 'adam', loss= 'sparse_categorical_crossentropy', metrics= [ 'accuracy' ])
  return model

댓글