參考教程:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 數據下載地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_download.ht ...
參考教程:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html
數據下載地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_download.html
環境:windows+Python3.5+tensorflow
python代碼
from tensorflow.examples.tutorials.mnist import input_data #載入訓練數據 MNIST_data_folder=r"D:\WorkSpace\tensorFlow\data" mnist=input_data.read_data_sets(MNIST_data_folder,one_hot=True) # print(mnist.train.next_batch(1)) import tensorflow as tf # 建立抽象模型 x = tf.placeholder("float", [None, 784]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x,W) + b) y_ = tf.placeholder("float", [None,10]) # 定義損失函數和訓練方法 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) # 損失函數為交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # 梯度下降法,學習速率為0.01 # 訓練目標:最小化損失函數 init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))