Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow运行时错误:试图使用关闭的会话_Python_Tensorflow_Session_Runtime - Fatal编程技术网

Python Tensorflow运行时错误:试图使用关闭的会话

Python Tensorflow运行时错误:试图使用关闭的会话,python,tensorflow,session,runtime,Python,Tensorflow,Session,Runtime,我正在尝试运行上发布的conviz.py代码。代码返回RuntimeError:试图使用关闭的会话 注意:我使用的是python 3.6和TensorFlow 1.13.1 我只是克隆了GitHub源代码,并对其进行了一些小的修改。e、 xrange和cross_熵部分中的g不兼容问题 下面是代码中似乎与错误相关的部分 with tf.Session() as sess: sess.run(init) step = 1 # Keep training until reac

我正在尝试运行上发布的conviz.py代码。代码返回RuntimeError:试图使用关闭的会话

注意:我使用的是python 3.6和TensorFlow 1.13.1

我只是克隆了GitHub源代码,并对其进行了一些小的修改。e、 xrange和cross_熵部分中的g不兼容问题

下面是代码中似乎与错误相关的部分

with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                       keep_prob: dropout})
        if step % display_step == 0:
            # Calculate batch loss and accuracy
            loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                              y: batch_y,
                                                              keep_prob: 1.})
            print("\rIter " + str(step*batch_size) + ", Minibatch Loss= " +
                  "{:.6f}".format(loss) + ", Training Accuracy= " +
                  "{:.5f}".format(acc), end='')
        step += 1
    print("\rOptimization Finished!")

# Calculate accuracy for 256 mnist test images
print("Testing Accuracy:",
      sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
                                    y: mnist.test.labels[:256],
                                    keep_prob: 1.}))

# no need for feed dictionary here
conv_weights = sess.run([tf.get_collection('conv_weights')])
print("conv_weights done!")
for i, c in enumerate(conv_weights[0]):
    plot_conv_weights(c, 'conv{}'.format(i))

您应该像下面这样更改代码,sess对象必须位于中,并使用tf.Session作为sess::

with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                   keep_prob: dropout})
        if step % display_step == 0:
            # Calculate batch loss and accuracy
            loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                          y: batch_y,
                                                          keep_prob: 1.})
            print("\rIter " + str(step*batch_size) + ", Minibatch Loss= " +
              "{:.6f}".format(loss) + ", Training Accuracy= " +
              "{:.5f}".format(acc), end='')
        step += 1
    print("\rOptimization Finished!")
    # Calculate accuracy for 256 mnist test images
    print("Testing Accuracy:",
    sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
                                y: mnist.test.labels[:256],
                                keep_prob: 1.}))
    # no need for feed dictionary here
    conv_weights = sess.run([tf.get_collection('conv_weights')])
    print("conv_weights done!")
    for i, c in enumerate(conv_weights[0]):
        plot_conv_weights(c, 'conv{}'.format(i))

在print语句中,在tf.session上下文之外使用会话。
with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                   keep_prob: dropout})
        if step % display_step == 0:
            # Calculate batch loss and accuracy
            loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                          y: batch_y,
                                                          keep_prob: 1.})
            print("\rIter " + str(step*batch_size) + ", Minibatch Loss= " +
              "{:.6f}".format(loss) + ", Training Accuracy= " +
              "{:.5f}".format(acc), end='')
        step += 1
    print("\rOptimization Finished!")
    # Calculate accuracy for 256 mnist test images
    print("Testing Accuracy:",
    sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
                                y: mnist.test.labels[:256],
                                keep_prob: 1.}))
    # no need for feed dictionary here
    conv_weights = sess.run([tf.get_collection('conv_weights')])
    print("conv_weights done!")
    for i, c in enumerate(conv_weights[0]):
        plot_conv_weights(c, 'conv{}'.format(i))