1. 程式人生 > >Tensorflow 的動態機制Eager Execution

Tensorflow 的動態機制Eager Execution

從tensorflow的1.5版起,tensorflow也開始啟用動態機制Eager Execution了,它支援大部分tensorflow運作和gpu加速

Eager Execution是一個很靈活的機器學習平臺,可以提供給(An intuitive interface,Easier debugging,Natural control flow)

下面幾行程式碼可以很容易的幫我們入門使用Eager Execution,使用Eager Execution可以很及時的返回運算結果:

>>> import tensorflow as tf

>>> import tensorflow.contrib.eager as tfe

>>> tfe.enable_eager_execution()

>>> x = [[2.]]

>>> m = tf.matmul(x, x)

>>> print(m)

tf.Tensor([[ 4.]], shape=(1, 1), dtype=float32)

而以往不使用Eager Execution的結果便是:

>>> x = [[2.]]

>>> m = tf.matmul(x, x)

>>> print(m)

Tensor("MatMul:0", shape=(1, 1), dtype=float32)

需要在一個session裡執行:

>>> import tensorflow as tf

>>> x = [[2.]]

>>> m = tf.matmul(x, x)

>>> print(m)

Tensor("MatMul:0", shape=(1, 1), dtype=float32)

>>> sess=tf.Session()

>>> print(sess.run(m))

[[ 4.]]

>>> import tensorflow as tf

>>> x = [[2.]]

>>> m = tf.matmul(x, x)

>>> print(m)

Tensor("MatMul:0", shape=(1, 1), dtype=float32)

>>> sess=tf.Session()

>>> print(sess.run(m))

[[ 4.]]

簡單的Eager Execution初步嘗試