在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

【連載】深度學習筆記7:Tensorflow入門

人工智能實訓營 ? 2018-08-20 12:47 ? 次閱讀

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

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

作為谷歌的深度學習框架, Tensorflow 在深度學習領域可謂風頭無二。其中 Tensor 可以理解為類似于 numpy 的 N 維數組,名為張量; flow 則意味著 N 維數組的流計算,而 Tensor 的數據流計算形式則為一個計算圖的形式進行計算。這里重點提一下,如果大學本科期間的線性代數忘記了的話,我勸你趕緊回去翻一翻,線性代數和矩陣論是深度學習的基礎,希望你能熟練掌握。

先看個簡單的例子。

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(變量),之后對變量進行初始化,創建計算會話,最后執行會話計算并打印結果。所以我們可以看到運行 Tensorflow的基本機制:
創建一些尚未被執行的張量——定義這些張量之間的運算操作——初始化這些張量——創建會話——執行會話

需要注意的一點是,創建會話后一定要執行這個會話,且看下面示例:

a=tf.constant(2) b=tf.constant(10) c=tf.multiply(a,b) print(c) Tensor("Mul:0",shape=(),dtype=int32)

在上面的示例中,我們創建了兩個 Tensor和 Tensor之間的乘積運算,但直接打印的結果卻不是我們想要看到的 20. 原因則在于這里我們沒有創建會話并執行,只是打印了兩個張量運算之后的張量。創建會話并執行操作如下:

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

20

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

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

6

相信你已經大致明白了基于張量運算的 Tensorflow的底層運行機制了。總結而言就是:創建張量、初始化張量、創建會話并執行。

下面展示幾個 Tensorflow的神經網絡計算的基礎函數示例。


線性函數

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函數

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

計算損失函數

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 編碼

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

參數初始化

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

一頓操作之后,我們已經將神經網絡的一些基礎運算利用 Tensorflow 定義好了。在下一期筆記中,我們將學習如何使用 Tensorflow 搭建神經網絡。

本文來自《自興動腦人工智能》項目部:凱文

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 人工智能
    +關注

    關注

    1803

    文章

    48406

    瀏覽量

    244646
  • 機器學習
    +關注

    關注

    66

    文章

    8478

    瀏覽量

    133810
  • 深度學習
    +關注

    關注

    73

    文章

    5546

    瀏覽量

    122278
