強化學習筆記2-Python/OpenAI/TensorFlow/ROS-程序指令

強化學習筆記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 


作者:zhangrelay
來源:CSDN
原文:https://blog.csdn.net/ZhangRelay/article/details/91414600
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章