为什么我不能访问我在 TensorFlow 中使用变量名加上作用域路径创建的变量? [英] Why can't I access the variable I create using the variable name plus scope path in TensorFlow?

查看:30
本文介绍了为什么我不能访问我在 TensorFlow 中使用变量名加上作用域路径创建的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取我在一个简单函数中创建的变量,但我不断收到错误消息.我在做:

I was trying to get a variable I created in a simple function but I keep getting errors. I am doing:

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

但蟒蛇抱怨如下:

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>.

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

it seems its trying to create a new variable, but I am simply trying to get a defined one. Why is it doing this?

整个代码是:

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)

推荐答案

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

You need to pass the option reuse=True to tf.variable_scope() if you want to get the same variable twice.

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

See the documentation (https://www.tensorflow.org/versions/r0.9/how_tos/variable_scope/index.html) for more details.

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

Alternatively, you could get the variable once, outside your Python function, and pass it in as a argument in Python. I find that a bit cleaner since it makes it explicit what variables the code uses.

希望能帮到你!

这篇关于为什么我不能访问我在 TensorFlow 中使用变量名加上作用域路径创建的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