開坑之前
今年3、4月份的時候就買了這本書,同時還買了另外一本更為淺顯的書,當時讀不懂這本,所以一度以為這本書很一般,前些日子看見知乎有人推薦它,也就拿出來翻翻看,發現寫的的確蠻好,只是稍微深一點,當時的自己理解不了罷了。另外一方面,感覺自己雖然對tensorflow比較熟稔,但是由於一開始的學習期對於編程實在太白,所以基礎並不牢靠,今來重讀之,在技術層面:希望能對tensorflow有個更為系統的理解,希望對基於深度學習的圖像、文字、強化學習有更為系統的認識,希望對tensorflow基礎編碼之外包含GPU加速、分布式、可視化、代碼調試等等輔助模塊有更深的見地。而由於這本書的編寫結構很好,所以也想借這本書,對深度學習發展有個系統的梳理,這也就意味着我不僅會跟着體會比較復雜的代碼網絡,簡單的腳本我也會寫,比如這篇的……
SoftMax分類器
softmax對mnist數據集合的准確率在92%左右,程序如下,
# Author : Hellcat # Time : 2017/12/6 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('../../../Mnist_data/',one_hot=True) print(mnist.train.images.shape,mnist.train.labels.shape) print(mnist.test.images.shape,mnist.test.labels.shape) print(mnist.validation.images.shape,mnist.validation.labels.shape) import tensorflow as tf sess = tf.InteractiveSession() x = tf.placeholder(tf.float32, [None,784]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.add(tf.matmul(x, W),b)) y_ = tf.placeholder(tf.float32,[None, 10]) # tf的函數繼承了np風格,能夠自動廣播,所以這里直接對onehot碼進行計算 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),axis=1)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) tf.global_variables_initializer().run() # 節點使用run ''' # 下面代碼也會報錯 # 說明op沒有tensor返回時會返回op實例作為替代 init = tf.global_variables_initializer() init.exal() ''' for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x:batch_xs, y_:batch_ys}) # equal函數會返回bool值,使用cast將之轉換為float32 correct_prediction = tf.equal(tf.argmax(y,axis=1), tf.argmax(y_,axis=1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print('准確率為:{0:.3f}'. format(accuracy.eval({x:mnist.test.images, y_:mnist.test.labels}))) # 張量使用eval
tensorflow理解深化:
operation.run()
tensor.eval()
a = operation()操作如果op返回tensor的話a會代表tensor,否則a會代表節點本身,此時啟動會話方式是有區別的。