重置默认图表不会删除变量

Resetting default graph does not remove variables

我正在寻找一种在 Jupyter 的交互式会话中快速更改图形以测试不同结构的方法。最初我想简单地删除现有变量并使用不同的初始化程序重新创建它们。这似乎不可能 [1].

然后我找到了 [2],现在正试图简单地丢弃并重新创建默认图表。但这似乎不起作用。这就是我所做的:

一个。开始会话

import tensorflow as tf
import math

sess = tf.InteractiveSession()

b。在默认图中创建一个变量

IMAGE_PIXELS = 32 * 32
HIDDEN1 = 200

BATCH_SIZE = 100
NUM_POINTS = 30

images_placeholder = tf.placeholder(tf.float32, shape=(BATCH_SIZE, IMAGE_PIXELS))
points_placeholder = tf.placeholder(tf.float32,   shape=(BATCH_SIZE, NUM_POINTS))


# Hidden 1
with tf.name_scope('hidden1'):
  weights_init = tf.truncated_normal([IMAGE_PIXELS, HIDDEN1], stddev=1.0 / math.sqrt(float(IMAGE_PIXELS)))
  weights      = tf.Variable(weights_init, name='weights')
  biases_init  = tf.zeros([HIDDEN1])
  biases       = tf.Variable(biases_init, name='biases')
  hidden1      = tf.nn.relu(tf.matmul(images_placeholder, weights) + biases)

c。使用变量

# Add the variable initializer Op.
init = tf.initialize_all_variables()

# Run the Op to initialize the variables.
sess.run(init) 

d。重置图表

tf.reset_default_graph()

e。重新创建变量

with tf.name_scope('hidden1'):
  weights      = tf.get_variable(name='weights', shape=[IMAGE_PIXELS, HIDDEN1], 
                                 initializer=tf.contrib.layers.xavier_initializer())
  biases_init  = tf.zeros([HIDDEN1])
  biases       = tf.Variable(biases_init, name='biases')
  hidden1      = tf.nn.relu(tf.matmul(images_placeholder, weights) + biases)

但是,我得到一个例外(见下文)。所以我的问题是:是否可以 reset/remove 图表并像以前一样重新创建它?如果是这样,如何?

感谢任何指点。

TIA,

参考文献

异常

ValueError                                Traceback (most recent call last)
<ipython-input-5-e98a82c45473> in <module>()
      5   biases_init  = tf.zeros([HIDDEN1])
      6   biases       = tf.Variable(biases_init, name='biases')
----> 7   hidden1      = tf.nn.relu(tf.matmul(images_placeholder, weights) + biases)
  8 

/home/hmf/my_py3/lib/python3.4/site-packages/tensorflow/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, a_is_sparse, b_is_sparse, name)
   1323     A `Tensor` of the same type as `a`.
   1324   """
-> 1325   with ops.op_scope([a, b], name, "MatMul") as name:
   1326     a = ops.convert_to_tensor(a, name="a")
   1327     b = ops.convert_to_tensor(b, name="b")

/usr/lib/python3.4/contextlib.py in __enter__(self)
     57     def __enter__(self):
     58         try:
 ---> 59             return next(self.gen)
     60         except StopIteration:
     61             raise RuntimeError("generator didn't yield") from None

/home/hmf/my_py3/lib/python3.4/site-packages/tensorflow/python/framework/ops.py in op_scope(values, name, default_name)
   4014     ValueError: if neither `name` nor `default_name` is provided.
   4015   """
-> 4016   g = _get_graph_from_inputs(values)
   4017   n = default_name if name is None else name
   4018   if n is None:

/home/hmf/my_py3/lib/python3.4/site-packages/tensorflow/python/framework/ops.py in _get_graph_from_inputs(op_input_list, graph)
   3812         graph = graph_element.graph
   3813       elif original_graph_element is not None:
-> 3814         _assert_same_graph(original_graph_element, graph_element)
   3815       elif graph_element.graph is not graph:
   3816         raise ValueError(

/home/hmf/my_py3/lib/python3.4/site-packages/tensorflow/python/framework/ops.py in _assert_same_graph(original_item, item)
   3757   if original_item.graph is not item.graph:
   3758     raise ValueError(
-> 3759         "%s must be from the same graph as %s." % (item, original_item))
   3760 
   3761 

ValueError: Tensor("weights:0", shape=(1024, 200), dtype=float32_ref) must be from the same graph as Tensor("Placeholder:0", shape=(100, 1024), dtype=float32).`

重置默认图时,不会删除之前创建的张量。调用 tf.reset_default_graph() 时,会创建一个新图形并将其设置为默认值。

这里举例说明:

x = tf.constant(1)
print tf.get_default_graph() == x.graph  # prints True

tf.reset_default_graph()
print tf.get_default_graph() == x.graph  # prints False

您遇到的错误表明两个张量必须来自同一个图,这意味着您仍在使用前一个图和当前默认图的一些张量。

简单的解决方法是再次创建两个占位符 images_placeholderpoints_placeholder