關(guān)于TensorFlow簡(jiǎn)單例子
在本文中,我們將看一些 TensorFlow 的例子,并從中感受到在定義張量和使用張量做數(shù)學(xué)計(jì)算方面有多么容易,我還會(huì)舉些別的機(jī)器學(xué)習(xí)相關(guān)的例子。
TensorFlow 是什么?
TensorFlow 是 Google 為了解決復(fù)雜的數(shù)學(xué)計(jì)算耗時(shí)過(guò)久的問(wèn)題而開(kāi)發(fā)的一個(gè)庫(kù)。
事實(shí)上,TensorFlow 能干許多事。比如:
- 求解復(fù)雜數(shù)學(xué)表達(dá)式
- 機(jī)器學(xué)習(xí)技術(shù)。你往其中輸入一組數(shù)據(jù)樣本用以訓(xùn)練,接著給出另一組數(shù)據(jù)樣本基于訓(xùn)練的數(shù)據(jù)而預(yù)測(cè)結(jié)果。這就是人工智能了!
- 支持 GPU 。你可以使用 GPU(圖像處理單元)替代 CPU 以更快的運(yùn)算。TensorFlow 有兩個(gè)版本: CPU 版本和 GPU 版本。
開(kāi)始寫(xiě)例子前,需要了解一些基本知識(shí)。
什么是張量?
張量是 TensorFlow 使用的主要的數(shù)據(jù)塊,它類(lèi)似于變量,TensorFlow 使用它來(lái)處理數(shù)據(jù)。張量擁有維度和類(lèi)型的屬性。
維度指張量的行和列數(shù),讀到后面你就知道了,我們可以定義一維張量、二維張量和三維張量。
類(lèi)型指張量元素的數(shù)據(jù)類(lèi)型。
定義一維張量
可以這樣來(lái)定義一個(gè)張量:創(chuàng)建一個(gè) NumPy 數(shù)組(LCTT 譯注:NumPy 系統(tǒng)是 Python 的一種開(kāi)源數(shù)字?jǐn)U展,包含一個(gè)強(qiáng)大的 N 維數(shù)組對(duì)象 Array,用來(lái)存儲(chǔ)和處理大型矩陣 )或者一個(gè) Python 列表 ,然后使用 tf_convert_to_tensor
函數(shù)將其轉(zhuǎn)化成張量。
可以像下面這樣,使用 NumPy 創(chuàng)建一個(gè)數(shù)組:
import numpy as np arr = np.array([1, 5.5, 3, 15, 20])
arr = np.array([1, 5.5, 3, 15, 20])
運(yùn)行結(jié)果顯示了這個(gè)數(shù)組的維度和形狀。
import numpy as np
arr = np.array([1, 5.5, 3, 15, 20])
print(arr)
print(arr.ndim)
print(arr.shape)
print(arr.dtype)
它和 Python 列表很像,但是在這里,元素之間沒(méi)有逗號(hào)。
現(xiàn)在使用 tf_convert_to_tensor
函數(shù)把這個(gè)數(shù)組轉(zhuǎn)化為張量。
import numpy as np
import tensorflow as tf
arr = np.array([1, 5.5, 3, 15, 20])
tensor = tf.convert_to_tensor(arr,tf.float64)
print(tensor)
這次的運(yùn)行結(jié)果顯示了張量具體的含義,但是不會(huì)展示出張量元素。
要想看到張量元素,需要像下面這樣,運(yùn)行一個(gè)會(huì)話(huà):
import numpy as np
import tensorflow as tf
arr = np.array([1, 5.5, 3, 15, 20])
tensor = tf.convert_to_tensor(arr,tf.float64)
sess = tf.Session()
print(sess.run(tensor))
print(sess.run(tensor[1]))
定義二維張量
定義二維張量,其方法和定義一維張量是一樣的,但要這樣來(lái)定義數(shù)組:
arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])
接著轉(zhuǎn)化為張量:
import numpy as np
import tensorflow as tf
arr = np.array([(1, 5.5, 3, 15, 20),(10, 20, 30, 40, 50), (60, 70, 80, 90, 100)])
tensor = tf.convert_to_tensor(arr)
sess = tf.Session()
print(sess.run(tensor))
現(xiàn)在你應(yīng)該知道怎么定義張量了,那么,怎么在張量之間跑數(shù)學(xué)運(yùn)算呢?
在張量上進(jìn)行數(shù)學(xué)運(yùn)算
假設(shè)我們有以下兩個(gè)數(shù)組:
arr1 = np.array([(1,2,3),(4,5,6)])
arr2 = np.array([(7,8,9),(10,11,12)])
利用 TenserFlow ,你能做許多數(shù)學(xué)運(yùn)算?,F(xiàn)在我們需要對(duì)這兩個(gè)數(shù)組求和。
使用加法函數(shù)來(lái)求和:
import numpy as np
import tensorflow as tf
arr1 = np.array([(1,2,3),(4,5,6)])
arr2 = np.array([(7,8,9),(10,11,12)])
arr3 = tf.add(arr1,arr2)
sess = tf.Session()
tensor = sess.run(arr3)
print(tensor)
也可以把數(shù)組相乘:
import numpy as np
import tensorflow as tf
arr1 = np.array([(1,2,3),(4,5,6)])
arr2 = np.array([(7,8,9),(10,11,12)])
arr3 = tf.multiply(arr1,arr2)
sess = tf.Session()
tensor = sess.run(arr3)
print(tensor)
現(xiàn)在你知道了吧。
三維張量
我們已經(jīng)知道了怎么使用一維張量和二維張量,現(xiàn)在,來(lái)看一下三維張量吧,不過(guò)這次我們不用數(shù)字了,而是用一張 RGB 圖片。在這張圖片上,每一塊像素都由 x、y、z 組合表示。
這些組合形成了圖片的寬度、高度以及顏色深度。
首先使用 matplotlib 庫(kù)導(dǎo)入一張圖片。如果你的系統(tǒng)中沒(méi)有 matplotlib ,可以 使用 pip來(lái)安裝它。
將圖片放在 Python 文件的同一目錄下,接著使用 matplotlib 導(dǎo)入圖片:
import matplotlib.image as img
myfile = "likegeeks.png"
myimage = img.imread(myfile)
print(myimage.ndim)
print(myimage.shape)
從運(yùn)行結(jié)果中,你應(yīng)該能看到,這張三維圖片的寬為 150 、高為 150 、顏色深度為 3 。
你還可以查看這張圖片:
import matplotlib.image as img
import matplotlib.pyplot as plot
myfile = "likegeeks.png"
myimage = img.imread(myfile)
plot.imshow(myimage)
plot.show()
真酷!
那怎么使用 TensorFlow 處理圖片呢?超級(jí)容易。
使用 TensorFlow 生成或裁剪圖片
首先,向一個(gè)占位符賦值:
myimage = tf.placeholder("int32",[None,None,3])
使用裁剪操作來(lái)裁剪圖像:
cropped = tf.slice(myimage,[10,0,0],[16,-1,-1])
***,運(yùn)行這個(gè)會(huì)話(huà):
result = sess.run(cropped, feed\_dict={slice: myimage})
然后,你就能看到使用 matplotlib 處理過(guò)的圖像了。
這是整段代碼:
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plot
myfile = "likegeeks.png"
myimage = img.imread(myfile)
slice = tf.placeholder("int32",[None,None,3])
cropped = tf.slice(myimage,[10,0,0],[16,-1,-1])
sess = tf.Session()
result = sess.run(cropped, feed_dict={slice: myimage})
plot.imshow(result)
plot.show()
是不是很神奇?
使用 TensorFlow 改變圖像
在本例中,我們會(huì)使用 TensorFlow 做一下簡(jiǎn)單的轉(zhuǎn)換。
首先,指定待處理的圖像,并初始化 TensorFlow 變量值:
myfile = "likegeeks.png"
myimage = img.imread(myfile)
image = tf.Variable(myimage,name='image')
vars = tf.global_variables_initializer()
然后調(diào)用 transpose 函數(shù)轉(zhuǎn)換,這個(gè)函數(shù)用來(lái)翻轉(zhuǎn)輸入網(wǎng)格的 0 軸和 1 軸。
sess = tf.Session()
flipped = tf.transpose(image, perm=[1,0,2])
sess.run(vars)
result=sess.run(flipped)
接著你就能看到使用 matplotlib 處理過(guò)的圖像了。
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plot
myfile = "likegeeks.png"
myimage = img.imread(myfile)
image = tf.Variable(myimage,name='image')
vars = tf.global_variables_initializer()
sess = tf.Session()
flipped = tf.transpose(image, perm=[1,0,2])
sess.run(vars)
result=sess.run(flipped)
plot.imshow(result)
plot.show()
以上例子都向你表明了使用 TensorFlow 有多么容易。