Tensorflow中的Variable、get_variable、variable_scope、name_scope、Graph

本文深入解析了TensorFlow中tf.Variable()与tf.get_variable()的区别,包括如何管理变量命名冲突及共享变量;同时对比了tf.variable_scope()与tf.name_scope()的作用,并通过实例展示了tf.Graph()在不同计算图上张量和运算不共享的特点。
部署运行你感兴趣的模型镜像

1、tf.Variable() 和 tf.get_variable()

(1)tf.Variable()会自动检测命名冲突并自行处理。

with tf.Session( ) as sess:
    with tf.variable_scope("scope2"):
        w2 = tf.Variable(0.0, name="w2")
        w3 = tf.Variable(1.1, name="w2")
    with tf.variable_scope("scope2"):
        w2_p = tf.Variable(1.0, name="w2")
    tf.global_variables_initializer().run()
    print(w2.name)
    print(w3.name) 
    print(w2_p.name)
//运行结果:
scope2/w2:0
scope2/w2_1:0
scope2_1/w2:0
//注:tf.variable_scope()用于管理一个graph中变量的名字,避免变量之间的命名冲突

(2)tf.get_variable() 当我们需要共享变量的时候,需要使用tf.get_variable()。如果name还不存在,就用initializer值初始化变量,若name已经存在,且reuse=True,则变量值为之前已经定义的值,initializer值不起作用。若未使用变量名重复使用机制(reuse=True),在同一个variable_scope中定义具有相同name的多个variable会报错。


with tf.Session( ) as sess:
    with tf.variable_scope("scope1") as scope1:
        w1 = tf.get_variable("w1", initializer=5.0)
    with tf.variable_scope("scope1", reuse=tf.AUTO_REUSE) as scope2:
        w2 = tf.get_variable("w1", initializer=6.0)
    with tf.variable_scope("scope2") as scope3:
        w3 = tf.get_variable("w1", initializer=7.0)
    tf.global_variables_initializer().run()
    print(w1.name, sess.run(w1))
    print(w2.name, sess.run(w2))
    print(w3.name, sess.run(w3))
//运行结果:
scope1/w1:0 5.0
scope1/w1:0 5.0
scope2/w1:0 7.0

若将reuse设为False,则会报错:ValueError: Variable w1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

2、tf.variable_scope()与tf.name_scope()

(1)tf.variable_scope()一般与tf.name_scope()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突,tf.variable_scope()允许在一个variable_scope下面共享变量。variable_scope的reuse的默认值为False。见上面的例子。

(2)tf.name_scope()创建一个graph,作用相当于一个以name参数命名的文件夹,主要用于管理一个graph里的各种operation。每一个graph下面可以定义各种operation或者subgraph,实现一种层次化有条理的管理,避免各个op之间命名冲突。

  (3) 通常情况下,tf.variable_scope和tf.name_scope配合,能画出非常漂亮的流程图,但是他们两个之间又有着细微的差别,那就是name_scope只能管住操作ops的名字,而管不住变量Variables的名字。

3、tf.Graph() 该函数可以生成新的计算图。不同计算图上的张量运算都不会共享。

g1 = tf.Graph()
with g1.as_default():
    v = tf.get_variable("v", shape=[1], initializer=tf.zeros_initializer)
g2 = tf.Graph()
with g2.as_default():
    v = tf.get_variable("v", shape=[1], initializer=tf.ones_initializer)
with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=tf.AUTO_REUSE):
        v = tf.get_variable("v")
        print(v.name)
        print(sess.run(v))
with tf.Session(graph=g2) as sess1:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=tf.AUTO_REUSE):
        v = tf.get_variable("v")
        print(v.name)
        print(sess1.run(v))
// 运行结果:
v:0
[0.]
v:0
[1.] //值不同

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值