TensorFlow > tensorflow.python.framework.errors.OutOfRangeError: RandomShuffleQueue '_3_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)

### TensorFlow

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
```

### code

実装途中。
input:100, output:100のノード数での学習予定。

```learn_in100out100.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

'''
v0.1 Feb. 06, 2017
    - read [test_in.csv],[test_out.csv]
'''

'''
codingrule:PEP8
'''

filename_inp = tf.train.string_input_producer(["test_in.csv"])
filename_out = tf.train.string_input_producer(["test_out.csv"])
NUM_INP_NODE = 100
NUM_OUT_NODE = 100

# parse csv
# a. input node
reader = tf.TextLineReader()
key, value = reader.read(filename_inp)
deflist = [[0.] for idx in range(NUM_INP_NODE)]
input1 = tf.decode_csv(value, record_defaults=deflist)
# b. output node
key, value = reader.read(filename_out)
deflist = [[0.] for idx in range(NUM_OUT_NODE)]
output1 = tf.decode_csv(value, record_defaults=deflist)
# c. pack
inputs = tf.pack([input1])
outputs = tf.pack([output1])

batch_size = 4
inputs_batch, output_batch = tf.train.shuffle_batch([inputs, outputs], batch_size, capacity=40, min_after_dequeue=batch_size)

input_ph = tf.placeholder("float", [None, 1])
output_ph = tf.placeholder("float", [None, 1])

# network
hiddens = slim.stack(input_ph, slim.fully_connected, [7,7,7],
    activation_fn=tf.nn.sigmoid, scope="hidden")
prediction = slim.fully_connected(hiddens, 1, activation_fn=None, scope="output")
loss = tf.contrib.losses.mean_squared_error(prediction, output_ph)

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        sess.run(init_op)
        for idx in range(10):
            inpbt, outbt = sess.run([inputs_batch, output_batch])
            _, t_loss = sess.run([train_op, loss], feed_dict={input_ph:inpbt, output_ph:outbt})

            if (idx+1) % 100 == 0:
            	print("%d,%f" % (idx+1, t_loss))
    finally:
        coord.request_stop()

```

### エラー

実行時に以下のエラーが出た。

```
Traceback (most recent call last):
  File "learn_in100out100.py", line 60, in <module>
    inpbt, outbt = sess.run([inputs_batch, output_batch])
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.OutOfRangeError: RandomShuffleQueue '_3_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
         [[Node: shuffle_batch = QueueDequeueMany[_class=["loc:@shuffle_batch/random_shuffle_queue"], component_types=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]

Caused by op u'shuffle_batch', defined at:
  File "learn_in100out100.py", line 38, in <module>
    inputs_batch, output_batch = tf.train.shuffle_batch([inputs, outputs], batch_size, capacity=40, min_after_dequeue=batch_size)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 819, in shuffle_batch
    dequeued = queue.dequeue_many(batch_size, name=name)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py", line 458, in dequeue_many
    self._queue_ref, n=n, component_types=self._dtypes, name=name)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 905, in _queue_dequeue_many
    timeout_ms=timeout_ms, name=name)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

OutOfRangeError (see above for traceback): RandomShuffleQueue '_3_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
         [[Node: shuffle_batch = QueueDequeueMany[_class=["loc:@shuffle_batch/random_shuffle_queue"], component_types=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
```


### 参考と対処

参考 http://stackoverflow.com/questions/34050071/tensorflow-random-shuffle-queue-is-closed-and-has-insufficient-elements

> was caused by the queue running out of data. This is often caused by thinking you have enough data for N iterations when really you only have enough for M iterations where M < N.

今回は100パターンしか用意していないのだが、

- batch_size = 4
- capacity=40
- for idx in range(10):

などの処理でパターン数100を超えたのが原因のようだ。

- batch_size = 1
- capacity=1

のように変更したところ、該当エラーは出なくなった。

```
$ python learn_in100out100.py 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
Traceback (most recent call last):
  File "learn_in100out100.py", line 38, in <module>
    inputs_batch, output_batch = tf.train.shuffle_batch([inputs, outputs], batch_size, capacity=1, min_after_dequeue=batch_size)
  File "/home/yasokada/tensorflow-GPU/local/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 808, in shuffle_batch
    (1. / (capacity - min_after_dequeue)))
ZeroDivisionError: float division by zero
```

最後のエラーは別途要検討。

バッチサイズと学習パターン数などは今後検討することになる。