tf.Variable()和tf.get_variable()对比


1-4原文链接:https://blog.csdn.net/u012223913/article/details/78533910

1. tf.Variable()

W = tf.Variable(<initial-value>, name=<optional-name>)
用于生成一个初始值为initial-value的变量。必须指定初始化值

2.tf.get_variable()

 W = tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None,
       regularizer=None, trainable=True, collections=None)

获取已存在的变量(要求不仅名字,而且初始化方法等各个参数都一样),如果不存在,就新建一个。 
可以用各种初始化方法,不用明确指定值。

3.区别

推荐使用tf.get_variable(), 因为:

1. 初始化更方便

比如用xavier_initializer:

W = tf.get_variable("W", shape=[784, 256],
       initializer=tf.contrib.layers.xavier_initializer())

 

2. 方便共享变量

因为tf.get_variable() 会检查当前命名空间下是否存在同样name的变量,可以方便共享变量。而tf.Variable 每次都会新建一个变量。

需要注意的是tf.get_variable() 要配合reusetf.variable_scope() 使用。

4. 举个栗子

4.1 首先介绍一下tf.variable_scope().

如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。为了解决这个问题,TensorFlow 提出了 tf.variable_scope 函数:它的主要作用是,在一个作用域 scope 内共享一些变量,举个简单的栗子:

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1]) #v.name == "foo/v:0"


简单来说就是给变量名前再加了个变量空间名。

4.2 对比

接下来看看怎么用tf.get_variable()实现共享变量:

with tf.variable_scope("one"):
    a = tf.get_variable("v", [1]) #a.name == "one/v:0"
with tf.variable_scope("one"):
    b = tf.get_variable("v", [1]) #创建两个名字一样的变量会报错 ValueError: Variable one/v already exists 
with tf.variable_scope("one", reuse = True): #注意reuse的作用。
    c = tf.get_variable("v", [1]) #c.name == "one/v:0" 成功共享,因为设置了reuse

assert a==c #Assertion is true, they refer to the same object.


然后看看如果用tf.Variable() 会有什么效果:

with tf.variable_scope("two"):
    d = tf.get_variable("v", [1]) #d.name == "two/v:0"
    e = tf.Variable(1, name = "v", expected_shape = [1]) #e.name == "two/v_1:0"  

assert d==e #AssertionError: they are different objects


可以看到,同样的命名空间(‘two’)和名字(v),但是d和e的值却不一样。

5  tf.name_scope()

为变量划分范围,在可视化中, 这表示在计算图中的一个层级,name_scope会影响op_name,不会影响get_variable()创建的变量。

with tf.variable_scope('foo') as foo :
	with tf.name_scope('bar') as bar:
		v = tf.get_variable('v', [1])
		b = tf.Variable(tf.zeros([1]), name='b')
		x = 1.0 + v
	assert v.name == 'foo/v:0'
	assert b.name == 'foo/bar/b:0'
	assert x.op.name == 'foo/bar/add'

看如何影响 名字的: 

with tf.variable_scope('V1') as fo:
	a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
	x1 = a1 + 2
	x2 = a1 + 5
	# print(fo.name)     # V1
	# print(x1.op.name)  # V1/add
	# print(x2.op.name)  # V1/add_1
	# print(x1.name)	   # V1/add:0
	# print(x2.name)     # V1/add_1:0
with tf.variable_scope('V1', reuse=True) as f:
	a3 = tf.get_variable('a1')
	# print(x2.name)  # V1/add_1:0
	# print(x2.op.name)  # V1/add_1
	x = a1 + a3
	b = x + 3
	print(f.name)  # V1
	print(x.op.name )  # V1_1/add
	print(b.name)  # V1_1/add_1:0
	print(a1.name, a3.name)  # V1/a1:0 V1/a1:0
	assert a1 == a3

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值