keras多层感知器识别mnist

前言

对深度学习抱有很大的好奇,本来打算直接上手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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值