Multi-input 모델

우수민·2021년 6월 29일
0

잡다한 공부

목록 보기
8/13

Keras: Multiple Inputs and Mixed Data

  • 모델을 구성하면서 데이터가 이미지, 텍스트 정보, 메타 데이터(나날짜 및 숫자)가 존재할 수 있다. 이를 쉽게 표현을 하면 아래와 같다.

    • Numeric/continuous values, such as age, heart rate, blood pressure
    • Categorical values, including gender and ethnicity
    • Image data, such as any MRI, X-ray, etc.
  • 이를 위한 모델을 만들기 위해서는 각각의 모델을 만들어서 합치는 경우도 생각할 수 있지만, 이를 한번에 학습하는 방법이 존재하는데 이는 keras functional api를 활용할 수 있다.


  • 위의 이미지처럼 Numeric + Categorical Data / Image Data가 존재하는데 각각의 목적에 맞는 모델을 생성하여 최종적으로 결과를 출력해주는 형태이다.

  • 전체적인 과정은 아래와 같으며 일반적인 방법과 크게 차이가 없다.

    1. 데이터 로드
    2. 모델에 훈련 시킬 수 있게 전처리
    3. multi-input Keras network에 적용
  • Keras의 경우에는 다양한 복잡한 모델을 설계할 수 있다.

    1. Multi-input models
    2. Multi-output models
    3. Models that are both multiple input and multiple output
    4. Directed acyclic graphs
    5. Models with shared layers

  • 코드 예시
# Multiple Inputs and Mixed Data

# define two sets of inputs
inputA = Input(shape=(32,))
inputB = Input(shape=(128,))
# the first branch operates on the first input
x = Dense(8, activation="relu")(inputA)
x = Dense(4, activation="relu")(x)
x = Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = Dense(64, activation="relu")(inputB)
y = Dense(32, activation="relu")(y)
y = Dense(4, activation="relu")(y)
y = Model(inputs=inputB, outputs=y)
# combine the output of the two branches
combined = concatenate([x.output, y.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
z = Dense(2, activation="relu")(combined)
z = Dense(1, activation="linear")(z)
# our model will accept the inputs of the two branches and
# then output a single value
model = Model(inputs=[x.input, y.input], outputs=z)
  • 다른 경우의 모델을 도식화하면 아래처럼 표현할 수 있다.
  • 하나의 인풋에 여러개 값의 경우 아래와 같다.


profile
데이터 분석하고 있습니다

0개의 댓글