Yiksan0315's Blog

Keras

# Tag:


ANN with Keras

데이터 전처리 과정

from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Activation from tensorflow.keras.utils import to_categorical from tensorflow.keras.datasets import mnist import numpy as np (x_train, y_train), (x_test, y_test) = mnist.load_data() X_train = x_train.reshape(60000, 784) X_test = x_test.reshape(10000, 784) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 # Nomarlization X_test /= 255 # Nomarlization Y_train = to_categorical(y_train, 10) Y_test = to_categorical(y_test, 10)
  • Sequential: 순차적인 ANN을 구성할 때 사용할 수 있는 함수
  • Dense: 전결합층(fully-connected layer): 각 Layer의 Node 개수를 설정할 수 있다.
  • to_categorical: One Hot Encoding, 수치형 데이터를 범주형 데이터로 변환하는 것이다.
  • from tensorflow.keras.datasets import mnist: Keras를 사용하여 딥러닝 모델 개발을 연습할 수 있는 여러 데이터 중 mnist를 불러옴.

ANN 설계 과정

model = Sequential() model.add(Dense(512, input_shape=(784,))) model.add(Activation('relu')) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dense(10)) model.add(Activation('softmax')) # Nomalization model.summary()
  • input_shape: 입력하는 데이터의 형태
  • Dense: 은닉층의 Node 개수 결정

오차 계산과 줄이기

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=128, epochs=10, verbose=1)
  • compile: 학습 방법을 정하는 function
    • loss='categorical_crossentropy': 다중 분류 손실 함수(Loss)
    • optimizer='adam': Gradient Descent
    • metrics: 학습 결과, accuracy는 정확도
  • fit: 실제 학습 실행
    • batch_size: 인공지능이 한번에 학습하는 데이터의 수
    • epochs: 데이터 학습 반복 횟수
    • verbose: fit 함수의 결과값을 출력하는 방법.
verbose 값의미
0아무런 표시를 하지 않음
1에포크별 진행 사항을 알려줌
2에포크별 학습 결과를 알려줌

모델 정확도 평가

score = model.evaluate(X_test, Y_test) print('Test score:', score[0]) print('Test accuracy:', score[1])
  • score[0]: loss, 오차값
  • score[1]: accuracy, 정확

모델 학습결과

predicted_classes = np.argmax(model.predict(X_test), axis=1) correct_indices = np.nonzero(predicted_classes == y_test)[0] incorrect_indices = np.nonzero(predicted_classes != y_test)[0]
  • numpy 끼리의 연산에서 !=, ==은 각각 뒤의 인자와의 비교한 numpy array를 반환한다.
  • nonzero는 0이 아닌 요소의 인덱스를 반환
toc test

이 페이지는 리디주식회사에서 제공한 리디바탕 글꼴이 사용되어 있습니다. 리디바탕의 저작권은 리디주식회사가 소유하고 있습니다.

This Font Software is licensed under the SIL Open Font License, Version 1.1.

Copyright 2025. yiksan0315 All rights reserved.