2. 함수형 API

간단한 예제

2개의 Dense 층을 가진 간단한 함수형 모델

from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(3,), name="my_input")
features = layers.Dense(64, activation="relu")(inputs)
outputs = layers.Dense(10, activation="softmax")(features)
model = keras.Model(inputs=inputs, outputs=outputs)

※ 심볼릭 텐서(symbolic tensor) : 데이터 텐서와 달리 실제 데이터를 가지고 있지 않지만 사용할 때 보게 될 데이터 텐서의 사양이 인코딩되어 있다. inputs, features, outputs 객체는 심볼릭 텐서이다.

inputs.shape

TensorShape([None, 3])

★ 이 모델은 각 샘플의 크기가 (3,)인 배치를 처리한다. 배치당 샘플의 개수는 가변적(배치 크기가 None으로 지정됨)

inputs.dtype

tf.float32

features.shape

TensorShape([None, 64])