빅데이터/머신 러닝 with Python (old)

Keras를 이용한 딥러닝 시작, ANN-DNN, 손글씨 인식시키기

언제나휴일 2020. 8. 17. 18:01
반응형

 

#데이터 로딩
import numpy as np 
from keras import datasets              
data_train,data_test = datasets.mnist.load_data()
image_train, label_train = data_train
image_test, label_test = data_test

#데이터 정제화
from keras.utils import np_utils
label_train2 = np_utils.to_categorical(label_train)
print(label_test[0])
label_test2 = np_utils.to_categorical(label_test)
print(label_test2[0])
length,weight,height = image_train.shape
image_train2 = image_train.reshape(-1,weight*height)
image_test2 = image_test.reshape(-1,weight*height)
print("image_test2의 구조:",image_test2.shape)

print(image_train2[0][200:210])
image_train2 = image_train2 / 255.0
print(image_train2[0][200:210])
image_test2 = image_test2 / 255.0
#모델 생성
from keras import layers,models
model = models.Sequential()
#모델에 계층 추가 
model.add(layers.Dense(100,activation="relu",input_shape=(784,)))
model.add(layers.Dense(28,activation='relu'))
model.add(layers.Dense(10,activation='softmax'))          
#모델 컴파일
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])#스텝 방향Momentum, 크기Adgrad-RMSProp
#모델 학습
model.fit(image_train2,label_train2,epochs=15)
#모델 예측 및 평가
score = model.evaluate(image_test2,label_test2)
print("score:",score)
반응형