gpt4 book ai didi

python - Tensorflow,程序卡在 sess.run( ) 函数上

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:42 25 4
gpt4 key购买 nike

我是 Tensorflow 新手,遇到了一个问题。当我的程序到达 x_batch = sess.run(X_mb) 时,它被卡住了(因此,它可以打印 1 和 2,但无法打印 4。我猜这是一个无限循环)。我打印 X_mb 的值,结果是 Tensor("batch:0", shape=(32, 39), dtype=float32)。有人可以帮助我吗?谢谢!mb_size = 32,g 的形状为 [60366 , 39]。

更新的代码:

X = tf.placeholder(tf.float32, shape=[None, X_dim])

def sample_z(m, n):
return np.random.uniform(-1., 1., size=[m, n])

g = tf.unstack(data, num = 60366, axis = 0)
X_mb, *_ = tf.train.batch(g ,mb_size, capacity = 60366)
sess = tf.train.MonitoredSession()


i = 0

for it in range(2000):
#print(1)
for _ in range(5):
#print(2)

#print(X_mb)
x_batch = sess.run(X_mb)
# print(4)
_, D_loss_curr, _ = sess.run(
[D_solver, D_loss, clip_D],
feed_dict={X: x_batch, z: sample_z(mb_size, z_dim)}
)

_, G_loss_curr = sess.run(
[G_solver, G_loss],
feed_dict={z: sample_z(mb_size, z_dim)}
)

这是错误消息:

runfile('/Users/franklan123/.spyder-py3/temp.py', wdir='/Users/franklan123/.spyder-py3')
Traceback (most recent call last):

File "<ipython-input-4-6af95f06eb7f>", line 1, in <module>
runfile('/Users/franklan123/.spyder-py3/temp.py', wdir='/Users/franklan123/.spyder-py3')

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 688, in runfile
execfile(filename, namespace)

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/franklan123/.spyder-py3/temp.py", line 48, in <module>
X = tf.placeholder(tf.float32, shape=[None, X_dim])

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
name=name)

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2458, in create_op
self._check_not_finalized()

File "/Users/franklan123/anaconda/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2181, in _check_not_finalized
raise RuntimeError("Graph is finalized and cannot be modified.")

RuntimeError: Graph is finalized and cannot be modified.

最佳答案

没有无限循环,tf.train.batch 创建队列,您必须运行队列运行程序才能获取任何数据。否则,当您执行 sess.run 时,您的代码只会卡在queue.dequeue() 操作上(因为没有线程实际将数据放入队列中)。

简单的解决方案:

  1. 在 session 创建之前移动 X_mb 线(这会修改图表!)
  2. 将 tf.Session 更改为 tf.train.MonitoredSession (这将为您运行队列,否则您必须手动运行它们 + MonitoredSession 完成图表,这样您将来就可以避免像上面这样的错误)。完成此操作后 - 删除带有全局初始化程序的行(MonitoredSession 也会为您初始化)。

关于python - Tensorflow,程序卡在 sess.run( ) 函数上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com