Post

[Deep Learning] Keras Locally Connection(concatenate, add) 구현

Keras Functional API를 사용한 Locally Connection 구현하기.

✅ Locally Connection

  • 딥러닝 Connection에는 ConcatenateAdd Layer가 있다.
  • 특징들을 종류별로 따로 연결하여 학습한다.
  • Concatenate는 각각의 feature를 살려 합치는 형태
    • ex) (None, 2) 와 (None, 4)를 concatenate 하면 (None, 6)이 된다.
  • Add는 단순히 각각 element wise하게 더하기 때문에, Shape이 같아야 한다.
    • ex) (None, 2) 와 (None, 2)를 add 하면 (None, 2)이 된다.

✅ 코드 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
input_layer1 = keras.layers.Input(shape=(2, )) # ex) 특징을 구분하여 따로 넣어줌
input_layer2 = keras.layers.Input(shape=(2, )) # ex) 특징을 구분하여 따로 넣어줌

input_layer3 = keras.layers.Input(shape=(4, )) # ex) 전체 특징

hl_1 = keras.layers.Dense(2, activation='relu')(input_layer1)
hl_2 = keras.layers.Dense(2, activation='relu')(input_layer2)

add_layer = keras.layers.Add()([hl_1, hl_2])
concat_layer = keras.layers.Concatenate()([add_layer, input_layer3])

output_layer = keras.layers.Dense(1, activation='sigmoid')(concat_layer)

model = keras.models.Model([input_layer1, input_layer2, input_layer3], output_layer)

위 코드의 모델 구조도

image

This post is licensed under CC BY 4.0 by the author.

[Deep Learning] Keras Functional API로 모델 생성

[Deep Learning] 딥러닝 모델 구조 시각화