728x90
반응형
[IBM AI course #3] Deep Learning & Neural Networks with Keras
Introduction to Advanced Keras
실제 적용 분야
헬스케어: 질병 진단용 의료 이미지 분석
금융: 시장 예측
자율주행: 객체 및 차선 인식
Functional API
Keras Functional API는 Sequential API보다 더 유연하게 복잡한 신경망 모델을 구축하게 해주며, 단순한 레이어 스택 이상의 모델이 필요할 때 유용하다.
가능한 구조
다중 입력과 다중 출력
공유 레이어 (Shared Layers)
비선형 데이터 흐름
→ 맞춤형 솔루션이 필요한 복잡한 문제를 연구하고 해결하는 데 매우 중요
레이어들을 순차적으로 쌓는 것이 아니라, 레이어 간 연결을 명시적으로 정의하는 구조로 되어있다.
명확한 모델 구조로 재사용성이 높다
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
# 입력 정의
inputs = Input(shape=(784,))
# 레이어 연결
x = Dense(64, activation='relu')(inputs)
x = Dense(32, activation='relu')(x)
outputs = Dense(10, activation='softmax')(x)
# 모델 정의
model = Model(inputs=inputs, outputs=outputs)
Subclassing API
Sequential API, Functional API와 달리 가장 높은 유연성을 제공한다.
tf.keras.Model 클래스를 상속받아 직접 __init__및 call() 메서드를 구현해야 한다.
(정적으로 정의할 수 없는 모델 구조에 적합)
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = Dense(64, activation='relu')
self.dense2 = Dense(32, activation='relu')
self.out = Dense(10, activation='softmax')
def call(self, inputs):
x = self.dense1(inputs)
x = self.dense2(x)
return self.out(x)
# 모델 인스턴스 생성
model = MyModel()
Subclassing API는 언제 사용하는가?
동적 아키텍처가 필요한 모델 (예: 강화학습 등)
커스텀 학습 루프 필요 시
새로운 구조나 레이어를 실험할 때
tf.GradientTape
Subclassing API에서는 Keras의 fit() 메서드 대신 tf.GradientTape를 사용해 직접 학습 루프를 작성할 수 있다.
Custom Layer
표준 레이어로 해결 안되는 특수 기능이 필요할 때
새로운 연구 아이디어 구현
성능 최적화
고유한 동작 구현
코드 가독성과 유지보수 향상
Custom Layer 구조 (3가지 메서드 구현 필요)
- __init__: 레이어 속성 초기화
- build: 레이어 가중치 생성 (처음에 한번 호출)
- call: 순전파 정의
from tensorflow.keras.layers import Layer
import tensorflow as tf
class CustomDenseLayer(Layer):
def __init__(self, units=32):
super(CustomDenseLayer, self).__init__()
self.units = units
def build(self, input_shape):
self.w = self.add_weight(shape=(input_shape[-1], self.units),
initializer='random_normal',
trainable=True)
self.b = self.add_weight(shape=(self.units,),
initializer='zeros',
trainable=True)
def call(self, inputs):
return tf.nn.relu(tf.matmul(inputs, self.w) + self.b)
from tensorflow.keras.layers import Softmax
model = Sequential([
CustomDenseLayer(128),
CustomDenseLayer(10),
Softmax()
])
728x90
반응형
'🥇 certification logbook' 카테고리의 다른 글
[Coursera/IBM course #3] Unsupervised Learning in Keras (2) | 2025.05.31 |
---|---|
[Coursera/IBM course #3] Transformers in Keras (2) | 2025.05.28 |
[Coursera/IBM course #3] TensorFlow for Image Processing (1) | 2025.05.24 |
[Coursera/IBM course #3] TensorFlow 2.x (1) | 2025.05.21 |
[Coursera/IBM] Deep Learning & Neural Networks with Keras 코스 소개 (2) | 2025.05.18 |
[Coursera/IBM course #2] Using Pretrained Models & Fine-Tuning (0) | 2025.05.18 |
[Coursera/IBM course #2] Transformers & Autoencoders (1) | 2025.05.18 |
[Coursera/IBM course #2] CNN & RNN (1) | 2025.05.17 |