1. 在上下文管理器“foo”中创建变量“v”。
import tensorflow as tf
In [
3]:
with tf.variable_scope(
"foo"):
v = tf.get_variable(
"v", [
1], initializer=tf.constant_initializer(
1.0))
#
with tf.variable_scope(
"foo"):
# v = tf.get_variable(
"v", [
1])
with tf.variable_scope(
"foo", reuse=
True):
v1 = tf.get_variable(
"v", [
1])
print( v == v1)
#
with tf.variable_scope(
"bar", reuse=
True):
# v = tf.get_variable(
"v", [
1])
True
2. 嵌套上下文管理器中的reuse参数的使用。
In [
5]:
with tf.variable_scope(
"root"):
print(tf.get_variable_scope().reuse)
with tf.variable_scope(
"foo", reuse=
True):
print (tf.get_variable_scope().reuse)
with tf.variable_scope(
"bar"):
print( tf.get_variable_scope().reuse)
print( tf.get_variable_scope().reuse)
False
True
True
False
3. 通过variable_scope来管理变量。
In [6]:
v1 = tf.get_variable("v", [1])
print(v1.name)
with tf.variable_scope("foo",reuse=True):
v2 = tf.get_variable("v", [1])
print (v2.name)
with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v3 = tf.get_variable("v", [1])
print (v3.name)
v4 = tf.get_variable("v1", [1])
print (v4.name)
v:0
foo/v:0
foo/bar/v:0
v1:0
4. 我们可以通过变量的名称来获取变量。
In [8]:
with tf.variable_scope("",reuse=True):
v5 = tf.get_variable("foo/bar/v", [1])
print( v5 == v3)
print( v2 == v3)
v6 = tf.get_variable("v1", [1])
print (v6 == v4)
True
False
True
5. 获取全局变量
v = tf
.Variable(
0, dtype=tf
.float32, name=
"v")
for variables
in tf
.global_variables(): print( variables
.name)
ema = tf
.train.ExponentialMovingAverage(
0.99)
maintain_averages_op = ema
.apply(tf
.global_variables())
for variables
in tf
.global_variables(): print( variables
.name)