Feenaboccles
Feenaboccles

Reputation: 412

TensorFlow/Python: "Nesting violated for default stack" error with custom graph

I get a strange error regarding stacks when I try to construct a simple expression in a custom, named graph.

The following code works fine:

tf.reset_default_graph()

# The basic model
X = tf.placeholder(tf.float32, [None, MnistDim], "X")
W = tf.get_variable(
        name="W", 
        shape=[MnistDim, DigitCount], 
        dtype=np.float32,
        initializer=tf.zeros_initializer()
)
b = tf.get_variable(
        name="b", 
        shape=[DigitCount], 
        dtype=np.float32,
        initializer=tf.zeros_initializer()
)

a = tf.matmul(X, W, name="a") + b
y = tf.nn.softmax (a, name="y")

# The training elements  
t = tf.placeholder (tf.float32, [None, 10], "t")
cross_entropy = tf.reduce_mean(-tf.reduce_sum(t * tf.log(y), reduction_indices=[1]))
# I know about tf.nn.softmax_cross_entropy_with_logits(a)

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

However if I put that code in a custom graph by prepending the following:

mnist_train_graph = tf.Graph()
with mnist_train_graph.as_default():
    tf.reset_default_graph()

    # The basic model
    X = tf.placeholder(tf.float32, [None, MnistDim], "X")

    etc. 
    etc.

    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

Then I get the following error.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-23-13b0e3c5f232> in <module>()
     30     # More stable to use the following
     31     # tf.nn.softmax_cross_entropy_with_logits(a)
---> 32     train_step =     tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     87         if type is None:
     88             try:
---> 89                 next(self.gen)
     90             except StopIteration:
     91                 return

    /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in get_controller(self, default)
   3626     finally:
   3627       if self._enforce_nesting:
-> 3628         if self.stack[-1] is not default:
   3629           raise AssertionError(
   3630               "Nesting violated for default stack of %s objects"

IndexError: list index out of range

Can anyone explain this? I'm using tensorflow-gpu version 1.1.0 on Python 3.6, installed using Pip, on Mac OS 10.12. I'm running it within a Jupyter session.

Thanks

Upvotes: 0

Views: 3462

Answers (1)

Feenaboccles
Feenaboccles

Reputation: 412

So I kept on looking and 30mins after posting this I found the answer (typical!). Essentially it's not safe to use tf.reset_default_graph() with a custom graph as_default(). Removing it fixes the issue. More details here: http://github.com/tensorflow/tensorflow/issues/11121

Upvotes: 5

Related Questions