Readers are not supported when eager execution is enabled

本文详细记录了在使用TensorFlow 2.x框架进行数据读取时遇到的问题,即使用TFRecordReader读取TFRecord文件不再受支持。文中提供了错误信息并展示了如何通过禁用eager execution模式来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在复现tensorflow框架第二版代码的时候遇到了

Readers are not supported when eager execution is enabled. Instead, please use tf.data to get data into your model.

这个问题。

源码如下:

# 创建一个reader类读取TFRecord文件中的样例
reader = tf.TFRecordReader()

#创建一个队列来维护输入文件列表
filename_queue = tf.train.string_input_producer(["F:/anaconda/workspace/Data/output.tfrecords"])

# 从文件中读出一个样例,也可以用read_up_to函数一次性读取多个样例
_,serialized_example = reader.read(filename_queue)
#解析读入的一个样例,如果需要解析多个样例,也可以用parse_example函数
features = tf.parse_single_example(
           serialized_example,
           features = {
               'image_raw': tf.FixedLenFeature([],tf.string),
               'pixels': tf.FixedLenFeature([],tf.int64),
               'label': tf.FixedLenFeature([],tf.int64),
            })

# tf.decode_raw可以将字符串解析成图像对应的像素数组
image = tf.decode_raw(features['image_raw'],tf.uint8)
label = tf.cast(features['label'],tf.int32)
pixels = tf.cast(features['pixels'],tf.int32)

sess = tf.Session()
#启动多线程处理输入数据
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess = sess, coord = coord)

# 每次运行可读取TFReccord文件中的一个样例。当所有的样例都读完之后,在此样例中程序会再重头读取
for i in range(10):
    print(sess.run([image,label,pixels]))

看似毫无问题,但是运行起来就报错。

根据报错信息改成tf.data还是报错,后来没办法就在代码开头加了一句:

tf.compat.v1.disable_eager_execution() 

完美解决

 

多内容下载机器学习资料请扫描下方二维码关注小编公众号:程序员大管

 

 

这个错误通常是由于你的TensorFlow版本启用了Eager Execution(即立即执行模式),而在Eager Execution模式下,无法导出/导入Meta Graph。 解决这个问题的方法之一是禁用Eager Execution。你可以使用以下代码在TensorFlow 2.0中禁用Eager Execution: ```python import tensorflow as tf tf.compat.v1.disable_eager_execution() ``` 如果你正在使用TensorFlow 1.x,则可以按照以下方式禁用Eager Execution: ```python import tensorflow as tf tf.compat.v1.disable_eager_execution() ``` 如果你需要使用Eager Execution,但又需要导出/导入Meta Graph,则可以使用`tf.compat.v1.Session()`来创建一个不启用Eager Execution的会话。例如,你可以按照以下方式导出Meta Graph: ```python import tensorflow as tf # 构建计算图 x = tf.placeholder(tf.float32, shape=[None, 784], name='x') y = tf.placeholder(tf.float32, shape=[None, 10], name='y') W = tf.Variable(tf.zeros([784, 10]), name='W') b = tf.Variable(tf.zeros([10]), name='b') logits = tf.matmul(x, W) + b loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)) train_op = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # 创建会话并导出Meta Graph with tf.compat.v1.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) saver = tf.compat.v1.train.Saver() saver.save(sess, 'my-model', global_step=0, write_meta_graph=True) ``` 在这个示例中,我们使用`tf.compat.v1.Session()`创建了一个不启用Eager Execution的会话,并使用`saver.save()`方法导出了Meta Graph,使得我们可以在其他地方导入该图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值