[인공지능] Keras 모델 생성 기본 구조

참조


Keras 모델 생성 기본 구조

  1. 데이터 셋 생성
    • 훈련을 위한 데이터
    • 검증을 위한 데이터
    • 테스트를 위한 데이터
  2. 모델 구성
    • 시퀀스 모델 생성한 다음 레이어를 추가(간단한 모델)
    • 복잡한 모델은 케라스 함수 API를 사용
  3. 모델 학습과정 설정
    • Cost 함수 정의, 최적화 방법 정의
    • Compile 함수 사용
  4. 모델 학습
    • 트레이닝 데이터로 모델 학습
    • Fit 함수 사용
  5. 훈련 셋, 검증 셋의 COST 측정, 정확도 측정
  6. 모델 평가
    • 테스트 데이터셋으로 평가
    • Evaluate 함수가 사용
  7. 모델 사용
    • 입력 -> 모델 -> 출력
    • Predict 함수가 사용

Keras 예제 코드

import tensorflow as tf

from keras.datasets import mnist 
from keras.models import Sequential 
from keras.layers import Dense, Activation
from keras.utils import np_utils 

#1 데이터셋 생성하기
(xTrain,yTrain),(xTest,yTest) = mnist.load_data()

#1-1. 데이터 전처리
xTrain = xTrain.reshape(60000,784).astype('float32')/255.0
xTest = xTest.reshape(10000,784).astype('float32')/255.0
yTrain = np_utils.to_categorical(yTrain) # 원핫인코딩 
yTest = np_utils.to_categorical(yTest) # 원핫인코딩 

#2 모델 구성
model = Sequential()
model.add(Dense(units = 64, input_dim = 28*28, activation = 'relu'))
model.add(Dense(units = 10, activation = 'softmax'))

#3 모델 학습과정 설정
model.compile(loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy'])

#4. 모델 학습
hist = model.fit(xTrain, yTrain, epochs = 10, batch_size = 32)

#5 검증셋으로 검증
print("loss:"+ str(hist.history['loss']))
print("accuracy:"+ str(hist.history['accuracy']))

#6 모델평가
res = model.evaluate(xTest, yTest, batch_size=32)
print(res)

#7 모델 예측
xhat = xTest[0:1]
yhat = model.predict(xhat)
print(yhat)

실행 결과

2022-02-10 15:12:28.955386: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2       
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-02-10 15:12:29.417417: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2782 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1
Epoch 1/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.6668 - accuracy: 0.8280
Epoch 2/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3507 - accuracy: 0.9013
Epoch 3/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3042 - accuracy: 0.9133
Epoch 4/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.2751 - accuracy: 0.9226
Epoch 5/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.2538 - accuracy: 0.9282
Epoch 6/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.2354 - accuracy: 0.9336
Epoch 7/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.2202 - accuracy: 0.9383
Epoch 8/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.2068 - accuracy: 0.9428
Epoch 9/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.1952 - accuracy: 0.9450
Epoch 10/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.1849 - accuracy: 0.9481
loss:[0.6667617559432983, 0.35065653920173645, 0.30417704582214355, 0.27511969208717346, 0.2537631094455719, 0.23544098436832428, 0.22016207873821259, 0.20680344104766846, 0.19522258639335632, 0.184888556599617]
accuracy:[0.8279500007629395, 0.901283323764801, 0.9133166670799255, 0.9225999712944031, 0.9281666874885559, 0.9335833191871643, 0.9382500052452087, 0.942799985408783, 0.9450333118438721, 0.948116660118103]
313/313 [==============================] - 1s 3ms/step - loss: 0.1797 - accuracy: 0.9477
[0.17967970669269562, 0.947700023651123]
[[3.1819582e-05 8.6954728e-08 3.4863374e-04 1.3576148e-03 1.5613044e-07
  1.2719647e-05 1.7137844e-09 9.9816775e-01 6.4207752e-06 7.4854906e-05]]
728x90

이 글을 공유하기

댓글

Designed by JB FACTORY