我是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()操作上(因为实际上没有线程将数据放入队列中)。

简单的解决方案:


在会话创建之前将X_mb行移动(这修改了图形!)
将tf.Session更改为tf.train.MonitoredSession(它将为您运行队列,否则,您将不得不手动运行它们+ MonitoredSession最终确定图形,以便将来避免出现上述错误)。完成此操作后-删除带有全局初始化程序的行(MonitoredSession也会为您初始化)。

09-08 00:04