zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然TensorFlow高层封装:使用TFLearn处理MNIST数据集实现LeNet-5模型

    # 1. 通过TFLearn的API定义卷机神经网络。
    
    import tflearn
    import tflearn.datasets.mnist as mnist
    
    from tflearn.layers.conv import conv_2d, max_pool_2d
    from tflearn.layers.estimator import regression
    from tflearn.layers.core import input_data, dropout, fully_connected
     
    trainX, trainY, testX, testY = mnist.load_data(data_dir="F:\TensorFlowGoogle\201806-github\datasets\MNIST_data", one_hot=True)
    # 将图像数据resize成卷积卷积神经网络输入的格式。
    trainX = trainX.reshape([-1, 28, 28, 1])
    testX = testX.reshape([-1, 28, 28, 1])
     
    # 构建神经网络。
    net = input_data(shape=[None, 28, 28, 1], name='input')
    net = conv_2d(net, 32, 5, activation='relu')
    net = max_pool_2d(net, 2)
    net = conv_2d(net, 64, 5, activation='relu')
    net = max_pool_2d(net, 2)
    net = fully_connected(net, 500, activation='relu')
    net = fully_connected(net, 10, activation='softmax')
    # 定义学习任务。指定优化器为sgd,学习率为0.01,损失函数为交叉熵。
    net = regression(net, optimizer='sgd', learning_rate=0.01,loss='categorical_crossentropy')
    # 2. 通过TFLearn的API训练神经网络。
    # 通过定义的网络结构训练模型,并在指定的验证数据上验证模型的效果。
    model = tflearn.DNN(net, tensorboard_verbose=0)
    model.fit(trainX, trainY, n_epoch=10,validation_set=([testX, testY]),show_metric=True)

  • 相关阅读:
    NOIP201105铺地毯
    50148155HYF旅游
    连通性判断
    传递消息1
    找朋友
    5796: 最短Hamilton路径(状压dp)
    2283: A Mini Locomotive(01背包)
    2616: Cow Frisbee Team(01背包)
    2593: Secret Message(字典树)
    Stammering Aliens(二分+Hash 卡过)
  • 原文地址:https://www.cnblogs.com/tszr/p/12070148.html
Copyright © 2011-2022 走看看