import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.datasets import mnist
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
def classificationReport(y_test,y_pred,category_names):
print('start doing report')
report_string = classification_report(y_test,y_pred,target_names=category_names)
print("report_string=",report_string)
def history_plot(model_id,history):
plt.figure(figsize=(8,8))
plt.subplot(2,1,1)
plt.title('cross entropy loss -'+model_id,fontsize=12)
plt.plot(history.history['loss'],color='blue',label='train')
plt.plot(history.history['val_loss'],color='orange',label='val')
plt.xlabel("Epochs",fontsize=12)
plt.ylabel("loss",fontsize=12)
plt.legend(loc='upper right')
plt.subplot(2,1,2)
plt.title('classification accuracy'+model_id,fontsize=10)
plt.plot(history.history['accuracy'],color='blue',label='train')
# plt.plot()
plt.plot(history.history['val_accuracy'],color='orange',label='val')
plt.xlabel("Epochs",fontsize=12)
plt.ylabel("Accuracy",fontsize=12)
plt.legend(loc='lower right')
plt.show()
def cnn_model():
model=Sequential()
model.add(Conv2D(32,5,5, padding='same',input_shape=(1,28,28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
seed=10
np.random.seed(seed)
(X_train,y_train), (X_test, y_test)= mnist.load_data()
# Convert the data into float values
X_train=X_train.reshape(X_train.shape[0], 1,28,28).astype('float32')
X_test=X_test.reshape(X_test.shape[0], 1,28,28).astype('float32')
# Normalize the data
X_train=X_train/255
X_test=X_test/255
y_train= np_utils.to_categorical(y_train)
y_test= np_utils.to_categorical(y_test)
num_classes=y_train.shape[1]
print(num_classes)
model=cnn_model()
history=model.fit(X_train, y_train, validation_data=(X_test,y_test),epochs=100, batch_size=200, verbose=2)
score= model.evaluate(X_test, y_test, verbose=0)
print('The error is: %.2f%%'%(100-score[1]*100))
history_plot('CNN',history)
50个epoch后:
100个epoches后:
TODO :
1.没有画那个roc曲线 计算auc的值 .
2.没有进行data arguation.
3.这个没有区分validation test.
4.如果想要提升一下性能 能不能增加一下这个的数据量呢。