02. NN Classification with TF
(참고) udemy - TF Developer in 2022
Contents
- Non-linear activation function
- Multi-class Classification
- Learning rate scheduler
1과 거의 동일.
차이점? loss function & output layer
tf.random.set_seed(42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(100),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
# Binary Classification
### loss="binary_crossentropy" 도 OK
model.compile(loss=tf.keras.losses.BinaryCrossentropy(),
optimizer=tf.keras.optimizers.SGD(),
metrics=['accuracy'])
# 3. Fit the model
model.fit(X, y, epochs=5)
(1) Non linear activation function
model = tf.keras.Sequential([
tf.keras.layers.Dense(4, activation="relu"),
tf.keras.layers.Dense(4, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
(2) Multi-class Classification
- input : 28x28 image
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(4, activation="relu"),
tf.keras.layers.Dense(4, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")
])
model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(),
optimizer=tf.keras.optimizers.Adam(),
metrics=["accuracy"])
(3) Learning rate scheduler
tf.keras.callbacks.LearningRateScheduler
fit
안의callbacks
argument에 넣는다
lr_scheduler = tf.keras.callbacks.LearningRateScheduler(lambda epoch: 1e-3 * 10**(epoch/20))
# Fit the model
find_lr_history = model_13.fit(train_data,
train_labels,
epochs=40,
validation_data=(test_data, test_labels),
callbacks=[lr_scheduler])