從前面的學習筆記中,和大家一起使用了 numpy 一步一步從感知機開始到兩層網(wǎng)絡以及最后實現(xiàn)了深度神經(jīng)網(wǎng)絡的算法搭建。而后我們又討論了改善深度神經(jīng)網(wǎng)絡的基本方法,包括神經(jīng)網(wǎng)絡的正則化、參數(shù)優(yōu)化和調(diào)參等問題。這一切工作我們都是基于numpy 完成的,沒有調(diào)用任何深度學習框架。在學習深度學習的時候,一開始不讓大家直接上手框架可謂良苦用心,旨在讓大家能夠跟筆者一樣,一步一步通過 numpy 搭建神經(jīng)網(wǎng)絡的過程就是要讓你能夠更加深入的理解神經(jīng)網(wǎng)絡的架構(gòu)、基本原理和工作機制,而不是黑箱以視之。
但學習到這個階段,你已充分理解了神經(jīng)網(wǎng)絡的工作機制,馬上就要接觸更深層次的卷積神經(jīng)網(wǎng)絡(CNN)和遞歸神經(jīng)網(wǎng)絡(RNN),依靠純手工去搭建這些復雜的神經(jīng)網(wǎng)絡恐怕并不現(xiàn)實。這時候就該深度學習框架出場了。針對深度學習,目前有很多優(yōu)秀的學習框架,比如說馬上要講的 Tensorflow,微軟的 CNTK,伯克利視覺中心開發(fā)的 caffe,以及別具一格的 PyTorch 和友好易用的 keras,本系列深度學習筆記打算從 Tensorflow 開始,對三大主流易用的深度學習框架 Tensorflow、PyTorch 和 keras 進行學習和講解。選擇這三個框架的原因在于其簡單易用、方便編程和運行速度相對較快。
作為谷歌的深度學習框架, Tensorflow 在深度學習領(lǐng)域可謂風頭無二。其中 Tensor 可以理解為類似于 numpy 的 N 維數(shù)組,名為張量; flow 則意味著 N 維數(shù)組的流計算,而 Tensor 的數(shù)據(jù)流計算形式則為一個計算圖的形式進行計算。這里重點提一下,如果大學本科期間的線性代數(shù)忘記了的話,我勸你趕緊回去翻一翻,線性代數(shù)和矩陣論是深度學習的基礎(chǔ),希望你能熟練掌握。
先看個簡單的例子。
importtensorflowastf#Definey_hatconstant.Setto36.y_hat=tf.constant(36,name='y_hat')#Definey.Setto39y=tf.constant(39,name='y')#Createavariableforthelossloss=tf.Variable((y-y_hat)**2,name='loss')#Wheninitisrunlater(session.run(init)),thelossvariablewillbeinitializedandreadytobecomputedinit=tf.global_variables_initializer()#Createasessionandprinttheoutputwithtf.Session()assession: #Initializesthevariables session.run(init) #Printstheloss print(session.run(loss))9
在上述代碼中,我們首先定義了兩個常量,然后定義了一個 loss Tensor(變量),之后對變量進行初始化,創(chuàng)建計算會話,最后執(zhí)行會話計算并打印結(jié)果。所以我們可以看到運行 Tensorflow的基本機制:
創(chuàng)建一些尚未被執(zhí)行的張量——定義這些張量之間的運算操作——初始化這些張量——創(chuàng)建會話——執(zhí)行會話
需要注意的一點是,創(chuàng)建會話后一定要執(zhí)行這個會話,且看下面示例:
a=tf.constant(2) b=tf.constant(10) c=tf.multiply(a,b) print(c) Tensor("Mul:0",shape=(),dtype=int32)在上面的示例中,我們創(chuàng)建了兩個 Tensor和 Tensor之間的乘積運算,但直接打印的結(jié)果卻不是我們想要看到的 20. 原因則在于這里我們沒有創(chuàng)建會話并執(zhí)行,只是打印了兩個張量運算之后的張量。創(chuàng)建會話并執(zhí)行操作如下:
sess=tf.Session() print(sess.run(c))20
除了直接定義變量之外,我們還可以通過創(chuàng)建占位符變量來稍后為之賦值,然后在運行會話中傳入一個 feed_dict,示例如下:
x=tf.placeholder(tf.int64,name='x') print(sess.run(2*x,feed_dict={x:3})) sess.close()6
相信你已經(jīng)大致明白了基于張量運算的 Tensorflow的底層運行機制了??偨Y(jié)而言就是:創(chuàng)建張量、初始化張量、創(chuàng)建會話并執(zhí)行。
下面展示幾個 Tensorflow的神經(jīng)網(wǎng)絡計算的基礎(chǔ)函數(shù)示例。
線性函數(shù)
def linear_function(): """ Implements a linear function: Initializes W to be a random tensor of shape (4,3) Initializes X to be a random tensor of shape (3,1) Initializes b to be a random tensor of shape (4,1) Returns: result -- runs the session for Y = WX + b """ np.random.seed(1) X = tf.constant(np.random.randn(3,1), name='X') W = tf.constant(np.random.randn(4,3), name='W') b = tf.constant(np.random.randn(4,1), name='b') Y = tf.add(tf.matmul(W, X), b) # Create the session using tf.Session() and run it with sess.run(...) on the variable you want to calculate init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) result = sess.run(Y) # close the session sess.close() return result計算sigmoid函數(shù)
def sigmoid(z): """ Computes the sigmoid of z Arguments: z -- input value, scalar or vector Returns: results -- the sigmoid of z """ x = tf.placeholder(tf.float32, name='x') sigmoid = tf.sigmoid(x) with tf.Session() as sess: result = sess.run(sigmoid, feed_dict={x: z}) return result計算損失函數(shù)
one hot 編碼
參數(shù)初始化
def ones(shape): """ Creates an array of ones of dimension shape Arguments: shape -- shape of the array you want to create Returns: ones -- array containing only ones """ # Create "ones" tensor using tf.ones(...). (approx. 1 line) ones = tf.ones(shape) # Create the session (approx. 1 line) sess = tf.Session() # Run the session to compute 'ones' (approx. 1 line) ones = sess.run(ones) # Close the session (approx. 1 line). See method 1 above. sess.close() return ones一頓操作之后,我們已經(jīng)將神經(jīng)網(wǎng)絡的一些基礎(chǔ)運算利用 Tensorflow 定義好了。在下一期筆記中,我們將學習如何使用 Tensorflow 搭建神經(jīng)網(wǎng)絡。
本文來自《自興動腦人工智能》項目部:凱文
-
人工智能
+關(guān)注
關(guān)注
1796文章
47791瀏覽量
240584 -
機器學習
+關(guān)注
關(guān)注
66文章
8446瀏覽量
133127 -
深度學習
+關(guān)注
關(guān)注
73文章
5518瀏覽量
121612
發(fā)布評論請先 登錄
相關(guān)推薦
NPU在深度學習中的應用
AI大模型與深度學習的關(guān)系
快速部署Tensorflow和TFLITE模型在Jacinto7 Soc
![快速部署<b class='flag-5'>Tensorflow</b>和TFLITE模型在Jacinto<b class='flag-5'>7</b> Soc](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論