Reputation: 2893
I built a Deep-Q-Network using Tensorflow. When I try to create two of those (I would like to have the network playing against itself), I get:
ValueError: Trying to share variable dense/kernel, but specified shape (100, 160) and found shape (9, 100).
This is my network:
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)
And this code fails:
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)
Any idea how to solve this? (Running TF 1.10.1, Python 3.6.5)
Upvotes: 2
Views: 1735
Reputation: 2893
Solved.
I needed to:
variable_scope
with reuse=tf.AUTO_REUSE
(for the Adam optimizer)Upvotes: 3