使用tensorflow做实现是出现OutOfRangeError

Zxialuoke 2019-01-13 01:07:44
# -*- coding: utf-8 -*-
import tensorflow as tf
import os
#"占位符"
x=tf.placeholder(tf.float32,[None,28*28])
labels=tf.placeholder(tf.float32,[None,10])
#"------第一步:解析数据------"
nums=33#"所有训练样本的个数"
#"得到文件夹./data/下的所有tfRecord文件"
files=tf.train.match_filenames_once(os.path.curdir+
"/data/"+
"data*.tfrecord")
#"创建TFRecordReader对象"
num_epochs=1000
reader=tf.TFRecordReader()
records_queue=tf.train.string_input_producer(files,num_epochs=num_epochs)
_,serialized_example=reader.read(records_queue)
#"解析文件中的图像及其对应的标签"
features=tf.parse_single_example(
serialized_example,
features={
'img_raw':tf.FixedLenFeature([],tf.string),
'label':tf.FixedLenFeature([],tf.int64),
}
)

#"解码二进制数据"
img_raw=features['img_raw']
img_raw=tf.decode_raw(img_raw,tf.uint8)
img=tf.reshape(img_raw,[28*28])
img=tf.cast(img,tf.float32)
img=img/255.0
#"标签"
label=features['label']
label=tf.cast(label,tf.int64)
label_onehot=tf.one_hot(label,10,dtype=tf.float32)
#"每次从文件中读取3张图片"
BatchSize =3
imgs,labels_onehot=tf.train.shuffle_batch([img,label_onehot],
BatchSize,1000+3*BatchSize,1000)
#"------第2部分:构建全连接网络------"
#""输入层、隐含层、输出层的神经元个数""
I,H1,O=784,200,10
#"输入层到隐含层的权重矩阵和偏置"
w1=tf.Variable(tf.random_normal([I,H1],0,1,tf.float32),
dtype=tf.float32,name='w1')
b1=tf.Variable(tf.random_normal([H1],0,1,tf.float32),
dtype=tf.float32,name='b1')
#"隐含层的结果,采用 sigmoid 激活函数"
l1=tf.matmul(x,w1)+b1
sigma1=tf.nn.sigmoid(l1)
#"第2层隐含层到输出层的的权重矩阵和偏置"
w2=tf.Variable(tf.random_normal([H1,O],0,1,tf.float32),
dtype=tf.float32,name='w2')
b2=tf.Variable(tf.random_normal([O],0,1,tf.float32),
dtype=tf.float32,name='b2')
#"输出层的结果"
logits=tf.matmul(sigma1,w2)+b2
#"------第3部分:构造损失函数------"
loss=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(
labels=labels_onehot,logits=logits))
#"------第4部分:梯度下降------"
opti=tf.train.AdamOptimizer(0.001,0.9,0.999,1e-8).minimize(loss)
#"创建会话"
session=tf.Session()
session.run(tf.global_variables_initializer())
session.run(tf.local_variables_initializer())
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=session,coord=coord)
for i in range(num_epochs):
for n in range(int(nums/BatchSize)):
imgs_arr,lables_onehot_arr=session.run([imgs,labels_onehot])
session.run(opti,feed_dict={x:imgs_arr,labels:lables_onehot_arr})
coord.request_stop()
coord.join(threads)
session.close()

上面的代码是书上的列子,但是报错如下:
OutOfRangeError: RandomShuffleQueue '_49_shuffle_batch_8/random_shuffle_queue' is closed and has insufficient elements (requested 3, current size 1)
[[node shuffle_batch_8 (defined at D:/tensorflow_test/1_1.py:40) = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_8/random_shuffle_queue, shuffle_batch_8/n)]]

Caused by op 'shuffle_batch_8', defined at:
File "D:\Anaconda\envs\tensorflow\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "D:\Anaconda\envs\tensorflow\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder_kernels\console\__main__.py", line 11, in <module>
start.main()
File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder_kernels\console\start.py", line 310, in main
kernel.start()
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\kernelapp.py", line 505, in start
self.io_loop.start()
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\platform\asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "D:\Anaconda\envs\tensorflow\lib\asyncio\base_events.py", line 438, in run_forever
self._run_once()
File "D:\Anaconda\envs\tensorflow\lib\asyncio\base_events.py", line 1451, in _run_once
handle._run()
File "D:\Anaconda\envs\tensorflow\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\ioloop.py", line 758, in _run_callback
ret = callback()
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\gen.py", line 1233, in inner
self.run()
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\gen.py", line 1147, in run
yielded = self.gen.send(value)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 357, in process_one
yield gen.maybe_future(dispatch(*args))
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 267, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 534, in execute_request
user_expressions, allow_stdin,
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\ipkernel.py", line 294, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2819, in run_cell
raw_cell, store_history, silent, shell_futures)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2845, in _run_cell
return runner(coro)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\async_helpers.py", line 67, in _pseudo_sync_runner
coro.send(None)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3020, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3191, in run_ast_nodes
if (yield from self.run_code(code, result)):
File "D:\Anaconda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-f22c84be74c5>", line 1, in <module>
runfile('D:/tensorflow_test/1_1.py', wdir='D:/tensorflow_test')
File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/tensorflow_test/1_1.py", line 40, in <module>
BatchSize,1000+3*BatchSize,1000)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\util\deprecation.py", line 306, in new_func
return func(*args, **kwargs)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\training\input.py", line 1344, in shuffle_batch
name=name)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\training\input.py", line 871, in _shuffle_batch
dequeued = queue.dequeue_many(batch_size, name=name)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\data_flow_ops.py", line 478, in dequeue_many
self._queue_ref, n=n, component_types=self._dtypes, name=name)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py", line 3806, in queue_dequeue_many_v2
component_types=component_types, timeout_ms=timeout_ms, name=name)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
op_def=op_def)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()

OutOfRangeError (see above for traceback): RandomShuffleQueue '_49_shuffle_batch_8/random_shuffle_queue' is closed and has insufficient elements (requested 3, current size 1)
[[node shuffle_batch_8 (defined at D:/tensorflow_test/1_1.py:40) = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_8/random_shuffle_queue, shuffle_batch_8/n)]]

在网上找了很多类似的问题,但是始终没能解决!!因为是初学,还请大佬指教!谢谢了!
...全文
142 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