Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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:无法共享稠密/内核-值错误:尝试共享变量稠密/内核,但指定了形状(100160)和找到的形状(9100)_Python_Python 3.x_Tensorflow - Fatal编程技术网

Python Tensorflow:无法共享稠密/内核-值错误:尝试共享变量稠密/内核,但指定了形状(100160)和找到的形状(9100)

Python Tensorflow:无法共享稠密/内核-值错误:尝试共享变量稠密/内核,但指定了形状(100160)和找到的形状(9100),python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,我用Tensorflow建立了一个深度Q网络。当我尝试创建其中两个时(我希望网络与自身对抗),我得到: ValueError:尝试共享变量dense/kernel,但指定了形状 (100160)和找到的形状(9100) 这是我的网络: class QNetwork: """ A Q-Network implementation """ def __init__(self, input_size, output_size, hidden_layers_size, g

我用Tensorflow建立了一个深度Q网络。当我尝试创建其中两个时(我希望网络与自身对抗),我得到:

ValueError:尝试共享变量dense/kernel,但指定了形状 (100160)和找到的形状(9100)

这是我的网络:

class QNetwork:
    """
    A Q-Network implementation
    """
    def __init__(self, input_size, output_size, hidden_layers_size, gamma, maximize_entropy, reuse):
        self.q_target = tf.placeholder(shape=(None, output_size), dtype=tf.float32)
        self.r = tf.placeholder(shape=None, dtype=tf.float32)
        self.states = tf.placeholder(shape=(None, input_size), dtype=tf.float32)
        self.enumerated_actions = tf.placeholder(shape=(None, 2), dtype=tf.int32)
        self.learning_rate = tf.placeholder(shape=[], dtype=tf.float32)
        layer = self.states
        for l in hidden_layers_size:
            layer = tf.layers.dense(inputs=layer, units=l, activation=tf.nn.relu,
                                    kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                    reuse=reuse)
        self.output = tf.layers.dense(inputs=layer, units=output_size,
                                      kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                      reuse=reuse)
        self.predictions = tf.gather_nd(self.output, indices=self.enumerated_actions)
        if maximize_entropy:
            self.future_q = tf.log(tf.reduce_sum(tf.exp(self.q_target), axis=1))
        else:
            self.future_q = tf.reduce_max(self.q_target, axis=1)
        self.labels = self.r + (gamma * self.future_q)
        self.cost = tf.reduce_mean(tf.losses.mean_squared_error(labels=self.labels, predictions=self.predictions))
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.cost)
此代码失败:

q1 = QNetwork(9, 9, [100, 160, 160, 100], gamma=0.99, maximize_entropy=False, reuse=tf.AUTO_REUSE)
q2 = QNetwork(9, 9, [100, 160, 160, 100], gamma=0.99, maximize_entropy=False, reuse=tf.AUTO_REUSE)
你知道怎么解决这个问题吗?(运行TF1.10.1、Python3.6.5)已解决

我需要:

  • 为每个层指定一个唯一的名称
  • 使用
    reuse=tf.AUTO\u reuse
    (用于Adam优化器)将所有内容放在一个
    变量\u范围内