zoukankan      html  css  js  c++  java
  • DLINACT01 第一个网络

    1. 导入MNIST数据集

    from keras.datasets import mnist
    
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
    
    train_images.shape
    
    (60000, 28, 28)
    
    train_labels
    
    array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
    
    test_images.shape
    
    (10000, 28, 28)
    
    test_labels
    
    array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)
    

    2. 构建网络结构

    from keras import models
    from keras import layers
    
    network = models.Sequential()
    network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
    network.add(layers.Dense(10, activation='softmax')) # 10路的softmax层,返回10个概率
    network.compile(optimizer='rmsprop',
                   loss='categorical_crossentropy',
                   metrics=['accuracy']) # 选择loss function, optimizer, metrics
    

    3. 准备图像数据

    train_images = train_images.reshape((60000, 28 * 28))
    train_images = train_images.astype('float32') / 255
    
    test_images = test_images.reshape((10000, 28 * 28))
    test_images = test_images.astype('float32') / 255
    

    4. 准备标签

    from keras.utils import to_categorical
    
    train_labels = to_categorical(train_labels)
    test_labels = to_categorical(test_labels)
    

    5. 训练模型

    network.fit(train_images, train_labels, epochs=5, batch_size=128)
    
    Epoch 1/5
    60000/60000 [==============================] - 5s 79us/step - loss: 0.2603 - acc: 0.9248
    Epoch 2/5
    60000/60000 [==============================] - 5s 76us/step - loss: 0.1049 - acc: 0.9687
    Epoch 3/5
    60000/60000 [==============================] - 5s 77us/step - loss: 0.0686 - acc: 0.9794
    Epoch 4/5
    60000/60000 [==============================] - 4s 73us/step - loss: 0.0504 - acc: 0.9849
    Epoch 5/5
    60000/60000 [==============================] - 5s 81us/step - loss: 0.0372 - acc: 0.9886
    
    
    
    
    
    <keras.callbacks.History at 0x20f3012a390>
    

    6. 评估模型

    test_loss, test_acc = network.evaluate(test_images, test_labels)
    print('test_acc', test_acc)
    
    10000/10000 [==============================] - 1s 53us/step
    test_acc 0.9805
    CS专业在读,热爱编程。
    专业之外,喜欢阅读,尤爱哲学、金庸、马尔克斯。
  • 相关阅读:
    Silverlight 学习笔记(二)silverligth 界面布局
    (C#)WinForm窗体间传值
    软件开发所需要的十三个文档
    XP下安装SQL2000企业版本(转载)
    .net 框架下快速Web开发(一)——纠结的里程
    .net MVC 框架调试过程中,产生大量的临时文件
    认识NHitenate
    如何循序渐进向.Net架构师发展[转载]
    核桃煲鸡汤
    .NET获取客户端信息 (C#)
  • 原文地址:https://www.cnblogs.com/jmhwsrr/p/15755938.html
Copyright © 2011-2022 走看看