政策资讯

Policy Information


TF之LiR:基于tensorflow实现手写数字图片识别准确率

来源: 重庆市软件正版化服务中心    |    时间: 2022-09-20    |    浏览量: 52765    |   

TF之LiR:基于tensorflow实现手写数字图片识别准确率

目录

输出结果

代码设计


输出结果

  1. Extracting MNIST_data\train-images-idx3-ubyte.gz
  2. Please use tf.data to implement this functionality.
  3. Extracting MNIST_data\train-labels-idx1-ubyte.gz
  4. Please use tf.one_hot on tensors.
  5. Extracting MNIST_data\t10k-images-idx3-ubyte.gz
  6. Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
  7. Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
  8. Datasets(train=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x00000207535F9EB8>, validation=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x00000207611319E8>, test=<tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x0000020761131A20>)
  9. 迭代次数Epoch: 0001 下降值cost= 0.000000000
  10. 迭代次数Epoch: 0002 下降值cost= 0.000000000
  11. 迭代次数Epoch: 0003 下降值cost= 0.000000000
  12. 迭代次数Epoch: 0004 下降值cost= 0.000000000
  13. 迭代次数Epoch: 0005 下降值cost= 0.000000000
  14. 迭代次数Epoch: 0006 下降值cost= 0.000000000
  15. 迭代次数Epoch: 0007 下降值cost= 0.000000000
  16. 迭代次数Epoch: 0008 下降值cost= 0.000000000
  17. 迭代次数Epoch: 0009 下降值cost= 0.000000000
  18. 迭代次数Epoch: 0010 下降值cost= 0.000000000
  19. 迭代次数Epoch: 0011 下降值cost= 0.000000000
  20. 迭代次数Epoch: 0012 下降值cost= 0.000000000
  21. 迭代次数Epoch: 0013 下降值cost= 0.000000000
  22. 迭代次数Epoch: 0014 下降值cost= 0.000000000
  23. 迭代次数Epoch: 0015 下降值cost= 0.000000000
  24. 迭代次数Epoch: 0016 下降值cost= 0.000000000
  25. ……
  26. 迭代次数Epoch: 0099 下降值cost= 0.000000000
  27. 迭代次数Epoch: 0100 下降值cost= 0.000000000
  28. Optimizer Finished!

代码设计

  1. -*- coding: utf-8 -*-
  2. TF之LiR:基于tensorflow实现手写数字图片识别准确率
  3. import os
  4. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  5. import tensorflow as tf
  6. from tensorflow.examples.tutorials.mnist import input_data
  7. mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
  8. print(mnist)
  9. 设置超参数
  10. lr=0.001 学习率
  11. training_iters=100 训练次数
  12. batch_size=128 每轮训练数据的大小,如果一次训练5000张图片,电脑会卡死,分批次训练会更好
  13. display_step=1
  14. tf Graph的输入
  15. x=tf.placeholder(tf.float32, [None,784])
  16. y=tf.placeholder(tf.float32, [None, 10])
  17. 设置权重和偏置
  18. w =tf.Variable(tf.zeros([784,10]))
  19. b =tf.Variable(tf.zeros([10]))
  20. 设定运行模式
  21. pred =tf.nn.softmax(tf.matmul(x,w)+b)
  22. 设置cost function为cross entropy
  23. cost =tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
  24. GD算法
  25. optimizer=tf.train.GradientDescentOptimizer(lr).minimize(cost)
  26. 初始化权重
  27. init=tf.global_variables_initializer()
  28. 开始训练
  29. with tf.Session() as sess:
  30. sess.run(init)
  31. for epoch in range(training_iters): 输入所有训练数据
  32. avg_cost=0.
  33. total_batch=int(mnist.train.num_examples/batch_size)
  34. for i in range(total_batch): 遍历每个batch
  35. batch_xs,batch_ys=mnist.train.next_batch(batch_size)
  36. _, c=sess.run([optimizer,cost],feed_dict={x:batch_xs,y:batch_ys}) 把每个batch数据放进去训练
  37. avg_cost==c/total_batch
  38. if (epoch+1) % display_step ==0: 显示每次迭代日志
  39. print("迭代次数Epoch:","%04d" % (epoch+1),"下降值cost=","{:.9f}".format(avg_cost))
  40. print("Optimizer Finished!")
  41. 测试模型
  42. correct_prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
  43. accuracy=tf.equal_mean(tf.cast(correct_prediction),tf.float32)
  44. print("Accuracy:",accuracy_eval({x:mnist.test.image[:3000],y:mnist}))

评论

产品推荐

更多 >

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