TensorFlow变量管理
来源:互联网 发布:mysql怎样存储图片 编辑:程序博客网 时间:2024/05/17 22:48
一、TensorFlow变量管理
1. TensorFLow还提供了tf.get_variable函数来创建或者获取变量,tf.variable用于创建变量时,其功能和tf.Variable基本是等价的。tf.get_variable中的初始化方法(initializer)的参数和tf.Variable的初始化过程也类似,initializer函数和tf.Variable的初始化方法是一一对应的,详见下表。
tf.get_variable和tf.Variable最大的区别就在于指定变量名称的参数。对于tf.Variable函数,变量名称是一个可选的参数,通过name=”v”的形式给出,对于tf.get_variable函数,变量名称是一个必填的参数,tf.get_variable会根据这个名称去创建或者获取变量。
2. 通过tf.variable_scope函数可以控制tf.get_variable函数的语义。当tf.variable_scope函数的参数reuse=True生成上下文管理器时,该上下文管理器内的所有的tf.get_variable函数会直接获取已经创建的变量,如果变量不存在则报错;当tf.variable_scope函数的参数reuse=False或者None时创建的上下文管理器中,tf.get_variable函数则直接创建新的变量,若同名的变量已经存在则报错。
3. 另tf.variable_scope函数是可以嵌套使用的。嵌套的时候,若某层上下文管理器未声明reuse参数,则该层上下文管理器的reuse参数与其外层保持一致。
4.tf.variable_scope函数提供了一个管理变量命名空间的方式。在tf.variable_scope中创建的变量,名称.name中名称前面会加入命名空间的名称,并通过“/”来分隔命名空间的名称和变量的名称。tf.get_variable("foou/baru/u", [1]),可以通过带命名空间名称的变量名来获取其命名空间下的变量。
二、TensorFlow编程演示
import tensorflow as tf# 在名字为foo的命名空间内创建名字为v的变量with tf.variable_scope("foo"): v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))'''# 因为命名空间foo内已经存在变量v,再次创建则报错with tf.variable_scope("foo"): v = tf.get_variable("v", [1])# ValueError: Variable foo/v already exists, disallowed.# Did you mean to set reuse=True in VarScope?'''# 将参数reuse参数设置为True,则tf.get_variable可直接获取已声明的变量with tf.variable_scope("foo", reuse=True): v1 = tf.get_variable("v", [1]) print(v == v1) # True'''# 当reuse=True时,tf.get_variable只能获取指定命名空间内的已创建的变量with tf.variable_scope("bar", reuse=True): v2 = tf.get_variable("v", [1])# ValueError: Variable bar/v does not exist, or was not created with# tf.get_variable(). Did you mean to set reuse=None in VarScope?'''with tf.variable_scope("root"): # 通过tf.get_variable_scope().reuse函数获取当前上下文管理器内的reuse参数取值 print(tf.get_variable_scope().reuse) # False with tf.variable_scope("foo1", reuse=True): print(tf.get_variable_scope().reuse) # True with tf.variable_scope("bar1"): # 嵌套在上下文管理器foo1内的bar1内未指定reuse参数,则保持与外层一致 print(tf.get_variable_scope().reuse) # True print(tf.get_variable_scope().reuse) # False# tf.variable_scope函数提供了一个管理变量命名空间的方式u1 = tf.get_variable("u", [1])print(u1.name) # u:0with tf.variable_scope("foou"): u2 = tf.get_variable("u", [1]) print(u2.name) # foou/u:0with tf.variable_scope("foou"): with tf.variable_scope("baru"): u3 = tf.get_variable("u", [1]) print(u3.name) # foou/baru/u:0 u4 = tf.get_variable("u1", [1]) print(u4.name) # foou/u1:0# 可直接通过带命名空间名称的变量名来获取其命名空间下的变量with tf.variable_scope("", reuse=True): u5 = tf.get_variable("foou/baru/u", [1]) print(u5.name) # foou/baru/u:0 print(u5 == u3) # True u6 = tf.get_variable("foou/u1", [1]) print(u6.name) # foou/u1:0 print(u6 == u4) # True
- TensorFlow 变量管理
- TensorFlow变量管理
- Tensorflow 的变量管理
- TensorFlow 变量管理
- TensorFlow变量管理
- tensorflow--变量管理
- Tensorflow基础:变量管理
- Tensorflow的变量管理
- tensorflow变量管理
- TensorFlow笔记之变量管理
- 83、Tensorflow中的变量管理
- Tensorflow学习笔记-变量管理
- Tensorflow深度学习之十四:Tensorflow变量管理
- Tensorflow 变量
- 罗斯基白话:TensorFlow+实战系列(四)变量管理
- tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope
- tensorflow 共享变量
- Tensorflow共享变量
- oracle12c R2 asmca创建ASM磁盘组ORA-15031解决
- 还是畅通工程(最小生成树模板题)
- 如何将Eclipse的主题变得更优(装)雅(逼)
- VR技术在数据中心3D机房中的应用(下)
- 发送Django error log 到企业微信,python+微信实现即时告警
- TensorFlow变量管理
- js中的Object.defineProperty方法
- Eclipse手动集成插入SDK插件编写Android项目
- C# BinaryFormatter序列化对象保存后,不能改变命空间
- 将Excel表格转换为MarkDown表格
- day02 java语言基础(变量)
- WPF 使用winform控件
- 去除VS2010对汉字的拼写检查后留下的红色波浪线
- 关于屏幕适配?