TF之NN:利用DNN算法(SGD+softmax+cross_entropy)对mnist手写数字图片识别训练集(TF自带函数下载)实现87.4%识别
生活随笔
收集整理的這篇文章主要介紹了
TF之NN:利用DNN算法(SGD+softmax+cross_entropy)对mnist手写数字图片识别训练集(TF自带函数下载)实现87.4%识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TF之NN:利用DNN算法(SGD+softmax+cross_entropy)對mnist手寫數字圖片識別訓練集(TF自帶函數下載)實現87.4%識別
?
?
目錄
輸出結果
代碼設計
?
?
?
輸出結果
?
代碼設計
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.examples.tutorials.mnist import input_dataprint ("packs loaded") print ("Download and Extract MNIST dataset") mnist = input_data.read_data_sets('/tmp/data/', one_hot=True) print print (" tpye of 'mnist' is %s" % (type(mnist))) print (" number of trian data is %d" % (mnist.train.num_examples)) print (" number of test data is %d" % (mnist.test.num_examples))packs loaded Download and Extract MNIST dataset tpye of 'mnist' is <class 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets'> number of trian data is 55000 number of test data is 10000?
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #這是TensorFlow 為了教學Mnist而提前設計好的程序 # number 1 to 10 data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #TensorFlow 會檢測數據是否存在。當數據不存在時,系統會自動將數據下載到MNIST_data/文件夾中。當執行完語句后,讀者可以自行前往MNIST_data/文件夾下查看上述4 個文件是否已經被正確地下載def add_layer(inputs, in_size, out_size, activation_function=None,):# add one more layer and return the output of this layerWeights = tf.Variable(tf.random_normal([in_size, out_size]))biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)Wx_plus_b = tf.matmul(inputs, Weights) + biasesif activation_function is None:outputs = Wx_plus_belse:outputs = activation_function(Wx_plus_b,)return outputsdef compute_accuracy(v_xs, v_ys): global prediction y_pre = sess.run(prediction, feed_dict={xs: v_xs}) correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys}) return result# define placeholder for inputs to network xs = tf.placeholder(tf.float32, [None, 784]) ys = tf.placeholder(tf.float32, [None, 10]) # add output layer prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)# the error between prediction and real data cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.Session() # important step sess.run(tf.global_variables_initializer())for i in range(1000):batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})if i % 50 == 0:print(compute_accuracy(mnist.test.images, mnist.test.labels))?
?
相關文章
TF之NN:(TF自帶函數下載MNIST55000訓練集圖片)實現手寫數字識別87.4%準確率識別:SGD法+softmax法+cross_entropy法
相關文章
TF之NN:(TF自帶函數下載MNIST55000訓練集圖片)實現手寫數字識別87.4%準確率識別:SGD法+softmax法+cross_entropy法
TF之DNN:(TF自帶函數下載MNIST55000訓練集圖片)利用 784 個神經元的三層全連接的DNN對MNIST手寫數字識別實現98%準確率
?
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的TF之NN:利用DNN算法(SGD+softmax+cross_entropy)对mnist手写数字图片识别训练集(TF自带函数下载)实现87.4%识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI:机器学习、深度学习在实际应用(工业
- 下一篇: Competition——互联网比赛(编