用法
as_default()
返回
- 用於將此圖用作默認圖的上下文管理器。
返回使此 Graph
成為默認圖的上下文管理器。
如果要在同一進程中創建多個圖形,則應使用此方法。為方便起見,提供了一個全局默認圖,如果您不顯式創建新圖,所有操作都將添加到該圖中。
將此方法與 with
關鍵字一起使用以指定應將在塊範圍內創建的操作添加到此圖中。在這種情況下,一旦退出with
的範圍,之前的默認圖再次設置為默認值。有一個堆棧,因此可以有多個嵌套級別的as_default
調用。
默認圖是當前線程的屬性。如果您創建一個新線程,並希望在該線程中使用默認圖形,則必須在該線程的函數中顯式添加 with g.as_default():
。
以下代碼示例是等效的:
# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
c = tf.constant(5.0)
assert c.graph is g
# 2. Constructing and making default:
with tf.Graph().as_default() as g:
c = tf.constant(5.0)
assert c.graph is g
如果啟用了即刻執行,則在此上下文管理器下創建的操作將被添加到圖中,而不是即刻執行。
相關用法
- Python tf.Graph.control_dependencies用法及代碼示例
- Python tf.Graph.container用法及代碼示例
- Python tf.Graph.device用法及代碼示例
- Python tf.Graph.get_name_scope用法及代碼示例
- Python tf.Graph.gradient_override_map用法及代碼示例
- Python tf.Graph.name_scope用法及代碼示例
- Python tf.Graph.colocate_with用法及代碼示例
- Python tf.Graph用法及代碼示例
- Python tf.GradientTape用法及代碼示例
- Python tf.GradientTape.jacobian用法及代碼示例
- Python tf.GradientTape.reset用法及代碼示例
- Python tf.GradientTape.batch_jacobian用法及代碼示例
- Python tf.GradientTape.stop_recording用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.summary.scalar用法及代碼示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代碼示例
- Python tf.linalg.LinearOperatorToeplitz.solve用法及代碼示例
- Python tf.raw_ops.TPUReplicatedInput用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.Graph.as_default。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。