【发布时间】:2017-04-13 19:25:11
【问题描述】:
tf.get_variable() 或tf.Variable() 生成的张量流变量似乎是全局变量。发生在我身上的情况如下:假设我制作了以下两个文件:
-
main.py
from prac_var import defineVar for i in range(1000): defineVar() -
prac_var.py
import tensorflow as tf def defineVar(): with tf.variable_scope('weight'): W = tf.Variable(tf.zeros([1,1]),name='W') print('\n',tf.trainable_variables())
现在如果我运行 main.py,它会产生
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_1/W:0' shape=(1, 1) dtype=float32_ref>]
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_1/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_2/W:0' shape=(1, 1) dtype=float32_ref>]
...
而我真正想要的是
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
[<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
...
我怎样才能以不平凡的方式解决这个问题?
【问题讨论】:
标签: python tensorflow