함수형 API :: 대화형 AI - mindscale
Skip to content

함수형 API

import tensorflow as tf

함수형 API

레이어 만들기

layer1 = tf.keras.layers.Dense(2, activation='relu')
layer2 = tf.keras.layers.Dense(1, activation='sigmoid')

입력 노드

input_node = tf.keras.Input(shape=(2,))

레이어들을 연결한다

out1 = layer1(input_node)
out2 = layer2(out1)

모형을 정의

model = tf.keras.Model(inputs=input_node, outputs=out2)

모형 요약

model.summary()
Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 2)]               0         
_________________________________________________________________
dense (Dense)                (None, 2)                 6         
_________________________________________________________________
dense_1 (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.40557355]], dtype=float32)>

복잡한 모형 만들기

레이어 정의

layer1 = tf.keras.layers.Dense(2, activation='relu')
layer2 = tf.keras.layers.Dense(2, activation='tanh')
layer3 = tf.keras.layers.Add()
layer4 = tf.keras.layers.Dense(1, activation='sigmoid')
layer5 = tf.keras.layers.Dense(1, activation='sigmoid')

입력 노드

input1 = tf.keras.Input(shape=(2, ))
input2 = tf.keras.Input(shape=(2, ))

레이어 연결

out1 = layer1(input1)
out2 = layer2(input2)
out3 = layer3([out1, out2])
out4 = layer4(out3)
out5 = layer5(out3)

모형 정의

model = tf.keras.Model(inputs=[input1, input2], outputs=[out4, out5])

모형 요약

model.summary()
Model: "functional_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_2 (InputLayer)            [(None, 2)]          0                                            
__________________________________________________________________________________________________
input_3 (InputLayer)            [(None, 2)]          0                                            
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 2)            6           input_2[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 2)            6           input_3[0][0]                    
__________________________________________________________________________________________________
add (Add)                       (None, 2)            0           dense_2[0][0]                    
                                                                 dense_3[0][0]                    
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 1)            3           add[0][0]                        
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 1)            3           add[0][0]                        
==================================================================================================
Total params: 18
Trainable params: 18
Non-trainable params: 0
__________________________________________________________________________________________________

모형 구조를 시각화

tf.keras.utils.plot_model(model)
model([x, x])
[<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.20597538]], dtype=float32)>,
 <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.13494876]], dtype=float32)>]