chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

【連載】深度學(xué)習(xí)筆記7:Tensorflow入門

人工智能實(shí)訓(xùn)營 ? 2018-08-24 18:31 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

從前面的學(xué)習(xí)筆記中,筆者和大家一起使用了 numpy 一步一步從感知機(jī)開始到兩層網(wǎng)絡(luò)以及最后實(shí)現(xiàn)了深度神經(jīng)網(wǎng)絡(luò)算法搭建。而后我們又討論了改善深度神經(jīng)網(wǎng)絡(luò)的基本方法,包括神經(jīng)網(wǎng)絡(luò)的正則化、參數(shù)優(yōu)化和調(diào)參等問題。這一切工作我們都是基于numpy 完成的,沒有調(diào)用任何深度學(xué)習(xí)框架。在學(xué)習(xí)深度學(xué)習(xí)的時候,一開始不讓大家直接上手框架可謂良苦用心,旨在讓大家能夠跟筆者一樣,一步一步通過 numpy 搭建神經(jīng)網(wǎng)絡(luò)的過程就是要讓你能夠更加深入的理解神經(jīng)網(wǎng)絡(luò)的架構(gòu)、基本原理和工作機(jī)制,而不是黑箱以視之。

但學(xué)習(xí)到這個階段,你已充分理解了神經(jīng)網(wǎng)絡(luò)的工作機(jī)制,馬上就要接觸更深層次的卷積神經(jīng)網(wǎng)絡(luò)(CNN)和遞歸神經(jīng)網(wǎng)絡(luò)(RNN),依靠純手工去搭建這些復(fù)雜的神經(jīng)網(wǎng)絡(luò)恐怕并不現(xiàn)實(shí)。這時候就該深度學(xué)習(xí)框架出場了。針對深度學(xué)習(xí),目前有很多優(yōu)秀的學(xué)習(xí)框架,比如說筆者馬上要講的 Tensorflow,微軟的 CNTK,伯克利視覺中心開發(fā)的 caffe,以及別具一格的 PyTorch 和友好易用的 keras,本系列深度學(xué)習(xí)筆記打算從 Tensorflow 開始,對三大主流易用的深度學(xué)習(xí)框架 Tensorflow、PyTorchkeras 進(jìn)行學(xué)習(xí)和講解。選擇這三個框架的原因在于其簡單易用、方便編程和運(yùn)行速度相對較快。

