C.W.K.
Stream
Lesson 05 of 07 · published

공유 layer·중첩 모델

~11 min · functional

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

같은 layer 인스턴스를 여러 곳에서 호출하면 weight 공유. shared = Dense(64); a = shared(input_a); b = shared(input_b) — a 와 b 가 같은 가중치 통과한 결과. siamese network 의 자연스러운 표현.

중첩 모델: 한 model 을 다른 model 안에서 *layer 처럼* 호출. backbone = keras.Model(...); x = backbone(input); output = head(x). backbone 의 weight 가 그대로 따라옴. transfer learning + custom head 의 표준 패턴.

Code

# Shared embedding for a Siamese network
shared_embedding = layers.Dense(64, activation="relu", name="shared_embed")

input_a = keras.Input(shape=(128,))
input_b = keras.Input(shape=(128,))

# Same weights used for both inputs
encoded_a = shared_embedding(input_a)
encoded_b = shared_embedding(input_b)

# Compute distance
distance = layers.Lambda(
    lambda x: keras.ops.abs(x[0] - x[1])
)([encoded_a, encoded_b])
output = layers.Dense(1, activation="sigmoid")(distance)

model = keras.Model(inputs=[input_a, input_b], outputs=output)
# Use an existing model as a layer
encoder = keras.Model(encoder_inputs, encoded, name="encoder")
decoder = keras.Model(decoder_inputs, decoded, name="decoder")

# Nest them
inputs = keras.Input(shape=(784,))
z = encoder(inputs)       # Model called like a layer
outputs = decoder(z)
autoencoder = keras.Model(inputs, outputs)

External links

Exercise

image similarity 용 siamese network 짜 — 공유 CNN encoder 를 두 입력에 적용, L2 거리 + sigmoid. Functional API 로. synthetic pair 로 학습.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.