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

标签 graph tensorflow reset

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

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

a.开始 session

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)

但是,我遇到了一个异常(exception)(见下文)。所以我的问题是:是否可以重置/删除图表并像以前一样重新创建它?如果是这样,怎么办?

感谢任何指点。

TIA,

引用文献

  1. Change initializer of Variable in Tensorflow
  2. Remove nodes from graph or reset entire default graph

异常

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

关于graph - 重置默认图表不会删除变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39352865/

相关文章:

css - 如何重置 CSS3 *-transform : translate(…)?

json - 我希望 Grafana 计算成功与失败 HTTP 响应的比率,并将这两个指标绘制在单个图表中。我怎样才能实现这个目标?

r - 为什么networkd3 R中的forceNetwork显示不在边缘列表中的所有节点?

algorithm - 模糊图比较

cuda - Tensorflow无法打开libcuda.so.1

sql-server - Transact-SQL - 在满足条件之前对行进行编号

python - matplotlib 在空白处写入文本

python-3.x - 为什么 Tensorflow-gpu 还在使用 cpu

python - Tensorflow对象检测api获取数组中的标签

java - 对数组进行排序,差不多完成了,只需要重置最小值