TensorFlow的基本介绍及Hello,world
轉載自:
http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html
https://blog.csdn.net/sarsscofy/article/details/78541836
####基本使用:
####綜述:
圖中的節點被稱之為 op (operation 的縮寫)。 一個 op 獲得 0 個或多個 Tensor(圖中的有向邊), 執行計算, 產生 0 個或多個 Tensor.。每個 Tensor 是一個類型化的多維數組。
####計算圖(graph)
TensorFlow 程序通常被組織成一個構建階段和一個執行階段。
在構建階段, op 的執行步驟 被描述成一個圖。
在執行階段, 使用會話(Session)執行執行圖中的 op。
例如, 通常在構建階段創建一個圖來表示和訓練神經網絡, 然后在執行階段反復執行圖中的訓練 op。
TensorFlow 支持 C, C++, Python 編程語言。目前, TensorFlow 的 Python 庫更加易用, 它提供了大量的輔助函數來簡化構建圖的工作, 這些函數尚未被 C 和 C++ 庫支持。
####構建圖
構建圖的第一步, 是創建源 op (source op)。源 op 不需要任何輸入, 例如 常量 (Constant). 源 op 的輸出被傳遞給其它 op 做運算。
Python 庫中, op 構造器的返回值代表被構造出的 op 的輸出, 這些返回值可以傳遞給其它 op 構造器作為輸入。
TensorFlow Python 庫有一個默認圖 (default graph), op 構造器可以為其增加節點。這個默認圖對 許多程序來說已經足夠用了。
####在一個會話中啟動圖
構造階段完成后, 才能啟動圖。啟動圖的第一步是創建一個 Session 對象, 如果無任何創建參數, 會話構造器將啟動默認圖。
Session 對象在使用完后需要關閉以釋放資源。 除了顯式調用 close 外, 也可以使用 “with” 代碼塊 來自動完成關閉動作。
在實現上, TensorFlow 將圖形定義轉換成分布式執行的操作, 以充分利用可用的計算資源(如 CPU 或 GPU)。 一般你不需要顯式指定使用 CPU 還是 GPU, TensorFlow 能自動檢測。 如果檢測到 GPU, TensorFlow 會盡可能地利用找到的第一個 GPU 來執行操作。
如果機器上有超過一個可用的 GPU, 除第一個外的其它 GPU 默認是不參與計算的。 為了讓 TensorFlow 使用這些 GPU, 你必須將 op 明確指派給它們執行。 with…Device 語句用來指派特定的 CPU 或 GPU 執行操作。
####實踐項目
MNIST是機器學習中的“hello,world”。該項目分為一下幾個步驟
數據集的下載(不要解壓),網址為:http://yann.lecun.com/exdb/mnist/,下載后在運行代碼目錄下新建MNIST_data文件夾,把上述四個文件放入該文件夾下。
train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)
編寫自動安裝和下載MNIST數據集的python代碼,代碼寫入文件input_data.py中,該文件和運行代碼同一個目錄,input_data.py文件內容編碼為utf-8。(代碼部分在下面顯示)
編寫運行代碼文件Test_1.py。
兩個.py文件加一個文件夾如下圖所示,
MNIST_data文件夾下有四個下載的數據文件;
input_data.py是自動下載和安裝的MNIST數據集的python代碼;
Test_1.py是運行代碼;
其中input_data.py代碼如下:
其中Test_1.py代碼如下:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import input_data import tensorflow as tfmnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #x不是一個特定的值,而是一個占位符placeholder,我們在TensorFlow運行計算時輸入這個值。 #我們希望能夠輸入任意數量的MNIST圖像,每一張圖展平成784維的向量。 #我們用2維的浮點數張量來表示這些圖,這個張量的形狀是[None,784 ]。 #(這里的None表示此張量的第一個維度可以是任何長度的。) x = tf.placeholder(tf.float32, [None, 784]) #權重值,初始值全為0 W = tf.Variable(tf.zeros([784,10])) #偏置量,初始值全為0 b = tf.Variable(tf.zeros([10]))#建立模型,y是匹配的概率 #tf.matmul(x,W)表示x乘以W #y是預測,y_是實際 y = tf.nn.softmax(tf.matmul(x,W) + b)#為計算交叉熵,添加的placeholder y_ = tf.placeholder("float", [None,10]) #交叉熵 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #用梯度下降算法(gradient descent algorithm)以0.01的學習速率最小化交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)#初始化我們創建的變量 init = tf.global_variables_initializer()#在Session里面啟動模型 sess = tf.Session() sess.run(init)#訓練模型 #循環的每個步驟中,都會隨機抓取訓練數據中的100個批處理數據點,然后用這些數據點作為參數替換之前的占位符來運行train_step #即:使用的隨機梯度下降訓練方法 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})#-------------------模型評估---------------------- #判斷預測標簽和實際標簽是否匹配 #tf.argmax 找出某個tensor對象在某一維上的其數據最大值所在的索引值 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}))運行結果顯示,每次運行的結果可能不一樣。
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 2018-08-15 19:42:54.657354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.657354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.657354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.657354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.657354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.658354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.658354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.658354: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. 2018-08-15 19:42:54.753360: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:887] Found device 0 with properties: name: GeForce GT 630 major: 3 minor: 0 memoryClockRate (GHz) 0.8755 pciBusID 0000:01:00.0 Total memory: 2.00GiB Free memory: 1.72GiB 2018-08-15 19:42:54.754360: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:908] DMA: 0 2018-08-15 19:42:54.754360: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:918] 0: Y 2018-08-15 19:42:54.754360: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GT 630, pci bus id: 0000:01:00.0) 0.9117Process finished with exit code 0 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的TensorFlow的基本介绍及Hello,world的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django中间件与python日志模块
- 下一篇: MFC实现图像灰度、采样和量化功能详解