순차적 API :: 대화형 AI - mindscale
Skip to content

순차적 API

import tensorflow as tf

순차적 API

model = tf.keras.Sequential([
    tf.keras.layers.Dense(2, input_shape=(2,), activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 2)                 6         
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 3         
=================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
_________________________________________________________________
x = tf.convert_to_tensor([[1.0, 2.0]])
model(x)
<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.5]], dtype=float32)>

모형의 입력 형식을 설정하지 않는 경우

model = tf.keras.Sequential([
    tf.keras.layers.Dense(2, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

모형에 데이터를 입력하면, 입력한 데이터에 맞게 입력 형식이 정해진다.

model(x)
<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.18382496]], dtype=float32)>
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              (1, 2)                    6         
_________________________________________________________________
dense_5 (Dense)              (1, 1)                    3         
=================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
_________________________________________________________________

model.add로 레이어 추가하기

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(2, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model(x)
<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.42741257]], dtype=float32)>
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_6 (Dense)              (1, 2)                    6         
_________________________________________________________________
dense_7 (Dense)              (1, 1)                    3         
=================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
_________________________________________________________________