當前位置: 首頁>>代碼示例>>Python>>正文


Python variable_scope._pure_variable_scope方法代碼示例

本文整理匯總了Python中tensorflow.python.ops.variable_scope._pure_variable_scope方法的典型用法代碼示例。如果您正苦於以下問題:Python variable_scope._pure_variable_scope方法的具體用法?Python variable_scope._pure_variable_scope怎麽用?Python variable_scope._pure_variable_scope使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.ops.variable_scope的用法示例。


在下文中一共展示了variable_scope._pure_variable_scope方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: build

# 需要導入模塊: from tensorflow.python.ops import variable_scope [as 別名]
# 或者: from tensorflow.python.ops.variable_scope import _pure_variable_scope [as 別名]
def build(self, _):
    # We need variable scopes for now because we want the variable partitioning
    # information to percolate down. We also use _pure_variable_scope's here
    # since we want to open up a name_scope in the `call` method while creating
    # the ops.
    with variable_scope._pure_variable_scope(self.name):  # pylint: disable=protected-access
      for column in self._feature_columns:
        with variable_scope._pure_variable_scope(  # pylint: disable=protected-access
            fc_v2._sanitize_column_name_for_variable_scope(column.name)):  # pylint: disable=protected-access
          # Create the state for each feature column
          column.create_state(self._state_manager)

          # Create a weight variable for each column.
          if isinstance(column, fc_v2.CategoricalColumn):
            first_dim = column.num_buckets
          else:
            first_dim = column.variable_shape.num_elements()
          self._state_manager.create_variable(
              column,
              name='weights',
              dtype=tf.float32,
              shape=(first_dim, self._units),
              initializer=tf.keras.initializers.zeros(),
              trainable=self.trainable)

      # Create a bias variable.
      self.bias = self.add_variable(
          name='bias_weights',
          dtype=tf.float32,
          shape=[self._units],
          initializer=tf.keras.initializers.zeros(),
          trainable=self.trainable,
          use_resource=True,
          # TODO(rohanj): Get rid of this hack once we have a mechanism for
          # specifying a default partitioner for an entire layer. In that case,
          # the default getter for Layers should work.
          getter=variable_scope.get_variable)

    super(_LinearModelLayer, self).build(None) 
開發者ID:tensorflow,項目名稱:estimator,代碼行數:41,代碼來源:linear.py

示例2: __init__

# 需要導入模塊: from tensorflow.python.ops import variable_scope [as 別名]
# 或者: from tensorflow.python.ops.variable_scope import _pure_variable_scope [as 別名]
def __init__(self, name, func, create_scope_now=False, unique_name=None,
               custom_getter=None):
    """Creates a template for the given function.

    Args:
      name: A name for the scope created by this template. The
        name will be made unique by appending `_N` to the it (see how
        `tf.variable_scope` treats the `default_name` for details).
      func: The function to apply each time.
      create_scope_now: Whether to create the scope at Template construction
        time, rather than first call. Defaults to false. Creating the scope at
        construction time may be more convenient if the template is to passed
        through much lower level code, and you want to be sure of the scope
        name without knowing exactly where it will be first called. If set to
        True, the scope will be created in the constructor, and all subsequent
        times in __call__, leading to a trailing numeral being added to the
        names of all created Tensors. If set to False, the scope will be created
        at the first call location.
      unique_name: When used, it overrides name_ and is not made unique. If a
        template of the same scope/unique_name already exists and reuse is
        false, an error is raised. Defaults to None.
      custom_getter: optional custom getter to pass to variable_scope()

    Raises:
      ValueError: if the name is None.
    """
    self._func = func
    self._stacktrace = traceback.format_stack()[:-2]
    self._name = name
    self._unique_name = unique_name
    self._custom_getter = custom_getter
    if name is None:
      raise ValueError("name cannot be None.")
    if create_scope_now:
      with variable_scope._pure_variable_scope(  # pylint:disable=protected-access
          (self._unique_name or
           variable_scope._get_unique_variable_scope(self._name)),  # pylint:disable=protected-access
          custom_getter=self._custom_getter) as vs:
        self._variable_scope = vs
    else:
      self._variable_scope = None
    # This variable keeps track of whether the template has been called yet,
    # which is not the same as whether the scope has been created.
    self._variables_created = False 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:46,代碼來源:template.py

示例3: create_variables_in_class_scope

# 需要導入模塊: from tensorflow.python.ops import variable_scope [as 別名]
# 或者: from tensorflow.python.ops.variable_scope import _pure_variable_scope [as 別名]
def create_variables_in_class_scope(method):
  """Force the variables constructed in this class to live in the sonnet module.
  Wraps a method on a sonnet module.

  For example the following will create two different variables.
  ```
  class Mod(snt.AbstractModule):
    @create_variables_in_class_scope
    def dynamic_thing(self, input, name):
      return snt.Linear(name)(input)
  mod.dynamic_thing(x, name="module_nameA")
  mod.dynamic_thing(x, name="module_nameB")
  # reuse
  mod.dynamic_thing(y, name="module_nameA")
  ```
  """
  @functools.wraps(method)
  def wrapper(obj, *args, **kwargs):
    def default_context_manager(reuse=None):
      variable_scope = obj.variable_scope
      return tf.variable_scope(variable_scope, reuse=reuse)

    variable_scope_context_manager = getattr(obj, "_enter_variable_scope",
                                             default_context_manager)
    graph = tf.get_default_graph()

    # Temporarily enter the variable scope to capture it
    with variable_scope_context_manager() as tmp_variable_scope:
      variable_scope = tmp_variable_scope

    with variable_scope_ops._pure_variable_scope(
        variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope:

      name_scope = variable_scope.original_name_scope
      if name_scope[-1] != "/":
        name_scope += "/"

      with tf.name_scope(name_scope):
        sub_scope = snt_util.to_snake_case(method.__name__)
        with tf.name_scope(sub_scope) as scope:
          out_ops = method(obj, *args, **kwargs)
          return out_ops

  return wrapper 
開發者ID:itsamitgoel,項目名稱:Gun-Detector,代碼行數:46,代碼來源:utils.py


注:本文中的tensorflow.python.ops.variable_scope._pure_variable_scope方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。