tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
2017-04-07 15:34
686 查看
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型)、或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时
,往往需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护
一个条理清晰的graph非常重要。因此,tensorflow中用tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()几个函数来实现:
1、tf.Variable(<variable_name>),tf.get_variable(<variable_name>)的作用与区别:
tf.Variable(<variable_name>)会自动检测命名冲突并自行处理,但tf.get_variable(<variable_name>)则遇到重名的变量创建且变量名没有设置为共享变量时,则会报错。
tf.Variable(<variable_name>)和tf.get_variable(<variable_name>)都是用于在一个name_scope下面获取或创建一个变量的两种方式,区别在于:
tf.Variable(<variable_name>)用于创建一个新变量,在同一个name_scope下面,可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生了其实是两个不同的变量。
tf.get_variable(<variable_name>)用于获取一个变量,并且不受name_scope的约束。当这个变量已经存在时,则自动获取;如果不存在,则自动创建一个变量。
2、tf.name_scope(<scope_name>)与tf.variable_scope(<scope_name>):
tf.name_scope(<scope_name>):主要用于管理一个图里面的各种op,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的
堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。
tf.variable_scope(<scope_name>):一般与tf.name_scope()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突,tf.variable_scope(<scope_name>)允许在一个variable_scope下面共享变量。
需要注意的是:创建一个新的variable_scope时不需要把reuse属性设置未False,只需要在使用的时候设置为True就可以了。
with tf.variable_scope("image_filters1") as scope1:
result1 = my_image_filter(image1)
with tf.variable_scope(scope1, reuse = True)
result2 = my_image_filter(image2)
获得更多深度学习在NLP方面应用的经典论文、实践经验和最新消息,欢迎关注微信公众号“DeepLearning_NLP”
或者扫描头像二维码添加关注。
参考文献:
http://www.cnblogs.com/Charles-Wan/p/6200446.html https://www.tensorflow.org/versions/r1.1/api_docs/python/tf/Graph#name_scope
,往往需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护
一个条理清晰的graph非常重要。因此,tensorflow中用tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()几个函数来实现:
1、tf.Variable(<variable_name>),tf.get_variable(<variable_name>)的作用与区别:
tf.Variable(<variable_name>)会自动检测命名冲突并自行处理,但tf.get_variable(<variable_name>)则遇到重名的变量创建且变量名没有设置为共享变量时,则会报错。
tf.Variable(<variable_name>)和tf.get_variable(<variable_name>)都是用于在一个name_scope下面获取或创建一个变量的两种方式,区别在于:
tf.Variable(<variable_name>)用于创建一个新变量,在同一个name_scope下面,可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生了其实是两个不同的变量。
tf.get_variable(<variable_name>)用于获取一个变量,并且不受name_scope的约束。当这个变量已经存在时,则自动获取;如果不存在,则自动创建一个变量。
2、tf.name_scope(<scope_name>)与tf.variable_scope(<scope_name>):
tf.name_scope(<scope_name>):主要用于管理一个图里面的各种op,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的
堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。
tf.variable_scope(<scope_name>):一般与tf.name_scope()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突,tf.variable_scope(<scope_name>)允许在一个variable_scope下面共享变量。
需要注意的是:创建一个新的variable_scope时不需要把reuse属性设置未False,只需要在使用的时候设置为True就可以了。
with tf.variable_scope("image_filters1") as scope1:
result1 = my_image_filter(image1)
with tf.variable_scope(scope1, reuse = True)
result2 = my_image_filter(image2)
获得更多深度学习在NLP方面应用的经典论文、实践经验和最新消息,欢迎关注微信公众号“DeepLearning_NLP”
或者扫描头像二维码添加关注。
参考文献:
http://www.cnblogs.com/Charles-Wan/p/6200446.html https://www.tensorflow.org/versions/r1.1/api_docs/python/tf/Graph#name_scope
相关文章推荐
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tf 共享变量 tensorflow 里面 name_scope, variable_scope
- tensorflow之变量作用域与变量共享(name_scope,variable_scope,get_variable,Variable)
- tensorflow共享变量, tf.get_variable() 和 tf.Varibele()的区别
- 【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别
- [Tensorflow]Sharing Variables 共享权值【tf.get_variable 和 tf.variable_scope】
- tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tensorflow里面共享变量、name_scope, variable_scope等如何理解
- tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- Tensorflow中tf.get_variable和tf.variable_scope的使用
- tf.variable_scope()和tf.name_scope()函数的区别
- tensorflow variable_scope,tf.name_scope, tf.variable, tf.get_varible
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
- tf.variable_scope()和tf.name_scope(),tf.Variable()和tf.get_variable()
- tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()