收藏 人收藏

    評論

    相關推薦

    用樹莓派搞深度學習TensorFlow啟動!

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

    NPU在深度學習中的應用

    隨著人工智能技術的飛速發展,深度學習作為其核心驅動力之一,已經在眾多領域展現出了巨大的潛力和價值。NPU(Neural Processing Unit,神經網絡處理單元)是專門為深度學習
    的頭像 發表于 11-14 15:17 ?1525次閱讀

    GPU深度學習應用案例

    GPU在深度學習中的應用廣泛且重要,以下是一些GPU深度學習應用案例: 一、圖像識別 圖像識別是深度學習
    的頭像 發表于 10-27 11:13 ?866次閱讀

    AI大模型與深度學習的關系

    AI大模型與深度學習之間存在著密不可分的關系,它們互為促進,相輔相成。以下是對兩者關系的介紹: 一、深度學習是AI大模型的基礎 技術支撐 :深度
    的頭像 發表于 10-23 15:25 ?2390次閱讀

    快速部署Tensorflow和TFLITE模型在Jacinto7 Soc

    電子發燒友網站提供《快速部署Tensorflow和TFLITE模型在Jacinto7 Soc.pdf》資料免費下載
    發表于 09-27 11:41 ?0次下載
    快速部署<b class='flag-5'>Tensorflow</b>和TFLITE模型在Jacinto<b class='flag-5'>7</b> Soc

    如何在Tensorflow中實現反卷積

    TensorFlow中實現反卷積(也稱為轉置卷積或分數步長卷積)是一個涉及多個概念和步驟的過程。反卷積在深度學習領域,特別是在圖像分割、圖像超分辨率、以及生成模型(如生成對抗網絡GANs)等任務中
    的頭像 發表于 07-14 10:46 ?923次閱讀

    TensorFlow是什么?TensorFlow怎么用?

    TensorFlow是由Google開發的一個開源深度學習框架,它允許開發者方便地構建、訓練和部署各種復雜的機器學習模型。TensorFlow
    的頭像 發表于 07-12 16:38 ?1074次閱讀

    深度學習中的時間序列分類方法

    時間序列分類(Time Series Classification, TSC)是機器學習深度學習領域的重要任務之一,廣泛應用于人體活動識別、系統監測、金融預測、醫療診斷等多個領域。隨著深度
    的頭像 發表于 07-09 15:54 ?1706次閱讀

    tensorflow和pytorch哪個更簡單?

    TensorFlow和PyTorch都是用于深度學習和機器學習的開源框架。TensorFlow由Google Brain團隊開發,而Py
    的頭像 發表于 07-05 09:45 ?1249次閱讀

    tensorflow和pytorch哪個好

    :2015年由Google Brain團隊發布。 語言支持 :主要使用Python,也支持C++、Java等。 設計哲學 :TensorFlow是一個端到端的機器學習平臺,支持從研究到生產的所有階段
    的頭像 發表于 07-05 09:42 ?977次閱讀

    tensorflow簡單的模型訓練

    在本文中,我們將詳細介紹如何使用TensorFlow進行簡單的模型訓練。TensorFlow是一個開源的機器學習庫,廣泛用于各種機器學習任務,包括圖像識別、自然語言處理等。我們將從安裝
    的頭像 發表于 07-05 09:38 ?1082次閱讀

    keras模型轉tensorflow session

    和訓練深度學習模型。Keras是基于TensorFlow、Theano或CNTK等底層計算框架構建的。TensorFlow是一個開源的機器學習
    的頭像 發表于 07-05 09:36 ?733次閱讀

    如何使用Tensorflow保存或加載模型

    TensorFlow是一個廣泛使用的開源機器學習庫,它提供了豐富的API來構建和訓練各種深度學習模型。在模型訓練完成后,保存模型以便將來使用或部署是一項常見的需求。同樣,加載已保存的模
    的頭像 發表于 07-04 13:07 ?2303次閱讀

    TensorFlow的定義和使用方法

    TensorFlow是一個由谷歌人工智能團隊谷歌大腦(Google Brain)開發和維護的開源機器學習庫。它基于數據流編程(dataflow programming)的概念,將復雜的數學運算表示為
    的頭像 發表于 07-02 14:14 ?1200次閱讀

    TensorFlow與PyTorch深度學習框架的比較與選擇

    深度學習作為人工智能領域的一個重要分支,在過去十年中取得了顯著的進展。在構建和訓練深度學習模型的過程中,深度
    的頭像 發表于 07-02 14:04 ?1382次閱讀
    主站蜘蛛池模板: 欧美一区二区三区不卡免费观看 | 天堂中文在线免费观看 | 91md天美精东蜜桃传媒在线 | 很黄很暴力 很污秽的小说 很黄很黄叫声床戏免费视频 | 亚洲天堂h | 日本污全彩肉肉无遮挡彩色 | 福利视频一区二区微拍堂 | 精品亚洲欧美无人区乱码 | 另类free性欧美护士 | 老师今晚让你爽个够 | 六月丁香中文字幕 | 在线观看一级片 | 五月婷婷六月丁香在线 | 久久这里精品青草免费 | 色婷婷婷丁香亚洲综合不卡 | 极品吹潮视频大喷潮tv | www性| 欧美性色黄大片四虎影视 | 国产成人悠悠影院 | 久久精品隔壁老王影院 | 免费视频一区二区 | 一级毛片真人免费播放视频 | 国产精品久久久久天天影视 | 丁香六月 久久久 | 欧美综合视频 | 琪琪see色原网一区二区 | 国产大乳喷奶水在线看 | 成人欧美一区二区三区视频不卡 | 免费在线一区二区三区 | 天天色天天色 | 成人综合激情 | 日本与大黑人xxxx | 免费看色视频 | 国产精品a在线观看香蕉 | 天天做天天爱夜夜大爽完整 | 5151hh四虎国产精品 | 国产欧美在线一区二区三区 | 日韩精品一卡二卡三卡四卡2021 | 中文字幕一二三四区2021 | 欧美 亚洲 国产 精品有声 | 天堂网欧美|