zoukankan      html  css  js  c++  java
  • 莫凡Python之keras 2

    莫凡Python 2

    Classifier 分类

    使用 mnist 数据集,这是0-9的图片数据,我们使用神经网络去识别这些图片。显示图片上的数据

    本质上是使用神经网络去分类。

    参考资料

    https://morvanzhou.github.io/tutorials/machine-learning/keras/2-2-classifier/

    数据预处理、熟悉数据

    # -*- coding: utf-8 -*-
    """ Classifier 分类 """
    from keras.datasets import mnist
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    import numpy as np
    from keras.models import Sequential
    from keras.layers import Dense
    import matplotlib.pyplot as plt
    # %% 数据处理、感受数据
    (X_train, Y_train), (X_test, Y_test) = mnist.load_data()
    
    for num in range(50):
        plt.imshow(X_train[num,:,:],cmap='Greys_r')
        plt.axis('off')
        plt.show()

    图片显示

    既然是图片数据集,我们可以通过显示图片来直接的了解这个数据集
    X_train 是600002828 的矩阵,是一个灰度图像矩阵,6000张图片,图片大小为28*28

    Y_trian 是 60000*1 的一维数组

    数字5
    数字5

    数字9
    数字9

    数字3
    数字3

    关键

    • reshape()的使用

    参考博客

    数据处理

    将图片数据变为 6000*784 的矩阵,也就是新矩阵的一行代表一幅图片 使用 reshape
    将标签数据化为 one_hot 使用函数 np_utils.to_categorical

    参考博客

    模型搭建

    导入的包

    """ Classifier 分类 """
    from keras.datasets import mnist
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    import numpy as np
    np.random.seed(1337)  # for reproducibility
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    import matplotlib.pyplot as plt
    from keras.utils import np_utils
    from keras.optimizers import RMSprop

    建立模型

    model = Sequential(
        [
            Dense(32, input_dim = 784 ),
            Activation('relu'),
            Dense(10),
            Activation('softmax')
        ]
    )
    rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
    

    注意

    Activation(‘softmax’) 其中softmax多用于分类问题,在是输出层中使用,输出一个类别的可能概率矩阵
    RMSprop函数中:

    • lr 为学习率


    参考连接
    https://keras-cn.readthedocs.io/en/latest/legacy/other/optimizers/

    编译模型

    model.compile(optimizer=rmsprop,loss=‘categorical_crossentropy’,metrics=[‘accuracy’])

    说明

    关于metrics
    关于metrics

    训练模型

    model.fit(X_train,Y_train,epochs=2,batch_size=32)

    模型评估

    loss,accuracy = model.evaluate(X_test,Y_test)

    • loss 误差 目标值 0
    • accuracy 准确率 目标值 100%

    模型检验

    主要通过model.predict() 函数,使用训练好的神经网络,输出对 测试集 中第一章图片 img_0的预测

    X_predict = X_test[:10,:]
    X_predict = X_predict.reshape(10,28,28)
    plt.imshow(X_predict[0,:,:],cmap='Greys_r')
    plt.axis('off')
    plt.show()
    img_0 = X_test[0,:].reshape(1,784)
    result = np.argmax(model.predict(img_0))
    print(result)

    关键

    model.predict() 返回值

    enter description here
    enter description here

    输出一个1*10的矩阵,分别对应图片上数字为0-9的可能性。
    这张图片对应的数字的最大可能性,就是该矩阵最大值对饮的下标

    获取array的最大值

    参考

    结果

    图片为:
    enter description here
    预测为:
    enter description here

  • 相关阅读:
    toj4119HDFS
    hdu2952Counting Sheep
    hdu2393Higher Math
    hdu2317Nasty Hacks
    hdu2309ICPC Score Totalizer Software
    hdu2304Electrical Outlets
    hdu2399GPA
    一、 软件测试概述
    JQuery选择器大全
    如何避免jQuery库和其他库的冲突
  • 原文地址:https://www.cnblogs.com/Howbin/p/12582826.html
Copyright © 2011-2022 走看看