본문 바로가기
Data Science/Basic

Sequential Model, Functional Model

by hyelog 2022. 4. 30.

Sequential Model, Functional Model

✅ 딥러닝 모델 구조 보고 여러 방식으로 만들어 보기

basic model

import tensorflow as tf

Sequential Model

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(8, activation='relu', input_shape=(4,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax')) # 10가지 정답을 가진 분류 모델

  • dense_4 Param = 4 * 8 + 8 = 40
  • dense_5 Param = 8 * 16 + 16 = 144
  • dense_4 Param = 16 * 32 + 32 = 544
  • dense_4 Param = 32 * 10 + 10 = 330

Functional Model

from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

input_ = Input(shape=(4,))

x = Dense(8, activation='relu')(input_)
x = Dense(16, activation='relu')(x)
x = Dense(32, activation='relu')(x)
output_ = Dense(10, activation='softmax')(x)

model = Model(inputs=input_,  outputs=output_)

  • 파라미터 계산 값은 위의 Sequential Model 과 같다.

Model Concatenate using Functional

concat model

input_1 = Input(shape=(4,))
x1 = Dense(8, activation='relu')(input_1)
x1 = Dense(16, activation='relu')(x1)
output_1 = Model(inputs=input_1,  outputs=x1)

input_2 = Input(shape=(8,))
x2 = Dense(8, activation='relu')(input_2)
output_2 = Model(inputs=input_2,  outputs=x2)

x = tf.keras.layers.concatenate([output_1.output,output_2.output])

output_ = Dense(10, activation='softmax')(x)

model = Model(inputs=[output_1.input,output_2.input], outputs=output_)

  • dense_16 = 4 * 8 + 8 = 40
  • dense_17 = 8 * 16 + 16 =144
  • dense_18 = 8 * 8 + 8 = 72
  • dense_19 = 24 * 10 + 10 =250

'Data Science > Basic' 카테고리의 다른 글

온톨로지는 뭘까.  (1) 2025.02.02
지표를 설계해보자  (1) 2024.11.10
지표, 넌 누구니?  (1) 2024.10.27
Pytorch Tutorial (1) _ Tensor  (0) 2022.06.05

댓글