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

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 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