當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.compat.v1.InteractiveSession用法及代碼示例

TensorFlow Session 用於交互式上下文,例如 shell。

用法

tf.compat.v1.InteractiveSession(
    target='', graph=None, config=None
)

參數

  • target (可選。)要連接的執行引擎。默認使用in-process 引擎。
  • graph (可選。)要啟動的Graph(如上所述)。
  • config (可選)ConfigProto proto 用於配置會話。

屬性

  • graph 在此會話中啟動的圖表。
  • graph_def 底層 TensorFlow 圖的可序列化版本。
  • sess_str 此會話將連接到的 TensorFlow 進程。

與常規Session 的唯一區別是InteractiveSession 將自身安裝為構造時的默認會話。 tf.Tensor.evaltf.Operation.run 方法將使用該會話來運行操作。

這在交互式 shell 和 IPython 筆記本中很方便,因為它避免了必須傳遞顯式 Session 對象來運行操作。

例如:

sess = tf.compat.v1.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print(c.eval())
sess.close()

請注意,在with 語句中創建常規會話時,它會將自身安裝為默認會話。非交互式程序中的常見用法是遵循該模式:

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.compat.v1.Session():
  # We can also use 'c.eval()' here.
  print(c.eval())

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.InteractiveSession。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。