強化學習筆記2-Python/OpenAI/TensorFlow/ROS-程序指令
TensorFlow
TensorFlow是Google的一個開源軟件庫,廣泛用于數值計算。它使用可在許多不同平臺上共享和執行的數據流圖。
它被廣泛用于構建深度學習模型,這是機器學習的一個子集。張量只不過是一個多維數組,所以當我們說TensorFlow時,它實際上是計算圖中的多維數組(張量)流。
安裝Anaconda后,安裝
TensorFlow變得非常簡單,直接安裝tensorflow也非常簡單。無論您使用何種平臺,都可以通過鍵入以下命令輕松安裝tensorflow。
conda install -c conda-forge tensorflow
或
pip install --user tensorflow
pip3 install --user tensorflow
如需GPU支持,需要-gpu。
運行以下hello world程序即可檢查成功的tensorflow安裝。
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
hello = tf.constant("Hello World")
sess = tf.Session()
print(sess.run(hello))
顯示結果如下:
b'Hello World'
變量Variables、常量Constants、占位符Placeholders
變量,常量,占位符是TensorFlow的基本要素。 但是,這三者之間總是存在混淆。 讓我們逐個看到每個元素,并了解它們之間的區別。
變量
變量是用于存儲值的容器。 變量將用作計算圖中其他幾個操作的輸入。 我們可以使用tf.Variable()函數創建tensorflow變量。 在下面的示例中,我們使用隨機正態分布中的值定義變量,并將其命名為權重。
weights = tf.Variable(tf.random_normal([8, 9], stddev=0.1), name="weights")
但是,在定義變量之后,我們需要使用tf.global_variables_initializer()方法顯式創建初始化操作,該方法將為變量分配資源。
常量
常量與變量不同,它們的值不能改變。
它們被分配了值,它們無法在整個過程中更改。 我們可以創建常量使用tf.constant()函數。
x = tf.constant(666)
占位符
將占位符視為一個變量,您只需定義類型和維度不分配價值。 占位符定義為沒有值。 占位符的值將在運行時提供。 占位符有一個名為shape的可選參數指定數據的維度。 如果形狀設置為none,那么我們可以提供任何數據運行時的大小。 可以使用tf.placeholder()函數定義占位符
x = tf.placeholder("float", shape=None)
簡單來說,我們使用tf.variable來存儲數據,使用tf.placeholder來提供外部數據。
計算圖(ROS中也有這個概念)Computation Graph
TensorFlow中的所有內容都將表示為由節點和邊組成的計算圖,其中節點是數學運算,例如加法,乘法等。邊是張量。 計算圖在優化資源方面非常有效,并且還促進了分布式計算。
假設我們有節點B,其輸入依賴于節點A的輸出,這種類型的依賴性稱為直接依賴:
A = tf.multiply(8,5)
B = tf.multiply(A,1)
當節點B不依賴于節點A進行輸入時,它被稱為間接依賴:
A = tf.multiply(8,5)
B = tf.multiply(4,3)
因此,如果我們能夠理解這些依賴關系,我們就可以在可用資源中分配獨立計算并減少計算時間。 每當我們導入tensorflow時,將自動生成默認圖形,并且我們創建的所有節點都將與默認圖形相關聯。
會話Sessions
只會定義計算圖,為了執行計算圖,我們使用tensorflow會話。 sess = tf.Session()我們可以使用tf.Session()方法為我們的計算圖創建會話,該方法將分配用于存儲變量當前值的內存。 創建會話后,我們可以使用sess.run()方法執行我們的圖形。 為了在tensorflow中運行任何東西,我們需要為一個實例啟動tensorflow會話,看下面的代碼:
import tensorflow as tf
a = tf.multiply(2,3)
print(a)
輸出:
Tensor("Mul_4:0", shape=(), dtype=int32)
它將打印tensorflow對象而不是6。因為如前所述,每當我們導入tensorflow時,將自動創建默認計算圖,并且我們創建的所有節點將附加到圖上。 為了執行圖形,我們需要初始化tensorflow會話,如下所示:
import tensorflow as tf
a = tf.multiply(2,3)
#create tensorflow session for executing the session
with tf.Session() as sess:
#run the session
print(sess.run(a))
輸出:
6
綜合到一個示例中:
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
hello = tf.constant("Hello World")
sess = tf.Session()
print(sess.run(hello))
a = tf.multiply(6,8)
print(a)
#create tensorflow session for executing the session
with tf.Session() as sess:
#run the session
print(sess.run(a))
b'Hello World'
Tensor("Mul:0", shape=(), dtype=int32)
48
TensorBoard
TensorBoard是tensorflow的可視化工具,可用于可視化計算圖。 它還可用于繪制各種中間計算的各種定量指標和結果。 使用TensorBoard,我們可以輕松地可視化復雜的模型,這對于調試和共享非常有用。 現在讓我們構建一個基本的計算圖并在tensorboard中可視化。
首先,讓我們導入庫:
import tensorflow as tf
接下來,我們初始化變量:
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
g = tf.add(c,f)
現在,我們將創建一個tensorflow會話,我們將使用tf.summary.FileWriter()將我們的圖形結果寫入稱為事件文件的文件:
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()
輸出:
26
要運行tensorboard,請轉到終端,找到工作目錄并鍵入:
tensorboard --logdir=logs --port=6003
添加范圍Adding Scope
范圍用于降低復雜性,并通過將相關節點分組在一起來幫助更好地理解模型。例如,在上面的示例中,我們可以將圖分解為兩個不同的組,稱為計算和結果。 如果你看一下前面的例子,我們可以看到節點,a到e執行計算,節點g計算結果。 因此,我們可以使用范圍單獨對它們進行分組以便于理解。 可以使用tf.name_scope()函數創建范圍。
with tf.name_scope("Computation"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
with tf.name_scope("Result"):
g = tf.add(c,f)
如果您看到計算范圍,我們可以進一步細分為單獨的部分,以便更好地理解。 假設我們可以創建作為第1部分的范圍,其具有節點a到c,范圍作為第2部分,其具有節點d到e,因為第1部分和第2部分彼此獨立。
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
通過在tensorboard中對它們進行可視化,可以更好地理解范圍。 完整代碼如下所示:
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
with tf.name_scope("Result"):
g = tf.add(c,f)
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()
全部示例如下:
import tensorflow as tf
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
with tf.name_scope("Result"):
g = tf.add(c,f)
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()
使用:tensorboard --logdir=logs --port=6003
在瀏覽器復制如下地址:TensorBoard 1.13.1 at
http://TPS2:6003
(Press CTRL+C to quit)
不同系統會有差異。
擴展閱讀:
OpenAI博客
TensorFlow官網
Github
Python TensorFlow 機器學習
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。