Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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/19.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
TensorFlow/Python:;默认堆栈的嵌套冲突;自定义图形错误_Python_Python 3.x_Tensorflow - Fatal编程技术网

TensorFlow/Python:;默认堆栈的嵌套冲突;自定义图形错误

TensorFlow/Python:;默认堆栈的嵌套冲突;自定义图形错误,python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,当我试图在一个自定义的命名图中构造一个简单的表达式时,我遇到了一个关于堆栈的奇怪错误 以下代码可以正常工作: 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.f

当我试图在一个自定义的命名图中构造一个简单的表达式时,我遇到了一个关于堆栈的奇怪错误

以下代码可以正常工作:

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)
但是,如果我通过在以下代码前面加上前缀,将该代码放入自定义图形中:

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)
然后我得到以下错误

---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在()
30#使用以下工具更稳定
31#tf.nn.softmax_cross_entropy_与_logits(a)
--->32序列步长=tf.序列梯度降阶器(0.5).最小化(交叉熵)
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in____________(self、type、value、traceback)
87如果类型为“无”:
88尝试:
--->89下一个(self.gen)
90除停止迭代外:
91返回
/get_controller中的opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/Python/framework/ops.py(self,默认)
3626最后:
3627如果自执行嵌套:
->3628如果self.stack[-1]不是默认值:
3629提出断言错误(
3630“违反了%s对象的默认堆栈嵌套”
索引器:列表索引超出范围
有人能解释一下吗?我在Python3.6上使用tensorflow gpu 1.1.0版,在Mac OS 10.12上使用Pip安装。我在Jupyter会话中运行它


谢谢

所以我继续查找,在发布此内容30分钟后我找到了答案(典型!)。本质上,使用自定义图形作为默认值()重置默认图()是不安全的。删除它可以解决问题。更多细节如下:

所以我继续查找,发布此内容30分钟后我找到了答案(典型!)。本质上,将自定义图形
重置为默认图形()
使用
tf.reset\u default\u graph()
是不安全的。删除它可以解决此问题。更多详细信息如下: