蒙蒙蒙王 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器