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


Python tf.Module.with_name_scope用法及代碼示例

用法

@classmethod
with_name_scope(
    method
)

參數

  • method 包裝的方法。

返回

  • 原始方法被包裝,使其進入模塊的名稱範圍。

裝飾器自動進入模塊名稱範圍。

class MyModule(tf.Module):
  @tf.Module.with_name_scope
  def __call__(self, x):
    if not hasattr(self, 'w'):
      self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
    return tf.matmul(x, self.w)

使用上述模塊將產生 tf.Variabletf.Tensor ,其名稱包括模塊名稱:

mod = MyModule()
mod(tf.ones([1, 2]))
<tf.Tensor:shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
mod.w
<tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
numpy=..., dtype=float32)>

相關用法


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