tf.get_variable中变量的重复利用,reuse关键字

reuse为True的时候表示用tf.get_variable 得到的变量可以在别的地方重复使用

例如:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  

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

with tf.variable_scope('V1', reuse=True):
	a3 = tf.get_variable('a1')

with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	print a1.name
	print sess.run(a1)
	print a3.name
	print sess.run(a3)
或者下面的这个代码:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  

with tf.variable_scope('V1') as scope:
	a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
 	scope.reuse_variables()
	a3 = tf.get_variable('a1')

with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	print a1.name
	print sess.run(a1)
	print a3.name
	print sess.run(a3)

输出:

V1/a1:0
[ 1.]
V1/a1:0
[ 1.]

分析:变量a1和a3一样的变量,名字和值都是一样的。

你可能感兴趣的:(tensorflow用法)