Jupyter 中的张量流:变量已经存在



这是来自"Hands-On Machine Learning with Scikit Learn and Tensorflow"示例的部分神经网络代码,该代码按预期工作:

with tf.name_scope("dnn"):
hidden1 = fully_connected(X, n_hidden1, activation_fn=leaky_relu, scope="hidden1")
hidden2 = fully_connected(hidden1, n_hidden2, activation_fn=leaky_relu, scope="hidden2")
logits = fully_connected(hidden3, n_outputs, activation_fn=None, scope="outputs")
with tf.name_scope("loss"):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,
logits=logits)
loss = tf.reduce_mean(xentropy, name="loss")

但是,如果我只是重新运行 Jupyter 单元,则会出现以下错误。这意味着我无法进行更改,例如添加另一个隐藏层。

ValueError: Variable hidden1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

我尝试了tf.reset_default_graph((,但只有重置整个Jupyter笔记本才能解决此问题。对于此错误,我可以对神经网络进行更改的最佳解决方法是什么?

您是否尝试过简单地为每个层设置 reuse=True? 重用:布尔值,是否重用同名的前一个图层的权重。 https://www.tensorflow.org/api_docs/python/tf/layers/dense

对我来说似乎是一个有效的选择。

最新更新