TensorFlow學習1——MNIST入門

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


#import data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

讀取MNIST_data,數據集在官網http://yann.lecun.com/exdb/mnist/

變量在定義時要初始化,但是如果有些變量剛開始我們並不知道它們的值,無法初始化,那怎麼辦呢?
那就用佔位符來佔個位置


# Create the model

x = tf.placeholder(tf.float32, [None, 784])

定義784*10的全0矩陣

W = tf.Variable(tf.zeros([784, 10]))

定義10個全0

b = tf.Variable(tf.zeros([10]))

softmax迴歸概率爲0~1,神經元y=x*W+b

y = tf.nn.softmax(tf.matmul(x, W) + b)

定義誤差和優化器
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

初始化變量
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()




# Train the model and save the model to disk as a model.ckpt file
# file is stored in the same directory as this python script is started
"""
The use of 'with tf.Session() as sess:' is taken from the Tensor flow documentation
on on saving and restoring variables.
https://www.tensorflow.org/versions/master/how_tos/variables/index.html
"""
with tf.Session() as sess:
    sess.run(init_op)
    for i in range(1000):

        batch_xs, batch_ys = mnist.train.next_batch(100)

60000行的訓練數據集(mnist.train)和10000行的測試數據集(mnist.test)。這樣的切分很重要,在機器學習模型設計時必須有一個單獨的測試數據集不用於訓練而是用來評估這個模型的性能,從而更加容易把設計的模型推廣到其他數據集上(泛化)。

正如前面提到的一樣,每一個MNIST數據單元有兩部分組成:一張包含手寫數字的圖片和一個對應的標籤。我們把這些圖片設爲“xs”,把這些標籤設爲“ys”。訓練數據集和測試數據集都包含xs和ys

        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


    save_path = saver.save(sess, "./model.ckpt")
    print ("Model saved in file: ", save_path)


       
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章