700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > TF之LiR:基于tensorflow实现手写数字图片识别准确率

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

时间:2020-11-22 21:39:39

相关推荐

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

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

目录

输出结果

代码设计

输出结果

Extracting MNIST_data\train-images-idx3-ubyte.gzPlease use tf.data to implement this functionality.Extracting MNIST_data\train-labels-idx1-ubyte.gzPlease use tf.one_hot on tensors.Extracting MNIST_data\t10k-images-idx3-ubyte.gzExtracting MNIST_data\t10k-labels-idx1-ubyte.gzPlease use alternatives such as official/mnist/dataset.py from tensorflow/models.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>)迭代次数Epoch: 0001 下降值cost= 0.000000000迭代次数Epoch: 0002 下降值cost= 0.000000000迭代次数Epoch: 0003 下降值cost= 0.000000000迭代次数Epoch: 0004 下降值cost= 0.000000000迭代次数Epoch: 0005 下降值cost= 0.000000000迭代次数Epoch: 0006 下降值cost= 0.000000000迭代次数Epoch: 0007 下降值cost= 0.000000000迭代次数Epoch: 0008 下降值cost= 0.000000000迭代次数Epoch: 0009 下降值cost= 0.000000000迭代次数Epoch: 0010 下降值cost= 0.000000000迭代次数Epoch: 0011 下降值cost= 0.000000000迭代次数Epoch: 0012 下降值cost= 0.000000000迭代次数Epoch: 0013 下降值cost= 0.000000000迭代次数Epoch: 0014 下降值cost= 0.000000000迭代次数Epoch: 0015 下降值cost= 0.000000000迭代次数Epoch: 0016 下降值cost= 0.000000000……迭代次数Epoch: 0099 下降值cost= 0.000000000迭代次数Epoch: 0100 下降值cost= 0.000000000Optimizer Finished!

代码设计

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。