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

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

2018年06月05日 09:38:25 yuan0061 阅读数:2567
 

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>

Tensorflow函数——tf.variable_scope()的更多相关文章

  1. Tensorflow函数——tf.placeholder()函数

    tf.placeholder()函数 Tensorflow中的palceholder,中文翻译为占位符,什么意思呢? 在Tensoflow2.0以前,还是静态图的设计思想,整个设计理念是计算流图,在编 ...

  2. Tensorflow函数——tf.set_random_seed(seed)

    设置图级随机seed. 依赖于随机seed的操作实际上从两个seed中获取:图级和操作级seed. 这将设置图级别的seed. 其与操作级seed的相互作用如下: 1.如果没有设置图形级别和操作see ...

  3. TensorFlow函数: tf.stop_gradient

    停止梯度计算. 在图形中执行时,此操作按原样输出其输入张量. 在构建计算梯度的操作时,这个操作会阻止将其输入的共享考虑在内.通常情况下,梯度生成器将操作添加到图形中,通过递归查找有助于其计算的输入来计 ...

  4. TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()

    tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...

  5. tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope

    tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get ...

  6. Tensorflow常用的函数:tf.cast

    1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example:  import ...

  7. TensorFlow函数教程:tf.nn.dropout

    tf.nn.dropout函数 tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) 定义在:tensorflow ...

  8. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)

    上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...

  9. TensorFlow函数:tf.truncated_normal

    tf.truncated_normal函数 tf.truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, ...

随机推荐

  1. GridView Bind 短日期 格式

    ASP.NET的GridView控件的编辑模板中,需要绑定数据库中的某个字段,如<%# Bind("startTime","{0:d}") %> 在 ...

  2. Spring boot http, https

    1:同时支持http, https配置 参考文献:  https://www.cnblogs.com/lianggp/p/8136540.html 2:http 转发到 https配置 @Bean p ...

  3. java byte[]与十六进制字符串相互转换

    http://blog.csdn.net/worm0527/article/details/69939307 http://blog.csdn.net/androiddeveloper_lee/art ...

  4. UI5-学习篇-9-本地Eclipse UI5应用发布到SAP前端服务器

    参考路径: https://blogs.sap.com/2017/11/19/sap-fiori-ui5-application-deployment/ 1.准备环境 2.上载SAP-FIORI前端服 ...

  5. Maven仓库—Nexus环境搭建及使用

    使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包 http://blog.csdn.net/yanjun008/article/details/42084109 Nexus介 ...

  6. 20.struts2的数据填充和类型转换.md

    目录 1. struts2的自动填充 2. struts2的对象填充 3. struts2的类型转换器 3.1 类继承关系 3.2 局部转换器 3.3 全局转换器 3.4 注意 1. struts2的 ...

  7. 学习JS的心路历程-参数传递方式(上)

    很多人认为JS的传递方式是值是Call by value, 物件及数组是Call by Reference.甚至还有人宣称其实JS是Call by sharing,那到底是哪一个呢? 这两天我们一一来 ...

  8. 新闻推荐系统:基于内容的推荐算法(Recommender System:Content-based Recommendation)

    https://blog.csdn.net/qq_32690999/article/details/77434381 因为开发了一个新闻推荐系统的模块,在推荐算法这一块涉及到了基于内容的推荐算法(Co ...

  9. 内核 platform_get_resource() 函数解析

    struct resource *platform_get_resource(struct platform_device *dev,            unsigned int type, un ...

  10. struts原理图

    n the diagram, an initial request goes to the Servlet container (such as Jetty or Resin) which is pa ...