복습 : 케라스 창시자에게 배우는 딥러닝
DeepLearning101_07_케라스완전정복_워크플로우란?
Aloha oe AI
2024. 9. 3. 23:14
Keras는 모델 훈련을 위한 다양한 워크플로를 제공한다.
워크플로란 딥러닝 모델의 전체과정으로,
데이터 수집*과 *전처리, 모델의 설계, 훈련, 검증, 튜닝, 평가, 배포, 모니터링까지 과정의 단계들을 내포한다.
만드는 것도, 훈련하는 것도 다양한 Keras 워크플로
케라스는 이 모든 과정을 크게 세 가지 방향으로 제공한다.
사용자가...
1. 개별 단계(ex.훈련루프)와 단계에서 사용될 요소(ex.모델)까지 모두 직접 짤 수 있는 방향과
2. 개별 단계를 매서드로 호출하도록 API를 만들어 매서드 내 모델과 파라미터 등 결정하면 되는 방향
3. 위 두 가지를 혼용하는 방향
이렇게 워크플로를 유연하게 사용할 수 있는 이유는, 모든 워크플로는 keras.Model
keras.Layer
같은 Keras API를 기반하기 때문이다.
아래는 두 번째 방향에 해당하는 "표준 워크플로"를 소개하겠다. (pg.261)
(데이터는 단일레이블-다중분류모델인 MNIST 데이터셋을 사용함)
7-17 표준 워크플로
(설계 : compile()
, 훈련 : fit()
, 평가 : evaluate()
, 예측 : predict()
)
데이터셋 가져오기(import)
from tensorflow.keras.datasets import mnist
모델 정의하기
def get_mnist_model():
inputs = keras.Input(shape=(28*28,))
features = layers.Dense(512, activation="relu")(inputs)
features = layers.Dropout(0.5)(features)
outputs = layers.Dense(10, activation="softmax")(features)
model = keras.Model(inputs, outputs)
return model
데이터 전처리 : train과 test 데이터 분리
(images, labels),(test_images, test_labels) = mnist.load_data()
images = images.reshape((60000, 28*28)).astype("float32") / 255
test_images = test_images.reshape((10000, 28*28)).astype("float32") / 255
train_images, val_images = images[10000:], images[:10000]
train_labels, val_labels = labels[10000:], labels[:10000]
모델 훈련, 평가하기
model = get_mnist_model()
model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
# 훈련루프
model.fit(train_images,train_labels,
epochs=3,
validation_data=(val_images, val_labels))
#평가루프
test_metrics = model.evaluate(test_images,test_labels)
predictions = model.predict(test_images)