TensorFlow2 0基礎知識

2021-10-03 23:56:52 字數 4894 閱讀 6059

三、常量與變數

四、在2.0中實現1.x的靜態圖執行模式

注:本文語法都是基於tensorflow2.0的

import tensorflow as tf      #  匯入

print

("tensorflow版本是:"

,tf.__verson__)

# 顯示當前版本

tensorflow版本是:2.0.0

node1 = tf.constant([[

3.0,

1.5],[

2.5,

6.0]

],tf.float32)

node2 = tf.constant([[

4.0,

1.0],[

5.0,

2.5]

],tf.float32)

node3 = tf.add(node1,node2)

node3 # 輸出的是乙個tensor

print

(node3.numpy())

#輸出運算結果tensor的值

[[7. 2.5]

[7.5 8.5]]

1.概念

2.屬性 形狀

階形狀維數例子0

()0-d41

(d0)

1-d[2,3,5]

2(d0,d1)

2-d[[2,3],[3,4]]

3(d0,d1,d2)

3-d[[[7], [[3]]],[[[2]],[[4]]]]

n(d0,d1,…,dn-1)

n-d形為(d0,d1,…,dn-1)的張量

scalar = tf. constant (

100)

vector = tf. constant([1

,2,3,4,5

])matrix = tf. constant([1

,2,3

],[4

,5,6

]])cube_matrix = tf. constant([[

1],[

2],[

3],[

[4],

[5],

[6]]

,[[7

],[8

],[9

]]])

print

(scalar.shape)

# 輸出()

print

(vector.shape)

# 輸出(5,)

print

(matrix.shape)

# 輸出(2,3)

print

(cube_matrix. get_shape(

)# 輸出(3, 3, 1)

()

(5,)

(2, 3)

(3,3,1)

型別

a = tf. constant([1

,2])

b = tf. constant(

[2.0

,3.0])

result = tf. add(a, b)

#執行報錯

在tensorflow中可以通過tf.cast()進行資料型別轉換

a = tf. constant([1

,2])

b = tf. constant(

[2.0

,3.0])

a = tf. cast(a, tf. float32)

#資料型別轉換

result = tf. add(a, b)

result

a = tf.constant([1

,2,3

,4,5

,6],tf.float32, shape=[2

,3])

a

a會自動的對應dtype值進行型別轉換,對應shape的值進行reshape工作

v1 = tf. variable(

[1, 2])

v2 = tf. variable(

[3, 4

], tf. float32)

v1, v2

(,

)

型別是根據初始值定義的

還可以直接利用張量賦初值

c = tf. constant (1) 

v = tf. variable(c)

c, v

(,

)

在tensorflow中變數和普通程式語言中的變數有著較大區別

tensorflow中的變數是一種特殊的設計,是可以被機器優化過程中自動改變值的張量,也可以理解為待優化的張量。

在tensorflow中變數建立(賦初值)後,一般無需人工進行賦值,系統會根據演算法模型,在訓練優化過程中自動調整變數的值。

在變數的引數中,trainable引數用來表徵當前變數是否需要被自動優化,建立變數物件時預設是啟用自動優化標誌。但是有時候(如遷移學習)對某些已經訓練好的權值,我們會將其凍結。

如果硬要人工更新變數值,可使用assign()實現

v = tf. variable(5)

v. assign(v+1)

v

還有加法和減法對應的assign_add()、assign_sub()

v1 = tf. variable(5)

v2 = tf. variable(5)

v1.assign_add (1)

v2.assign_sub(1)

v1, v2

(,

構建階段:建立乙個「計算圖」,通過圖的模式來定義資料與操作的執行步驟

執行階段:建立乙個會話,使用會話物件來實現計算圖的執行

#乙個簡單計算圖

node1 = tf . constant(

3.0, tf . float32, name=

" node1"

)node2 = tf . constant(

4.0, tf . float32 , name=

" node2"

)node3 = tf . add(node1, node2 )

print

(node3)

tensor(「add:0」, shape=(), dtype=float32)

輸出結果不是乙個具體的數字,而是乙個張量的結構,如圖:

tensorflow 2中執行或者開發tensorflow 1.x**,可以做如下處理:

匯入tensorflow時使用import tensorflow.compat.v1 as tf代替import tensorflow as tf;

執行tf.disable_ eager _execution()禁用tensorflow 2預設的即時執行模式。

#在tensorflow 2下執行tensorflow 1. x版本**

import tensorf low. compat. v1 as tf

tf. disable_ eager_ execution(

)# 改為圖執行模式執行

#定義了乙個簡單的「計算圖

nodel = tf. constant(

3.0, tf. float32, name=

" node1"

)node2 = tf. constant(

4.0, tf. float32, name=

" node2"

)node3 = tf. add (node1, node2)

print

(node3)

tensor(「add:0」,shape=(), dtype=float32)

若要執行,則

sess = tf. session(

)#建立對話並顯示執行結果(分配資源)

print

("執行sess. run (node1)的結果:",sess. run (node1)

)print

("執行sess. run (node2)的結果:",sess. run (node2)

)print

("執行sess. run (node3)的結果:",sess. run (node3)

)sess. close(

)#關閉session

執行sess. run (node1)的結果: 3.0

執行sess. run (node2)的結果: 4.0

執行sess. run (node3)的結果: 7.0

tensorflow2 0視訊記憶體設定

遇到乙個問題 新買顯示卡視訊記憶體8g但是tensorflow執行的時候介面顯示只有約6.3g的視訊記憶體可用,如下圖 即限制了我的視訊記憶體,具體原因為什麼我也不知道,但原來的視訊記憶體小一些的顯示卡就沒有這個問題。目前的解決辦法是 官方文件解決 然後對應的中文部落格 總結一下,就是下面的兩個辦法...

Tensorflow2 0 啟用函式

常用啟用函式及對應特點 神經網路結構的輸出為所有輸入的加權和,這導致整個神經網路是乙個線性模型。而線性模型不能解決異或問題,且面對多分類問題,也顯得束手無策。所以為了解決非線性的分類或回歸問題,啟用函式必須是非線性函式。神經網路中啟用函式的主要作用是提供網路的非線性建模能力。這是因為反向傳播演算法就...

初步了解TensorFlow2 0

為什麼要學習tensorflow?深度學習能夠更好地抽取資料中的規律,從而給公司帶來更大的價值 tensorflow是強大且靈活的開源框架 使用廣泛 2.0更加強大 易用 成熟 tensorflow是什麼?是google的開源軟體庫 採用資料流圖,用於數值計算 支援多平台 gpu cpu 移動裝置 ...