Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow tf.layers、tf.contrib.layers不使用可变范围_Python_Tensorflow - Fatal编程技术网

Python Tensorflow tf.layers、tf.contrib.layers不使用可变范围

Python Tensorflow tf.layers、tf.contrib.layers不使用可变范围,python,tensorflow,Python,Tensorflow,我开始使用TensorFlow进行一些简单的Q学习,但在尝试使用变量作用域和使用tf.layers和tf.contrib.layers构建的层时遇到了麻烦。简而言之,我想将相同的层应用于不同的输入张量(例如,保持当前和下一个Q值)。下面是一个使用tf.layers的最小示例: import tensorflow as tf inp1 = tf.placeholder(tf.float64, (4,1)) inp2 = tf.placeholder(tf.float64, (4,1)) def

我开始使用TensorFlow进行一些简单的Q学习,但在尝试使用变量作用域和使用
tf.layers
tf.contrib.layers
构建的层时遇到了麻烦。简而言之,我想将相同的层应用于不同的输入张量(例如,保持当前和下一个Q值)。下面是一个使用
tf.layers
的最小示例:

import tensorflow as tf

inp1 = tf.placeholder(tf.float64, (4,1))
inp2 = tf.placeholder(tf.float64, (4,1))

def process(inp):
    with tf.variable_scope("foo", reuse=True):
        return tf.layers.dense(inp, 12, name="bar", reuse=True)

process(inp1)
process(inp2)
尝试执行此代码会出现以下异常:

ValueError: Variable foo/bar/kernel does not exist, or was not created with
tf.get_variable(). Did you mean to set reuse=None in VarScope?
ValueError: Variable foo/bar/kernel already exists, disallowed. 
Did you mean to set reuse=True in VarScope?
我知道在
tf.layers.dense()
中设置
reuse=True
会使它试图找到一个已经定义的层,但可能无法找到。但是如果我将调用更改为
tf.layers.dense(inp,12,name=“bar”)
,那么它将失败,并出现相同的异常

如果我在
tf.variable\u scope()
中设置了
reuse=None
,则后一个版本在调用
进程(inp2)
时失败,例外情况是:

ValueError: Variable foo/bar/kernel does not exist, or was not created with
tf.get_variable(). Did you mean to set reuse=None in VarScope?
ValueError: Variable foo/bar/kernel already exists, disallowed. 
Did you mean to set reuse=True in VarScope?
不幸的是,使用
tf.contrib.layers
时也会出现类似的错误

我的问题是:有没有办法让
tf.layers
使用可变范围?我知道我可以分别定义权重和偏差,但最好保留
tf.layers
给出的抽象。非常感谢

我的安装程序是TensorFlow 1.3.0(CPU),在Windows 10上运行Python 3.6.1(通过64位Anaconda 4.4.0上的pip安装)


另外,我在的第17页发现了层变量作用域的使用。

两个错误是不同的:第一个错误发生在
过程(inp1)
中,它试图找到存在的变量,但没有;第二个发生在
进程(inp2)
中,其中存在同名变量,但它尝试创建同名的新变量,这是不允许的

我猜你想在Q学习中重用这些变量。因此,解决方案非常简单:第一次定义这些变量时,不要使用
reuse
,然后可以设置
reuse=True

在你的演讲中,我想他们之前已经定义了变量


这将帮助您了解更多信息。

两个错误是不同的:第一个错误发生在
过程(inp1)
中,它试图找到存在的变量,但没有;第二个发生在
进程(inp2)
中,其中存在同名变量,但它尝试创建同名的新变量,这是不允许的

我猜你想在Q学习中重用这些变量。因此,解决方案非常简单:第一次定义这些变量时,不要使用
reuse
,然后可以设置
reuse=True

在你的演讲中,我想他们之前已经定义了变量


这将帮助您了解更多信息。

Tensorflow现在似乎有了一个新功能:tf.AUTO\u重用。这可以用来代替“reuse=”的True和False,它完全实现了您之前所说的:第一次创建变量,然后再重用它们。Tensorflow现在似乎有了一个新功能:tf.AUTO_reuse。对于“reuse=”可以使用它来代替True和False,它完全实现了您之前所说的:第一次创建变量,然后再重用它们。