作為谷歌的深度學(xué)習(xí)框架, Tensorflow 在深度學(xué)習(xí)領(lǐng)域可謂風(fēng)頭無二。其中 Tensor 可以理解為類似于 numpy 的 N 維數(shù)組,名為張量; flow 則意味著 N 維數(shù)組的流計算,而 Tensor 的數(shù)據(jù)流計算形式則為一個計算圖的形式進(jìn)行計算。這里重點(diǎn)提一下,如果大學(xué)本科期間的線性代數(shù)忘記了的話,我勸你趕緊回去翻一翻,線性代數(shù)和矩陣論是深度學(xué)習(xí)的基礎(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(變量),之后對變量進(jìn)行初始化,創(chuàng)建計算會話,最后執(zhí)行會話計算并打印結(jié)果。所以我們可以看到運(yùn)行 Tensorflow 的基本機(jī)制:
創(chuàng)建一些尚未被執(zhí)行的張量——
定義這些張量之間的運(yùn)算操作——初始化這些張量——創(chuàng)建會話——執(zhí)行會話

需要注意的一點(diǎn)是,創(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)建了兩個 TensorTensor 之間的乘積運(yùn)算,但直接打印的結(jié)果卻不是我們想要看到的 20. 原因則在于這里我們沒有創(chuàng)建會話并執(zhí)行,只是打印了兩個張量運(yùn)算之后的張量。創(chuàng)建會話并執(zhí)行操作如下:

sess=tf.Session()
print(sess.run(c))
20

除了直接定義變量之外,我們還可以通過創(chuàng)建占位符變量來稍后為之賦值,然后在運(yùn)行會話中傳入一個 feed_dict ,示例如下:

x=tf.placeholder(tf.int64,name='x')
print(sess.run(2*x,feed_dict={x:3}))
sess.close()
6

相信你已經(jīng)大致明白了基于張量運(yùn)算的 Tensorflow 的底層運(yùn)行機(jī)制了??偨Y(jié)而言就是:創(chuàng)建張量、初始化張量、創(chuàng)建會話并執(zhí)行。

下面展示幾個 Tensorflow 的神經(jīng)網(wǎng)絡(luò)計算的基礎(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ù)

640?wx_fmt=png

def cost(logits, labels):  
""" Computes the cost using the sigmoid cross entropy Arguments: logits -- vector containing z, output of the last linear unit (before the final sigmoid activation) labels -- vector of labels y (1 or 0) Note: What we've been calling "z" and "y" in this class are respectively called "logits" and "labels" in the TensorFlow documentation. So logits will feed into z, and labels into y. Returns: cost -- runs the session of the cost (formula (2)) """ # Create the placeholders for "logits" (z) and "labels" (y) (approx. 2 lines) z = tf.placeholder(tf.float32, name='z') y = tf.placeholder(tf.float32, name='y') # Use the loss function (approx. 1 line) cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=z, labels=y) # Create a session (approx. 1 line). See method 1 above. sess = tf.Session() # Run the session (approx. 1 line). sess.run(cost, feed_dict={z: logits, y: labels}) # Close the session (approx. 1 line). See method 1 above. sess.close()
return cost
one hot 編碼

640?wx_fmt=png

def one_hot_matrix(labels, C):  
""" Creates a matrix where the i-th row corresponds to the ith class number and the jth column corresponds to the jth training example. So if example j had a label i. Then entry (i,j) will be 1. Arguments: labels -- vector containing the labels C -- number of classes, the depth of the one hot dimension Returns: one_hot -- one hot matrix """ # Create a tf.constant equal to C (depth), name it 'C'. (approx. 1 line) C = tf.constant(C) # Use tf.one_hot, be careful with the axis (approx. 1 line) one_hot_matrix = tf.one_hot(labels, C, axis=0) # Create the session (approx. 1 line) sess = tf.Session() one_hot = sess.run(one_hot_matrix) # Close the session (approx. 1 line). See method 1 above. sess.close()
return 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)絡(luò)的一些基礎(chǔ)運(yùn)算利用 Tensorflow 定義好了。在下一期筆記中,我們將學(xué)習(xí)如何使用 Tensorflow 搭建神經(jīng)網(wǎng)絡(luò)。

