• Tensorflow手写数字识别训练(梯度下降法)



    # coding: utf-8

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data

    #print("hello")

    #载入数据集
    mnist = input_data.read_data_sets("F:\TensorflowProject\MNIST_data",one_hot=True)

    #每个批次的大小,训练时一次100张放入神经网络中训练
    batch_size = 100

    #计算一共有多少个批次
    n_batch = mnist.train.num_examples//batch_size

    #定义两个placeholder
    x = tf.placeholder(tf.float32,[None,784])
    #0-9十个数字
    y = tf.placeholder(tf.float32,[None,10])

    #创建一个神经网络
    W = tf.Variable(tf.zeros([784,10]))
    b = tf.Variable(tf.zeros([10]))
    prediction = tf.nn.softmax(tf.matmul(x,W)+b)

    #二次代价函数
    loss = tf.reduce_mean(tf.square(y-prediction))
    #使用梯度下降法
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

    #初始化变量
    init = tf.global_variables_initializer()

    #结果存放在一个布尔型列表中
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    #求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    #
    with tf.Session() as sess:
      sess.run(init)
      for epoch in range(100):
        for batch in range(n_batch):
          batch_xs,batch_ys = mnist.train.next_batch(batch_size)
          sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        #测试准确率
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter: "+str(epoch)+" ,Testing Accuracy "+str(acc))

    #运行结果

    Extracting F:TensorflowProjectMNIST_data	rain-images-idx3-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	rain-labels-idx1-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	10k-images-idx3-ubyte.gz
    Extracting F:TensorflowProjectMNIST_data	10k-labels-idx1-ubyte.gz
    Iter: 0  ,Testing Accuracy  0.8322
    Iter: 1  ,Testing Accuracy  0.872
    Iter: 2  ,Testing Accuracy  0.8808
    Iter: 3  ,Testing Accuracy  0.888
    Iter: 4  ,Testing Accuracy  0.8938
    Iter: 5  ,Testing Accuracy  0.8969
    Iter: 6  ,Testing Accuracy  0.899
    Iter: 7  ,Testing Accuracy  0.9015
    Iter: 8  ,Testing Accuracy  0.9038
    Iter: 9  ,Testing Accuracy  0.9055
    Iter: 10  ,Testing Accuracy  0.9063
    Iter: 11  ,Testing Accuracy  0.9077
    Iter: 12  ,Testing Accuracy  0.9078
    ......
    Iter: 38  ,Testing Accuracy  0.9192
    Iter: 39  ,Testing Accuracy  0.9195
    Iter: 40  ,Testing Accuracy  0.92
    Iter: 41  ,Testing Accuracy  0.9199
    Iter: 42  ,Testing Accuracy  0.9205
    Iter: 43  ,Testing Accuracy  0.9201
    Iter: 44  ,Testing Accuracy  0.921
    Iter: 45  ,Testing Accuracy  0.9207
    Iter: 46  ,Testing Accuracy  0.9214
    Iter: 47  ,Testing Accuracy  0.9212
    Iter: 48  ,Testing Accuracy  0.9215
    Iter: 49  ,Testing Accuracy  0.9213
    .....
    Iter: 93  ,Testing Accuracy  0.9254
    Iter: 94  ,Testing Accuracy  0.9259
    Iter: 95  ,Testing Accuracy  0.926
    Iter: 96  ,Testing Accuracy  0.9262
    Iter: 97  ,Testing Accuracy  0.9263
    Iter: 98  ,Testing Accuracy  0.9262
    Iter: 99  ,Testing Accuracy  0.926
  • 相关阅读:
    protobuf简单测试应用
    golang代码片段(摘抄)
    ibatis中 $ 于 # 的 区别?
    eclipse debug 执行到断点处并没有停下,断点无效问题
    使用selenium遇到java.lang.NoSuchMethodError: org.apache.xpath.XPathContext,排查
    设置MAVEN_OPTS的推荐方法
    UI型Bug定义的处理方法
    select count(*)和select count(1)的区别
    mac下搭建java开发环境:eclipse+tomcat+maven
    eclipse中svn提交报错的解决
  • 原文地址:https://www.cnblogs.com/herd/p/9464359.html
Copyright © 2020-2023  润新知