參考內(nèi)容:
tensorflow中文社區(qū)
tensorflow官網(wǎng)教程
寫在MNIST前
先瀏覽新手入門介紹和基本用法
Getting Started With TensorFlow
1. 查看tensorflow安裝版本和路徑
在python中import tensorflow as tf
,通過tf.__version__
和tf.__path__
可以查看安裝版本和路徑如下:
2. 三維數(shù)據(jù)平面擬合
在使用tensorflow函數(shù)時參考其官網(wǎng)API速兔。根據(jù)tensorflow中文社區(qū)中新手入門簡介墅拭,試用一下tensorflow,代碼如下:
-VirtualBox:~$ python
Python 2.7.13 |Anaconda 4.3.1 (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import tensorflow as tf
>>> import numpy as np
#x_data為2*100矩陣涣狗,y_data為1*100矩陣谍婉,初始化y_data = [0.100, 0.200] * x_data + 0.300
>>> x_data = np.float32(np.random.rand(2, 100))
>>> y_data = np.dot([0.100, 0.200], x_data) + 0.300
#構(gòu)造一個線性模型,初始化權(quán)值矩陣W和偏置b镀钓,線性模型y = W * x_data + b
>>> b = tf.Variable(tf.zeros([1]))
>>> W = tf.Variable(tf.random_uniform([1,2], -1.0, 1.0))
>>> y = tf.matmul(W, x_data) + b
#最小化方差
>>> loss = tf.reduce_mean(tf.square(y-y_data))
>>> optimizer = tf.train.GradientDescentOptimizer(0.5)
>>> train = optimizer.minimize(loss)
#初始化變量穗熬,因為tf.initialize_all_variables()函數(shù)已被移除,根據(jù)提示使用tf.global_variables_initializer()進行初始化設(shè)置
>>> init = tf.initialize_all_variables()
WARNING:tensorflow:From <stdin>:1: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
>>> init = tf.global_variables_initializer()
#啟動圖
>>> sess = tf.Session()
>>> sess.run(init)
#擬合平面丁溅,在命令行時注意python中的TAB縮進
>>> for step in xrange(0, 201):
... sess.run(train)
... if step%20 == 0:
... print step, sess.run(W), sess.run(b)
...
得到最佳擬合結(jié)果 W: [[0.100 0.200]], b: [0.300]唤蔗,本次運行結(jié)果如下:
MNIST機器學(xué)習(xí)入門
1. MNIST數(shù)據(jù)下載
按照TF官網(wǎng)的說明,使用如下代碼下載并讀取MNIST數(shù)據(jù)集窟赏。
~$ python
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
此時措译,mnist.train[55000]、mnist.validation[5000]饰序、mnist.test[10000]中已經(jīng)保存了訓(xùn)練數(shù)據(jù)、驗證數(shù)據(jù)和測試數(shù)據(jù)规哪。mnist.train.images [55000×784]和mnist.train.labels[55000×10]中保存了訓(xùn)練數(shù)據(jù)圖片和標(biāo)簽求豫。
根據(jù)
/anaconda2/lib/python2.7/site-packages/tensorflow/examples/tutorials/mnist/input_data.py
文件可以看到from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
,因此在/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py
中可以找到相關(guān)數(shù)據(jù)讀取的函數(shù)。
2. MNIST初試
softmax函數(shù)參見實現(xiàn)回歸模型的前一部分蝠嘉。
Cross-entropy交叉熵作為loss function最疆,其中y是預(yù)測的值分布,y'是我們樣本中的label值蚤告。
Tensorflow不單獨地運行單一的復(fù)雜計算努酸,而是先用圖描述一系列可交互的計算操作,然后全部一起在Python之外運行杜恰,避免過多的開銷获诈。
僅使用softmax函數(shù),沒有用到mnist.validation中的數(shù)據(jù)心褐,實現(xiàn)MNIST識別如下:
-VirtualBox:~$ python
...
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
#實現(xiàn)回歸模型
>>> import tensorflow as tf
>>> 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.matmul(x, W) + b)
#訓(xùn)練模型
>>> y_ = tf.placeholder(tf.float32, [None, 10])
>>> cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
>>> train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
>>> sess = tf.InteractiveSession()
>>> tf.global_variables_initializer().run()
>>> for _ 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, tf.float32))
>>> print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
0.9183
>>>
這里舔涎,通過使用InteractiveSession類,可以更加靈活地構(gòu)建代碼逗爹。它能在運行圖的時候亡嫌,插入一些計算圖,這些計算圖是由某些操作(operations)構(gòu)成的掘而。這對于工作在交互式環(huán)境中的人們來說非常便利挟冠,比如使用IPython。如果沒有使用InteractiveSession袍睡,那么需要在啟動session之前構(gòu)建整個計算圖知染,然后啟動該計算圖。
MNIST深入
使用CNN實現(xiàn)MNIST識別
-VirtualBox:~$ python
...
#讀入數(shù)據(jù)
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
>>> import tensorflow as tf
>>> sess = tf.InteractiveSession()
#定義占位符x和y_
>>> x = tf.placeholder(tf.float32, shape=[None, 784])
>>> y_ = tf.placeholder(tf.float32, shape=[None, 10])
#定義用于初始化的兩個函數(shù)
>>> def weight_variable(shape):
... initial = tf.truncated_normal(shape, stddev=0.1)
... return tf.Variable(initial)
...
>>> def bias_variable(shape):
... initial = tf.constant(0.1, shape=shape)
... return tf.Variable(initial)
...
#定義卷積和池化的函數(shù)
#卷積使用1步長(stride size)女蜈,0邊距(padding size)的模板持舆,保證輸出和輸入大小相同
#池化用簡單傳統(tǒng)的2x2大小的模板做max pooling,因此輸出的長寬會變?yōu)檩斎氲囊话?>>> def conv2d(x, W):
... return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
...
>>> def max_pool_2x2(x):
... return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
...
#第一層卷積伪窖,卷積在每個5x5的patch中算出32個特征
>>> W_conv1 = weight_variable([5,5,1,32])
>>> b_conv1 = bias_variable([32])
>>> x_image = tf.reshape(x, [-1,28,28,1])
#第2逸寓、第3維對應(yīng)圖片的寬、高覆山,最后一維代表圖片的顏色通道數(shù)(因為是灰度圖所以這里的通道數(shù)為1竹伸,如果是rgb彩色圖,則為3)
>>> h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
>>> h_pool1 = max_pool_2x2(h_conv1)
>>>
#第二層卷積簇宽,每個5x5的patch會得到64個特征
>>> W_conv2 = weight_variable([5,5,32,64])
>>> b_conv2 = bias_variable([64])
>>> h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
>>> h_pool2 = max_pool_2x2(h_conv2)
>>>
#有1024個神經(jīng)元的全連接層勋篓,此時圖片大小為7*7
>>> W_fc1 = weight_variable([7*7*64, 1024])
>>> b_fc1 = bias_variable([1024])
>>> h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
>>> h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
>>>
#為了減少過擬合,在輸出層之前加入dropout魏割。用一個placeholder代表一個神經(jīng)元的輸出在dropout中保持不變的概率譬嚣。
#這樣可以在訓(xùn)練過程中啟用dropout,在測試過程中關(guān)閉dropout钞它。
>>> keep_prob = tf.placeholder(tf.float32)
>>> h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#softmax輸出層
>>> W_fc2 = weight_variable([1024, 10])
>>> b_fc2 = bias_variable([10])
>>> y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#應(yīng)為 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
>>>
#訓(xùn)練和評估模型
#用更加復(fù)雜的ADAM優(yōu)化器來做梯度最速下降拜银,在feed_dict中加入額外的參數(shù)keep_prob來控制dropout比例
>>> cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
>>> train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
>>> correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
>>> accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
>>> sess.run(tf.global_variables_initializer())
>>> for i in range(2000): #為減少訓(xùn)練時間殊鞭,降低迭代次數(shù)
... batch = mnist.train.next_batch(50)
... if i%100 == 0:
... train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0})
... print("step %d, training accuracy %g"%(i, train_accuracy))
... train_step.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})
...
#檢測模型的準(zhǔn)確率
print("test accuracy %g"%accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0}))
Tensorflow.CNN
為了避免之后github訪問不通,先clone了Tensorflow models上的內(nèi)容
mkdir TensorFlowModels
cd TensorFluwModels
git clone https://github.com/tensorflow/models.git
TensorFlow運作方式入門
TensorFlow Mechanics 101
瀏覽運作方式入門
卷積神經(jīng)網(wǎng)絡(luò)
CNN
CNN基于CIFAR-10數(shù)據(jù)集尼桶,其任務(wù)是對一組32x32RGB的圖像進行分類操灿,這些圖像涵蓋了10個類別:飛機, 汽車泵督, 鳥趾盐, 貓, 鹿小腊, 狗救鲤, 青蛙, 馬溢豆, 船以及卡車蜒简。