• Tensorflow函数——tf.variable_scope()


    Tensorflow函数——tf.variable_scope()详解

    https://blog.csdn.net/yuan0061/article/details/80576703

    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
    • 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
    • 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
    • 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
    • 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
    • 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:在此范围中创建的变量类型(默认为传递范围中的类型,或从父范围继承)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    返回:

    可以捕获和重复使用的范围。

            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
                </div>
    萍水相逢逢萍水,浮萍之水水浮萍!
  • 相关阅读:
    Dirichlet Process 和 Dirichlet Process Mixture模型
    也说说EM
    Seven Steps to Success Machine Learning in Practice
    matlab norm 范式
    nginx中时间的管理
    Android中解析网络请求的URL
    jsp if else c标签 总结
    Android开发系列之button事件的4种写法
    oracle中split的使用
    Unix C++(boost) 线程同步和线程组
  • 原文地址:https://www.cnblogs.com/AIBigTruth/p/10504161.html
Copyright © 2020-2023  润新知