中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

谷歌發(fā)布AutoGraph,自動(dòng)將Python轉(zhuǎn)化為TF圖

2018-07-20    來源:raincent

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
近日,谷歌發(fā)布了一項(xiàng)新的 TensorFlow 工具「AutoGraph」,能將 print() 函數(shù)和其它 Python 代碼轉(zhuǎn)化為純 TensorFlow 計(jì)算圖代碼。這一工具極大地加強(qiáng)了 TensorFlow 在調(diào)用純 Python 語句時(shí)的性能,開發(fā)者也不需要再用 TensorFlow 改寫常用的 IF 和 While 等 Python 語句來提升運(yùn)行效率。

項(xiàng)目地址:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/autograph

一般而言,在寫 TensorFlow 代碼時(shí),我們需要構(gòu)建整個(gè)算法的計(jì)算圖,或者規(guī)劃所有數(shù)據(jù)流的計(jì)算過程,然后再投入數(shù)據(jù)并快速執(zhí)行整個(gè)或局部計(jì)算圖。當(dāng)然因?yàn)楫?dāng)前 PyTorch 和 Keras 等動(dòng)態(tài)計(jì)算圖的流行,TensorFlow 也發(fā)布了 Eager Execution,它可以幫助用戶自動(dòng)構(gòu)建計(jì)算圖。但一般的 TensorFlow 還是常使用靜態(tài)計(jì)算圖的方式,因?yàn)樗臉?gòu)建邏輯與部署都非常有優(yōu)勢(shì)。

然而對(duì)于入門開發(fā)者而言,理解靜態(tài)計(jì)算圖是比較困難的,因此很容易引起開發(fā)者的困惑。尤其是在一些涉及更復(fù)雜模型場(chǎng)景中,例如使用 if 和 while 等 Python 語句,或使用 print() 與接受結(jié)構(gòu)化輸入等,它們都會(huì)引起我們對(duì)計(jì)算圖的困惑。

所以為什么 TensorFlow 需要使用計(jì)算圖呢?計(jì)算圖允許各種各樣的優(yōu)化,例如移除公共的子表達(dá)式和內(nèi)核融合等。此外,計(jì)算圖簡(jiǎn)化了分布式訓(xùn)練和部署時(shí)的環(huán)境配置,因此它們可被視為一種獨(dú)立于平臺(tái)的模型計(jì)算形式。這一特性對(duì)于在多 GPU 或 TPU 上的分布式訓(xùn)練極其重要,當(dāng)然基于 TensorFlow Lite 在移動(dòng)端和 IoT 上部署模型也非常重要。

以下是一個(gè)非常簡(jiǎn)單的操作示例:

def huber_loss(a):
  if tf.abs(a) <= delta:
    loss = a * a / 2
  else:
    loss = delta * (tf.abs(a) - delta / 2)
  return loss

使用 Eager Execution,這只是「正確運(yùn)行」而已,但是此類操作可能會(huì)比較慢,因?yàn)?Python 解釋器眾所周知在實(shí)現(xiàn)地比較慢,且需要的計(jì)算比較復(fù)雜,這會(huì)令它錯(cuò)過許多程序優(yōu)化的機(jī)會(huì)。

為了給圖執(zhí)行做好準(zhǔn)備,你需要重寫代碼,使用 tf.cond() 等語句,但是這很繁瑣且難以實(shí)現(xiàn)。AutoGraph 可以自動(dòng)完成該轉(zhuǎn)換,保持 Eager 編程的簡(jiǎn)易性,同時(shí)還提升了計(jì)算圖執(zhí)行的性能。

在該示例中,我們可以使用 autograph.convert() 布置我們的函數(shù),AutoGraph 將自動(dòng)生成圖可用的代碼。

使用 AutoGraph,由于 decorator,下列代碼:

@autograph.convert()
def huber_loss(a):
  if tf.abs(a) <= delta:
    loss = a * a / 2
  else:
    loss = delta * (tf.abs(a) - delta / 2)
  return loss

在執(zhí)行時(shí)變成如下代碼:

def tf__huber_loss(a):
  with tf.name_scope('huber_loss'):

    def if_true():
      with tf.name_scope('if_true'):
        loss = a * a / 2
        return loss,

    def if_false():
      with tf.name_scope('if_false'):
        loss = delta * (tf.abs(a) - delta / 2)
        return loss,
    loss = ag__.utils.run_cond(tf.less_equal(tf.abs(a), delta), if_true,
        if_false)
    return loss

接下來,你可以調(diào)用你的代碼,就像使用一般的 TensorFlow op 一樣:

with tf.Graph().as_default():  
  x_tensor = tf.constant(9.0)

  # The converted function works like a regular op: tensors in, tensors out.
  huber_loss_tensor = huber_loss(x_tensor)

  with tf.Session() as sess:
    print('TensorFlow result: %2.2f\n' % sess.run(huber_loss_tensor))

如你所見,AutoGraph 連接起 Eager execution 和 Graph。AutoGraph 使用 Eager-style 的 Python 代碼,然后將其轉(zhuǎn)換成圖生成代碼。

AutoGraph 不只是有用宏命令的集合,它還可以使用源代碼轉(zhuǎn)換來覆寫 Python 語言的任意部分,包括控制流、函數(shù)應(yīng)用和分配,生成樣板代碼,重構(gòu)慣用 Python,以使轉(zhuǎn)換成圖的過程變得簡(jiǎn)單。

