How to use two nets in TFlearn?
<p>I am trying to train DQN to play Tic-Tac-Toe. I trained it to play X (while O moves are random). After 12h of training it plays ok, but not flawless. Now I want to train two nets simultaneously - one for X moves and one for O moves.
But when I try to do model.predict(state) on second network, I get errors like:</p>

<pre><code>ValueError: Cannot feed value of shape (9,) for Tensor 'InputData/X:0', which has shape '(?, 9)'
</code></pre>

<p>But I know for shure that network definitions and data dimensions are identical. There is something with defining two DNNs.</p>

<p>Here is a generic example:</p>

<pre><code>import tflearn
import random

X = [[random.random(),random.random()] for x in range(1000)]
#reverse values order like [1,0] -&gt; [0,1]
Y = [[x[1],x[0]] for x in X]

n = tflearn.input_data(shape=[None,2])
n = tflearn.fully_connected(n, 2)
n = tflearn.regression(n)
m = tflearn.DNN(n)

m.fit(X, Y, n_epoch = 20)
#should print like [0.1,0.9]
print(m.predict([[0.9,0.1]]))

n2 = tflearn.input_data(shape=[None,2])
n2 = tflearn.fully_connected(n2, 2)
n2 = tflearn.regression(n2)
m2 = tflearn.DNN(n2)

# set second element value to first e.g. [1,0] -&gt; [1,1]
Y = [[x[0],x[0]] for x in X]

m2.fit(X, Y, n_epoch = 20)
#should print like [0.9,0.9]
print(m2.predict([[0.9,0.1]]))
</code></pre>

<p>Error will be like:</p>

<pre><code>Traceback (most recent call last):
  File "2_dnn_test.py", line 25, in &lt;module&gt;
    m2.fit(X, Y, n_epoch = 20)
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tflearn/models/dnn.py", line 157, in fit
    self.targets)
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tflearn/utils.py", line 267, in feed_dict_builder
    feed_dict[net_inputs[i]] = x
IndexError: list index out of range
</code></pre>

<p>Error is different because in my tic-tac-toe I call predict on second DNN sooner than do first fit(). If I comment out <code>m2.fit(X, Y, n_epoch = 20)</code> in my example I get same error:</p>

<pre><code>Traceback (most recent call last):
  File "2_dnn_test.py", line 27, in &lt;module&gt;
    print(m2.predict([[0.9,0.1]]))
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tflearn/models/dnn.py", line 204, in predict
    return self.predictor.predict(feed_dict)
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tflearn/helpers/evaluator.py", line 69, in predict
    o_pred = self.session.run(output, feed_dict=feed_dict).tolist()
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 372, in run
    run_metadata_ptr)
  File "/home/cpro/.pyenv/versions/3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 625, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2,) for Tensor 'InputData/X:0', which has shape '(?, 2)'
</code></pre>

<p>So two identical networks do not work at the same time. How do I make both of them work?</p>

<p>BTW example does not get expected prediction result :)</p>

<p>Looks like I should add</p>

<pre><code>with tf.Graph().as_default():
    #define model here
</code></pre>

<p>to prevent TFLearn to append both models to default graph. With this addition everything works.</p>

python machine-learning tensorflow