반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

keras.mnist(중간층) 본문

[AI]/python.keras

keras.mnist(중간층)

givemebro 2020. 5. 13. 17:42
반응형
import keras
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test)=mnist.load_data()

# 데이터 전처리

X_train=X_train.reshape(-1,28*28)/255.
X_test=X_test.reshape(-1,28*28)/255.
y_train=np.eye(10)[y_train]
y_test=np.eye(10)[y_test]


model=Sequential()
model.add(Dense(128,activation='relu',input_shape=(784,)))

# 중간층 추가
model.add(Dense(256,activation='relu'))
model.add(Dense(256,activation='relu'))
model.add(Dense(10,activation='softmax'))

 

 

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_30 (Dense)             (None, 128)               100480    
_________________________________________________________________
dense_31 (Dense)             (None, 256)               33024     
_________________________________________________________________
dense_32 (Dense)             (None, 256)               65792     
_________________________________________________________________
dense_33 (Dense)             (None, 10)                2570      
=================================================================
Total params: 201,866
Trainable params: 201,866
Non-trainable params: 0
_________________________________________________________________

 

model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['acc'])
model.fit(X_train,y_train,epochs=10,batch_size=128,validation_split=0.2)
# batch_size=128 : 미니배치 사이즈
# validation_split=0.2 : 검증용 데이터
# 훈련 > 검증 > 평가

# Train on 48000 samples : 훈련
# validate on 12000 samples : 검증용
Train on 48000 samples, validate on 12000 samples
Epoch 1/10
48000/48000 [==============================] - 2s 34us/step - loss: 0.3152 - acc: 0.9046 - val_loss: 0.1365 - val_acc: 0.9608
Epoch 2/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.1256 - acc: 0.9607 - val_loss: 0.1082 - val_acc: 0.9672
Epoch 3/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0841 - acc: 0.9743 - val_loss: 0.1104 - val_acc: 0.9664
Epoch 4/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0621 - acc: 0.9810 - val_loss: 0.0987 - val_acc: 0.9721
Epoch 5/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0486 - acc: 0.9849 - val_loss: 0.1051 - val_acc: 0.9708
Epoch 6/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0373 - acc: 0.9883 - val_loss: 0.0982 - val_acc: 0.9772
Epoch 7/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0316 - acc: 0.9900 - val_loss: 0.1155 - val_acc: 0.9723
Epoch 8/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0256 - acc: 0.9915 - val_loss: 0.1300 - val_acc: 0.9720
Epoch 9/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0219 - acc: 0.9928 - val_loss: 0.1080 - val_acc: 0.9768
Epoch 10/10
48000/48000 [==============================] - 1s 27us/step - loss: 0.0172 - acc: 0.9946 - val_loss: 0.1205 - val_acc: 0.9763
<keras.callbacks.History at 0x15e2609eb38>

 

 

# score train
pred_y_train=model.predict(X_train)

(pred_y_train*y_train).sum()/len(y_train)
0.9914280191622107

 

 

# score test
pred_y_test=model.predict(X_test)
(pred_y_test*y_test).sum()/len(y_test)
0.9756678668438166

 

 

# 틀린 그림 출력하기
index=np.where(y_test.argmax(axis=1)!=pred_y_test.argmax(axis=1))

j=0
for i in index[0][:12]:
    j=j+1
    plt.subplot(12/4,4,j)
    plt.imshow(X_test[i].reshape(28,28))
    

 

반응형

'[AI] > python.keras' 카테고리의 다른 글

keras.layers.Dropout  (0) 2020.05.13
keras.layers.Flatten  (0) 2020.05.13
keras.save  (0) 2020.05.13
keras.score  (0) 2020.05.13
keras.mnist(중간층X)  (0) 2020.05.13
keras.mnist  (0) 2020.05.13
keras.iris(분류)  (0) 2020.05.13
keras.basic(분류)  (0) 2020.05.13
Comments