蒙蒙蒙王 2018-03-17 02:50 采纳率: 100%
浏览 2905
已采纳

TensorFlow中with as语句的使用问题

A session may own resources, such as variables, queues, and readers. It is important to release these resources when they are no longer required.

with as语句在资源不再使用后才能使用,但是我在mnist初步入门的那个代码中使用后却报了一大堆错误,这是为啥?

import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf

mnist = input_data.read_data_sets('MNIST_data/',one_hot = True)
#mnist.train
#mnist.test
#mnist.train.images [60000,784]
#mnist.train.labels [60000,10]

x = tf.placeholder(tf.float32,[None,784])

w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,w)+b)

y_ = tf.placeholder("float",[None,10])

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100)

sess.run(train_step,feed_dict = {x:batch_xs,y_:batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))

output = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
print(output)
sess.close()
_
斜体部分 我用with tf.Session() as sess:…………代替后报错了,
是不是因为之前用了sess = tf.Sesssion()的缘故??
还有 , 像在上面代码中多次用到sess.run(),是不是不能随便用with as语句了呢?

  • 写回答

1条回答

  • 梦之启航 2018-03-17 05:38
    关注

    是这样的,with tf.Session() as sess语句相当于在这段代码开始时执行sess=tf.Session(),在结束时执行sess.close()
    因此正确的写法应该是这样的

    import tensorflow.examples.tutorials.mnist.input_data as input_data
    import tensorflow as tf
    
    mnist = input_data.read_data_sets('MNIST_data/',one_hot = True)
    #mnist.train
    #mnist.test
    #mnist.train.images [60000,784]
    #mnist.train.labels [60000,10]
    
    x = tf.placeholder(tf.float32,[None,784])
    
    w = tf.Variable(tf.zeros([784,10]))
    b = tf.Variable(tf.zeros([10]))
    
    y = tf.nn.softmax(tf.matmul(x,w)+b)
    
    y_ = tf.placeholder("float",[None,10])
    
    cross_entropy = -tf.reduce_sum(y_*tf.log(y))
    
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
    
        for i in range(1000):
            batch_xs,batch_ys = mnist.train.next_batch(100)
            sess.run(train_step,feed_dict = {x:batch_xs,y_:batch_ys})
    
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
    
            # 另外,此处你的feed_dict写错了,应该为y_,而不是y
        output = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
        print(output)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?