前言
对深度学习抱有很大的好奇,本来打算直接上手TensorFlow的,鉴于自己还是个小白,还是从简单的keras慢慢坑起,先入个门.通过这次学习,一直使用Pycharm的我发现这种项目适合使用jupyter notebook.jupyter将程序分成多个单元格解释执行,训练一次模型,就可以添加更多单元格程序对结果进行分析.
源码
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
import matplotlib.pyplot as plt
import pandas as pd
def plot_image_index(images,labels,prediction,indexs):
"""
:param images: 点阵像素点图像
:param labels: 图像真实值
:param indexs: 索引列表
:return:
"""
fig = plt.gcf() # 实例化图形
fig.set_size_inches(15, 15) # 设定图形大小
i=0
for index in indexs:
i+=1
if i>25:
break
ax = plt.subplot(5, 5, i) # 建立sub_graph子图
ax.imshow(images[index], cmap="binary") # 画出子图
title = str(index)+":label=" + str(labels[index]) # 设置子图title变量用以显示标签字段
if len(prediction) > 0: # 如果传入预测结果
title += ",predict=" + str(prediction[index]) # 设置标题加入预测结果
ax.set_title(title, fontsize=10) # 设置子图标题
ax.set_xticks([]) # 设置不显示刻度
ax.set_yticks([])
plt.show()
def images_labels_prediction(images, labels, prediction, index, num=10):
"""
:param images: 点阵像素点图像列表
:param labels: 图像对应真实值
:param prediction: 预测结果
:param index: 数据开始索引
:param num: 显示数据项数
:return:
"""
if num > 25:
num = 25
fig = plt.gcf() # 实例化图形
fig.set_size_inches(15, 15) # 设定图形大小
for i in range(0, num):
ax = plt.subplot(5, 5, 1 + i) # 建立sub_graph子图,为5行5列
ax.imshow(images[index], cmap="binary") # 画出子图
title = "label=" + str(labels[index]) # 设置子图title变量用以显示标签字段
if len(prediction) > 0: # 如果传入预测结果
title += ",predict=" + str(prediction[index]) # 设置标题加入预测结果
ax.set_title(title, fontsize=10) # 设置子图标题
ax.set_xticks([]) # 设置不显示刻度
ax.set_yticks([])
index += 1
plt.show()
def show_train_history(train_history, mode="acc"):
"""
:param train_history: 训练历史
:param mode: 可视化模式,acc/loss->准确率或失败率
:return:
"""
plt.plot(train_history.history[mode])
plt.plot(train_history.history["val_" + mode])
plt.title("Train History")
plt.ylabel(mode)
plt.xlabel("Epoch")
plt.legend(["train", "validation"], loc="upper left")
plt.show()
def image_resha