使用任意編譯器,都會(huì)對(duì)錯(cuò)誤信息可讀性產(chǎn)生擔(dān)憂;為此,AutoGraph 可以創(chuàng)建錯(cuò)誤信息,并堆疊揭示原始源代碼中錯(cuò)誤來源的多個(gè)軌跡,而不是僅僅顯示生成代碼的 reference。

可運(yùn)行示例

那么,AutoGraph 可以為我們做什么呢?以下有一些示例代碼,它們可以直接轉(zhuǎn)換為圖代碼而不需要任何的改寫。如果你想實(shí)際運(yùn)行這些操作,谷歌在這個(gè) GitHub 的 Colab 中提供了一個(gè) notebook 可供使用。

GitHub:https://github.com/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb

Colab:https://colab.research.google.com/github/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb

以下我們使用循環(huán)和分支來測(cè)試「科拉茲猜想」。注意,考慮到多樣性,我們將不使用 decorator,而使用 AutoGraph 的.to_graph() 函數(shù)將其轉(zhuǎn)換為圖。

def collatz(a):
    counter = 0
    while a != 1:
        if a % 2 == 0:
            a = a // 2
        else:
            a = 3 * a + 1
        counter = counter + 1
    return counter

graph_mode_collatz = autograph.to_graph(collatz)
# The code is human-readable, too
print(autograph.to_code(collatz))

collatz_tensor = graph_mode_collatz(tf.constant(n))

AutoGraph 可以支持任意的嵌套控制流,例如:

def f(n):
  if n >= 0:
    while n < 5:
      n += 1
      print(n)
  return n

AutoGraph 允許你在循環(huán)中添加元素到數(shù)組中。為了讓其工作,我們使用一些 AutoGraph 輔助工具,set_element_type 和 stack。

def f(n):
  z = []
  # We ask you to tell us the element dtype of the list
  autograph.set_element_type(z, tf.int32)
  for i in range(n):
    z.append(i)
  # when you're done with the list, stack it
  # (this is just like np.stack)
  return autograph.stack(z)

我們還支持 break、continue,甚至 print 和 assert 等語句。當(dāng)轉(zhuǎn)換完成后,這個(gè)片段的 Python assert 使用合適的 tf.Assert 將其轉(zhuǎn)換為 TensorFlow 計(jì)算圖。

def f(x):
  assert x != 0, 'Do not pass zero!'
  return x * x

具備輕易地添加循環(huán)、控制流等到圖上的能力意味著可以很容易將訓(xùn)練循環(huán)轉(zhuǎn)移到圖中?梢栽谶@個(gè) Colab 的 notebook 中找到一個(gè)示例,其中使用了一個(gè) RNN 訓(xùn)練循環(huán),并用一個(gè) sess.run() 調(diào)用來執(zhí)行它。當(dāng)你需要傳遞一個(gè)完整的訓(xùn)練循環(huán)到加速器時(shí),這很有用,比通過 CPU 控制器管理訓(xùn)練過程更好。

AutoGraph 打開了構(gòu)建和訓(xùn)練模型的新思路。谷歌在未來將基于開發(fā)者社區(qū)建議嘗試添加更多的功能到 AutoGraph 上,請(qǐng)?zhí)岢瞿愕慕ㄗh吧!

提建議:https://github.com/tensorflow/tensorflow/issues

Graph Performance 對(duì)比 Eager Execution

Eager Execution 相當(dāng)合用,但圖更快。盡管對(duì)比基準(zhǔn)較為復(fù)雜(由應(yīng)用以及硬件配置決定),但在一些簡(jiǎn)單示例中我們可以看到,當(dāng)從 Eager 轉(zhuǎn)換到 AutoGraph 代碼時(shí)有極大的加速,使用了大量 if 和 while 等語句。

最終,AutoGraph 讓你可以在 GPU 和 Cloud TPU 這樣的加速器硬件上使用動(dòng)態(tài)和流控制極嚴(yán)模型,這對(duì)在大量數(shù)據(jù)上訓(xùn)練大型模型非常有幫助。

AutoGraph 和 Eager Execution

雖然使用 Eager Execution,你也能通過 tf.contrib.eager.defun 對(duì)部分代碼根據(jù)計(jì)算圖執(zhí)行。但這需要你使用 tf.cond() 這樣計(jì)算圖類的 TensorFlow ops。未來,AutoGraph 將無縫與 defun 融合,讓你用簡(jiǎn)單的 eager-style Python 編寫圖代碼。當(dāng)成為現(xiàn)實(shí)時(shí),通過選擇性的把 eager 代碼轉(zhuǎn)換到圖分段,你就可以期待使用 AutoGraph 加速熱點(diǎn)了。

結(jié)論

AutoGraph 能夠讓你輕松的建立在 TensorFlow 圖中輕松運(yùn)行的直觀性、復(fù)雜模型。這是目前在 contrib 中運(yùn)行的實(shí)驗(yàn)性工具,但我們期望能夠盡快把它加入到 TensorFlow 核心。

原文鏈接:https://medium.com/tensorflow/autograph-converts-python-into-tensorflow-graphs-b2a871f87ec7

標(biāo)簽: Google 代碼 谷歌 開發(fā)者

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:完美謝幕后,世界杯大數(shù)據(jù)揭秘不眠的北上廣深!

下一篇:零基礎(chǔ)入門Python的秘密