Sequential API๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation=relu,
kernel_regualrizer=tf.keras.regularizers.l2(0.04),
input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10, activation='softmax') ])
Functional API๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
input = keras.Input(shape=(28, 28, 1), name='img')
x = layers.Conv2D(16, 3, activation='relu')(input)
x = layers.Conv2D(32, 3, activation='relu')(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation='relu')(x)
x = layers.Conv2D(16, 3, activation='relu')(x)
output = layers.GlobalMaxPooling2D()(x)
encoder = keras.Model(input, output, name='encoder')
Model Subclassing์ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
class ResNet(tf.keras.Model):
def __init__(self):
super(ResNet, self).__init__()
self.block_1 = ResNetBlock()
self.block_2 = ResNetBlock()
self.global_pool = layers.GlobalAveragePooling2D()
self.classifier = Dense(num_classes)
def call(self, inputs):
x = self.block_1(input)
x = self.block_2(x)
x = self.global_pool(x)
return self.classifier(x)
Layer List๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
layer_list = [layer1, layer2, ..., layern]
new_model = tf.keras.Sequential(layer_list)
## ๋๋
new_model = tf.kears.Sequential(layers=layer_list)