tf.valuable_scope()函数/类用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Arctic_Beacon/article/details/84304764

官网的解释和例子实在是wast time,不用去看它了。

import tensorflow as tf


a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) 


with tf.variable_scope('V1'):     
    a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2') 
    
    
with tf.variable_scope('V2'): 
    a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(80)) 
    

print (a1.name)
print (a2.name)
print (a3.name)

    
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())    
    print(sess.run(a1))
    print(sess.run(a2))
    print(sess.run(a3))

输出结果可以看出,V1是范围,变量名a2在V1范围。变量名a1在V2范围。后面的输出value可以看出,此函数主要是用于name管理。然而此a1非彼a1,一般我们用的是tensor.Variable。

type(a1)
Out[2]: tensorflow.python.ops.variables.Variable

type(a3)
Out[3]: tensorflow.python.ops.variables.Variable

猜你喜欢

转载自blog.csdn.net/Arctic_Beacon/article/details/84304764