那曲檬骨新材料有限公司

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 搭建神經網絡。

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

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

    關注

    1796

    文章

    47666

    瀏覽量

    240285
  • 機器學習
    +關注

    關注

    66

    文章

    8438

    瀏覽量

    133084
  • 深度學習
    +關注

    關注

    73

    文章

    5513

    瀏覽量

    121549
收藏 人收藏

    評論

    相關推薦

    NPU在深度學習中的應用

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

    GPU深度學習應用案例

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

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

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

    快速部署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 ?704次閱讀

    TensorFlow是什么?TensorFlow怎么用?

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

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

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

    tensorflow和pytorch哪個更簡單?

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

    tensorflow和pytorch哪個好

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

    tensorflow簡單的模型訓練

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

    keras模型轉tensorflow session

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

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

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

    TensorFlow的定義和使用方法

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

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

    深度學習作為人工智能領域的一個重要分支,在過去十年中取得了顯著的進展。在構建和訓練深度學習模型的過程中,深度
    的頭像 發表于 07-02 14:04 ?1075次閱讀

    FPGA在深度學習應用中或將取代GPU

    筆記本電腦或機架式服務器上訓練神經網絡時,這不是什么大問題。但是,許多部署深度學習模型的環境對 GPU 并不友好,比如自動駕駛汽車、工廠、機器人和許多智慧城市環境,在這些環境中硬件必須忍受熱、灰塵、濕度
    發表于 03-21 15:19
    威尼斯人娱乐场棋牌| 澳门百家乐官网视频| 六合彩网| bet365在线体育投注| 德州扑克 单机| 威尼斯人娱乐城新闻| 线上百家乐怎么玩| 二八杠游戏平台| 总统百家乐的玩法技巧和规则| 百家乐代理打| 百家乐规律和方法| 威尼斯人娱乐城 老品牌值得信赖| 百家乐网哪一家做的最好呀| 菲利宾太阳城娱乐网| 大发888下载地址| 百家乐新注册送彩金| 百家乐金海岸| 百家乐那里信誉好| 大发888开户xa11| E世博网址| 黑龙江省| 百家乐官网五湖四海娱乐平台 | 百家乐官网赌机厂家| 网络百家乐官网会输钱的多吗| 百家乐一般的庄闲比例是多少| 百家乐vshow| 香港六合彩的开奖结果| 四会市| 百家乐官网英皇娱乐城| 百家乐官网缩水软件| 百家乐博彩开户博彩通| 环球百家乐的玩法技巧和规则| 大发888手机版下载安装| 民丰县| 赌博百家乐官网的玩法技巧和规则| 玩百家乐官网都是什么人| 神人百家乐赌博| 大发扑克网址| 百家乐官网是否有规律| 黄大仙区| 娱乐网百家乐官网的玩法技巧和规则 |