# 存储模型的粒度
save_step=5# 创建保存模型文件的目录import os
skpt_dir="D:/Python_AI/断点续训/train_name/"ifnot os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir)# 创建saver对象
saver = tf.train.Saver()# 保存断点,在Session部分加入以下代码if(epoch+1)% save_step==0:
saver.save(sess,os.path.join(ckpt_dir,'train_name_model_{:06d}.ckpt'.format(epoch+1)))print('train_name_model_{:06d}.ckpt saved'.format(epoch+1))
saver.save(sess,os.path.join(ckpt_dir,'train_name_model.ckpt'))print('model saved')# 断点续训,在Session部分加入以下代码
ckpt=tf.train.get_checkpoint_state(skpt_dir)if skpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)print('restore model from ',ckpt.model_checkpoint_path)
卷积神经网络(CNN)
from keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense
from keras.models import Sequential
from keras.optimizers import RMSprop
from keras.callbacks import ReduceLROnPlateau
# 网络结构((conv->pool->dropout)*2->Flatten->Dense->dropout->Dense(out))
model=Sequential()
model.add(Conv2D())
model.add(MaxPooling2D())
model.add(Dropout())
model.add(Conv2D())
model.add(MaxPooling2D())
model.add(Dropout())
model.add(Flatten())
model.add(Dense())
model.add(Dropout())
model.add(Dense())# Define the optimizer
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)# 配置模型
model.compile(optimizer = optimizer , loss ="categorical_crossentropy", metrics=["accuracy"])# Set a learning rate annealer
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
epochs =1# Turn epochs to 30 to get 0.9967 accuracy
batch_size =86# Fit the model
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose =2, steps_per_epoch=X_train.shape[0]// batch_size
, callbacks=[learning_rate_reduction])# Plot the loss and accuracy curves for training and validation
fig, ax = plt.subplots(2,1)
ax[0].plot(history.history['loss'], color='b', label="Training loss")
ax[0].plot(history.history['val_loss'], color='r', label="validation loss",axes =ax[0])
legend = ax[0].legend(loc='best', shadow=True)
ax[1].plot(history.history['acc'], color='b', label="Training accuracy")
ax[1].plot(history.history['val_acc'], color='r',label="Validation accuracy")
legend = ax[1].legend(loc='best', shadow=True)# Display some error results # Errors are difference between predicted labels and true labels
errors =(Y_pred_classes - Y_true !=0)
Y_pred_classes_errors = Y_pred_classes[errors]
Y_pred_errors = Y_pred[errors]
Y_true_errors = Y_true[errors]
X_val_errors = X_val[errors]defdisplay_errors(errors_index,img_errors,pred_errors, obs_errors):""" This function shows 6 images with their predicted and real labels"""
n =0
nrows =2
ncols =3
fig, ax = plt.subplots(nrows,ncols,sharex=True,sharey=True)for row inrange(nrows):for col inrange(ncols):
error = errors_index[n]
ax[row,col].imshow((img_errors[error]).reshape((28,28)))
ax[row,col].set_title("Predicted label :{}\nTrue label :{}".format(pred_errors[error],obs_errors[error]))
n +=1# Probabilities of the wrong predicted numbers
Y_pred_errors_prob = np.max(Y_pred_errors,axis =1)# Predicted probabilities of the true values in the error set
true_prob_errors = np.diagonal(np.take(Y_pred_errors, Y_true_errors, axis=1))# Difference between the probability of the predicted label and the true label
delta_pred_true_errors = Y_pred_errors_prob - true_prob_errors
# Sorted list of the delta prob errors
sorted_dela_errors = np.argsort(delta_pred_true_errors)# Top 6 errors
most_important_errors = sorted_dela_errors[-6:]# Show the top 6 errors
display_errors(most_important_errors, X_val_errors, Y_pred_classes_errors, Y_true_errors)# predict results
results = model.predict(test)# select the indix with the maximum probability
results = np.argmax(results,axis =1)
results = pd.Series(results,name="Label")
submission = pd.concat([pd.Series(range(1,28001),name ="ImageId"),results],axis =1)
submission.to_csv("C:/Users/lihao/Desktop/cnn_mnist_datagen.csv",index=False)