`
wx1568037608
  • 浏览: 33494 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Tensorflow函数——tf.variable_scope()

 
阅读更多

tf.variable_scope(name_or_scope,default_name=None,values=None,initializer=None,regularizer=None,caching_device=None,partitioner=None,custom_getter=None,reuse=None,dtype=None)

返回一个用于定义创建variable(层)的op的上下文管理器。

该上下文管理器验证(可选)值来自同一图形,确保图形是默认图形,并推送名称范围和variable范围。

如果name_or_scope不为None,则按原样使用。 如果范围为None,则使用default_name。 在这种情况下,如果以前在同一个范围内使用了相同的名称,那么它将会被唯一的附加到_N。

可变范围允许创建新的variable并分享已创建的variable,同时提供检查,不会意外创建或共享。 有关详细信息,请参阅可变范围如何操作,这里我们仅提供几个基本示例。

如何创建新variable的简单示例:

with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])
        assert v.name == "foo/bar/v:0"
  • 1
  • 2
  • 3
  • 4

共享variable的基本示例:

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v", [1])
assert v1 == v
  • 1
  • 2
  • 3
  • 4
  • 5

通过捕获范围并设置重用来共享variable:

with tf.variable_scope("foo") as scope:
    v = tf.get_variable("v", [1])
    scope.reuse_variables()
    v1 = tf.get_variable("v", [1])
assert v1 == v
  • 1
  • 2
  • 3
  • 4
  • 5

为了防止意外共享variable,当在非重用范围内获取现有variable时,我们引发异常。

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
    v1 = tf.get_variable("v", [1])
    #  Raises ValueError("... v already exists ...").
  • 1
  • 2
  • 3
  • 4

同样,当尝试获取在重用模式下不存在的variable时,我们引发异常。

with tf.variable_scope("foo", reuse=True):
    v = tf.get_variable("v", [1])
    #  Raises ValueError("... v does not exists ...").
  • 1
  • 2
  • 3

请注意,重用标志是继承的:如果我们打开一个重用的范围,那么它的所有子范围也会变得重用。

ARGS:

name_or_scope:string或VariableScope:要打开的范围。
default_name:如果name_or_scope参数为None,则将使用默认名称,此名称将被唯一。 如果提供了name_or_scope,它将不会被使用,因此它不是必需的,可以是None。
值:传递给op函数的Tensor参数列表。
初始化器:此范围内的变量的默认初始化程序。
regularizer:此范围内的变量的默认正则符。
caching_device:此范围内的变量的默认缓存设备。
partitioner:此范围内变量的默认分区。
custom_getter:此范围内变量的默认定制getter。
重用:True或None 如果是,我们进入该范围以及所有子范围的重用模式; 如果没有,我们只是继承父范围重用。
dtype:在此范围中创建的变量类型(默认为传递范围中的类型,或从父范围继承)
分享到:
评论

相关推荐

    TensorFlow Python API documentation.pdf

    TensorFlow提供了对Tensor的多种操作函数,比如创建常量Tensor(tf.constant(value,dtype=None,shape=None,name='Const')),生成连续值的Tensor(如tf.linspace(start,stop,num,name=None)和tf.range(start,limit=...

    【官方文档】TensorFlow Python API documentation.pdf

    - `tf.Variable(initial_value, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None)`: 创建一个变量。 以上是关于TensorFlow Python API...

    TensorFlow学习笔记1

    18. **name_scope与get_variable的关系**:`tf.name_scope()`不影响`tf.get_variable()`创建的变量的命名,而`tf.variable_scope()`用于变量共享,与`tf.get_variable()`配合使用。 19. **获取可训练变量**:`tf....

    5-3tensorboard 网络运行

    W=tf.Variable(tf.zeros([784,10]),name='W') variable_summaries(W) with tf.name_scope('biases'): b=tf.Variable(tf.zeros([10]),name='b') variable_summaries(b) with tf.name_scope('wx_plus_b'): wx_...

    Tensorflow实现LSTM详解.pdf

    if i > 0: tf.get_variable_scope().reuse_variables() output, state = lstm_cell(input_s[:, i, :], state) outputs.append(output) ``` 以上代码展示了如何在TensorFlow中构建一个简单的LSTM模型。注意,实际...

    数据挖掘与数据分析应用案例 数据挖掘算法实践 基于Python的卷积神经网络在Tensorflow算法中的文本分类研究.doc

    W = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), name="W") embedded_chars = tf.nn.embedding_lookup(W, self.input_x) pooled_outputs = [] for i, filter_size in enumerate...

    TENSORFLOW变量作用域(VARIABLE SCOPE)

    在TensorFlow中,变量作用域的实现涉及到两个关键函数:tf.get_variable()和tf.variable_scope()。tf.get_variable()用于创建或者返回一个已经存在的变量,允许用户指定变量名称、形状和初始化器。tf.variable_scope...

    tensorflow之变量初始化(tf.Variable)使用详解

    总结起来,`tf.Variable`是TensorFlow中用于存储和更新模型参数的关键组件。了解如何正确创建、初始化和使用变量对于编写有效的TensorFlow代码至关重要。通过掌握这些基本概念,你可以更有效地构建和训练深度学习...

    Tensorflow基础.docx

    7. **命名空间与变量作用域**:`tf.name_scope`用于组织图形的视觉结构,而`tf.variable_scope`用于变量复用和变量命名规则。`tf.get_variable()`用于在指定作用域内创建或查找变量。 8. **Tensors的秩 (rank)**:...

    TensorFlow Python API文档

    ### TensorFlow Python API 文档知识点概览 #### 一、构建图(Building Graphs) 在 TensorFlow 中,计算图是核心概念之一。...- **变量**:`tf.Variable` — 用于存储模型参数的可训练变量。后续将详细介绍。

    浅谈Tensorflow由于版本问题出现的几种错误及解决方法

    解决方法是,在定义模型时使用`with tf.variable_scope(tf.get_variable_scope()) as scope`,然后在这个作用域中定义变量和模型。 对于上述提到的Tensorflow版本更新导致的错误,最重要的是要及时查看Tensorflow的...

    TensorFlow Python API documentation

    - **`tf.Variable(initial_value, trainable=True, collections=None, caching_device=None, name=None, validate_shape=True, constraint=None)`**:创建一个变量。 - **使用变量**: - **`var.initializer`**:...

    TensorFLow 变量命名空间实例

    本文将详细讲解 TensorFlow 中的两种命名空间管理工具:`tf.name_scope` 和 `tf.variable_scope`。 **一、tf.name_scope** `tf.name_scope` 主要用于为操作(ops)提供逻辑上的分组,使得在 TensorBoard 中展示的...

    TensorBoard:可视化学习

    为了简化这些任务并提升开发效率,TensorFlow 团队推出了一款强大的可视化工具——TensorBoard。 #### TensorBoard 概述 TensorBoard 是一个与 TensorFlow 密切集成的开源项目,用于提供图形化的用户界面,帮助...

    tensorflow创建变量以及根据名称查找变量

    with tf.variable_scope('scope1', reuse=tf.AUTO_REUSE): y1 = tf.get_variable('y1', initializer=1.0) y2 = tf.get_variable('y1', initializer=0.0) ``` 在这个例子中,尽管`y1`和`y2`都尝试创建名为'y1'的...

    tensorflow学习笔记

    tf.variable_scope(): 提供了简单的命名空间技术以避免冲突,可以让变量有相同的命名,包括tf.get_variable得到的变量,还有tf.Variable的变量 tf.name_scope(): 可以让变量有相同的命名,只是限于tf.Variable的...

    TensorFlow命名空间和TensorBoard图节点实例

    本篇将详细解释`tf.variable_scope`和`tf.name_scope`这两个关键的命名空间管理函数,并探讨它们在TensorBoard中如何影响计算图的可视化。 首先,`tf.variable_scope`主要用于管理变量的命名,它创建了一个命名空间...

Global site tag (gtag.js) - Google Analytics