本文由《自興動腦人工智能》項目部 凱文 投稿。


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 人工智能
    +關(guān)注

    關(guān)注

    1813

    文章

    49536

    瀏覽量

    259371
  • 機(jī)器學(xué)習(xí)

    關(guān)注

    66

    文章

    8532

    瀏覽量

    136020
  • 深度學(xué)習(xí)
    +關(guān)注

    關(guān)注

    73

    文章

    5587

    瀏覽量

    123764
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    學(xué)習(xí)物聯(lián)網(wǎng)怎么入門?

    景等。同時,學(xué)習(xí)物聯(lián)網(wǎng)的基本技術(shù),如傳感器技術(shù)、通信技術(shù)、云計算等,也是非常重要的。 其次,選擇適合自己的學(xué)習(xí)方式也是入門學(xué)習(xí)物聯(lián)網(wǎng)的重要一步。
    發(fā)表于 10-14 10:34

    用樹莓派搞深度學(xué)習(xí)?TensorFlow啟動!

    介紹本頁面將指導(dǎo)您在搭載64位Bullseye操作系統(tǒng)的RaspberryPi4上安裝TensorFlow。TensorFlow是一個專為深度學(xué)習(xí)開發(fā)的大型軟件庫,它消耗大量資源。您可
    的頭像 發(fā)表于 03-25 09:33 ?844次閱讀
    用樹莓派搞<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>?<b class='flag-5'>TensorFlow</b>啟動!

    IPC2221簡略學(xué)習(xí)筆記

    關(guān)于IPC2221的學(xué)習(xí)筆記。
    發(fā)表于 03-14 18:07 ?6次下載

    如何排除深度學(xué)習(xí)工作臺上量化OpenVINO?的特定層?

    無法確定如何排除要在深度學(xué)習(xí)工作臺上量化OpenVINO?特定層
    發(fā)表于 03-06 07:31

    軍事應(yīng)用中深度學(xué)習(xí)的挑戰(zhàn)與機(jī)遇

    人工智能尤其是深度學(xué)習(xí)技術(shù)的最新進(jìn)展,加速了不同應(yīng)用領(lǐng)域的創(chuàng)新與發(fā)展。深度學(xué)習(xí)技術(shù)的發(fā)展深刻影響了軍事發(fā)展趨勢,導(dǎo)致戰(zhàn)爭形式和模式發(fā)生重大變化。本文將概述
    的頭像 發(fā)表于 02-14 11:15 ?736次閱讀

    模電手賬筆記7

    模電手賬筆記7
    的頭像 發(fā)表于 02-13 16:31 ?490次閱讀
    模電手賬<b class='flag-5'>筆記</b>(<b class='flag-5'>7</b>)

    BP神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)的關(guān)系

    BP神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)之間存在著密切的關(guān)系,以下是對它們之間關(guān)系的介紹: 一、BP神經(jīng)網(wǎng)絡(luò)的基本概念 BP神經(jīng)網(wǎng)絡(luò),即反向傳播神經(jīng)網(wǎng)絡(luò)(Backpropagation Neural Network
    的頭像 發(fā)表于 02-12 15:15 ?1215次閱讀

    深度學(xué)習(xí)入門:簡單神經(jīng)網(wǎng)絡(luò)的構(gòu)建與實(shí)現(xiàn)

    深度學(xué)習(xí)中,神經(jīng)網(wǎng)絡(luò)是核心模型。今天我們用 Python 和 NumPy 構(gòu)建一個簡單的神經(jīng)網(wǎng)絡(luò)。 神經(jīng)網(wǎng)絡(luò)由多個神經(jīng)元組成,神經(jīng)元之間通過權(quán)重連接。我們構(gòu)建一個包含輸入層、隱藏層和輸出層的簡單
    的頭像 發(fā)表于 01-23 13:52 ?722次閱讀

    ES7P0214應(yīng)用筆記

    電子發(fā)燒友網(wǎng)站提供《ES7P0214應(yīng)用筆記.pdf》資料免費(fèi)下載
    發(fā)表于 01-16 16:06 ?0次下載
    ES<b class='flag-5'>7</b>P0214應(yīng)用<b class='flag-5'>筆記</b>

    ES7P0213應(yīng)用筆記

    電子發(fā)燒友網(wǎng)站提供《ES7P0213應(yīng)用筆記.pdf》資料免費(fèi)下載
    發(fā)表于 01-16 15:26 ?0次下載
    ES<b class='flag-5'>7</b>P0213應(yīng)用<b class='flag-5'>筆記</b>

    入門web安全筆記分享

    在計算機(jī)技術(shù)如日中天的今天,Web安全問題也接踵而來。但Web安全卻“入門簡單精通難”,涉及技術(shù)非常多且廣,學(xué)習(xí)阻力很大。 為此今天分享一份94頁的《Web Hacking 101》筆記,包含Web
    的頭像 發(fā)表于 12-03 17:04 ?835次閱讀
    <b class='flag-5'>入門</b>web安全<b class='flag-5'>筆記</b>分享

    NPU在深度學(xué)習(xí)中的應(yīng)用

    隨著人工智能技術(shù)的飛速發(fā)展,深度學(xué)習(xí)作為其核心驅(qū)動力之一,已經(jīng)在眾多領(lǐng)域展現(xiàn)出了巨大的潛力和價值。NPU(Neural Processing Unit,神經(jīng)網(wǎng)絡(luò)處理單元)是專門為深度學(xué)習(xí)
    的頭像 發(fā)表于 11-14 15:17 ?2613次閱讀

    Pytorch深度學(xué)習(xí)訓(xùn)練的方法

    掌握這 17 種方法,用最省力的方式,加速你的 Pytorch 深度學(xué)習(xí)訓(xùn)練。
    的頭像 發(fā)表于 10-28 14:05 ?919次閱讀
    Pytorch<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>訓(xùn)練的方法

    GPU深度學(xué)習(xí)應(yīng)用案例

    GPU在深度學(xué)習(xí)中的應(yīng)用廣泛且重要,以下是一些GPU深度學(xué)習(xí)應(yīng)用案例: 一、圖像識別 圖像識別是深度學(xué)習(xí)
    的頭像 發(fā)表于 10-27 11:13 ?1969次閱讀

    激光雷達(dá)技術(shù)的基于深度學(xué)習(xí)的進(jìn)步

    信息。這使得激光雷達(dá)在自動駕駛、無人機(jī)、機(jī)器人等領(lǐng)域具有廣泛的應(yīng)用前景。 二、深度學(xué)習(xí)技術(shù)的發(fā)展 深度學(xué)習(xí)是機(jī)器學(xué)習(xí)的一個分支,它通過模擬人
    的頭像 發(fā)表于 10-27 10:57 ?1352次閱讀