为什么我不能使用TensorFlow中的变量名称和范围路径访问我创建的变量?

时间:2016-07-01 00:51:40

标签: tensorflow

我试图获得一个我在一个简单的函数中创建的变量,但我一直在收到错误。我在做:

x = tf.get_variable('quadratic/x')

但是python抱怨如下:

python qm_tb_scopes.py
quadratic/x:0
Traceback (most recent call last):
  File "qm_tb_scopes.py", line 24, in <module>
    x = tf.get_variable('quadratic/x')
  File "/Users/my_username/path/tensor_flow_experiments/venv/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 732, in get_variable
    partitioner=partitioner, validate_shape=validate_shape)
  File "/Users/my_username/path/tensor_flow_experiments/venv/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 596, in get_variable
    partitioner=partitioner, validate_shape=validate_shape)
  File "/Users/my_username/path/tensor_flow_experiments/venv/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 161, in get_variable
    caching_device=caching_device, validate_shape=validate_shape)
  File "/Users/my_username/path/tensor_flow_experiments/venv/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 457, in _get_single_variable
    "but instead was %s." % (name, shape))
ValueError: Shape of a new variable (quadratic/x) must be fully defined, but instead was <unknown>.

它似乎试图创建一个新变量,但我只是想尝试定义一个。为什么要这样做?

整个代码是:

import tensorflow as tf

def get_quaratic():
    # x variable
    with tf.variable_scope('quadratic'):
        x = tf.Variable(10.0,name='x')
        # b placeholder (simualtes the "data" part of the training)
        b = tf.placeholder(tf.float32,name='b')
        # make model (1/2)(x-b)^2
        xx_b = 0.5*tf.pow(x-b,2)
        y=xx_b
        return y,x

y,x = get_quaratic()
learning_rate = 1.0
# get optimizer
opt = tf.train.GradientDescentOptimizer(learning_rate)
# gradient variable list = [ (gradient,variable) ]
print x.name
x = tf.get_variable('quadratic/x')
x = tf.get_variable(x.name)

2 个答案:

答案 0 :(得分:4)

如果要两次获取相同的变量,则需要将选项reuse=True传递给tf.variable_scope()。

参见文档(https://www.tensorflow.org/versions/r0.9/how_tos/variable_scope/index.html) 了解更多详情。

或者,您可以在Python函数之外获取一次变量,并将其作为Python中的参数传递。我发现它更清晰,因为它使得它明确了代码使用的变量。

我希望有所帮助!

答案 1 :(得分:0)

这不是最佳解决方案,但是尝试通过tf.get_variable()reuse=False来创建变量,以确保创建了新变量。然后,在获取变量时,将tf.get_variable()reuse=True一起使用以获取当前变量。如果没有确切的var,则将reuse设置为tf.AUTO_REUSE可能会创建新变量。还要确保在tf.get_variable()中指定变量的形状。

import tensorflow as tf

def get_quaratic():
    # x variable
    with tf.variable_scope('quadratic', reuse=False):
        x = tf.get_variable('x', ())
        tf.assign(x, 10)
        # b placeholder (simualtes the "data" part of the training)
        b = tf.placeholder(tf.float32,name='b')
        # make model (1/2)(x-b)^2
        xx_b = 0.5*tf.pow(x-b,2)
        y=xx_b
        return y,x
y,x = get_quaratic()
learning_rate = 1.0
# get optimizer
opt = tf.train.GradientDescentOptimizer(learning_rate)
# gradient variable list = [ (gradient,variable) ]
print (x.name)

with tf.variable_scope('', reuse=True):
    x = tf.get_variable('quadratic/x', shape=())
print(tf.global_variables())  # there is only 1 variable