TensorFlow > TFRecords読込み版sine curveの学習 v0.1,v0.2 > OutOfRangeError | current size 0 > tf.initialize_local_variables()を実施する

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.1.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
```

[TensorFlow > sine curveの学習 > TensorFlowコードでpredictionをグラフ化してみた > sine curveになっていなかった > sine curveになった ( 誤差:0.01以下)](http://qiita.com/7of9/items/7e45a69c822900a80c67)
のTFRecords版を実装中。

### ファイル書出し処理

```prep_TFRecord_170708.py
import numpy as np
import random
import tensorflow as tf

"""
v0.1, Jul. 08, 2017
   - add _int64_feature()
   - add _bytes_feature()
"""

# on
#   Ubuntu 16.04 LTS
#   TensorFlow v1.1
#   Python 3.5.2

# codingrule: PEP8


def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


OUT_FILE = 'input_170708.tfrecords'
NUM_DATA = 100
#
x_data = np.random.rand(NUM_DATA)
y_data = np.sin(2*np.pi*x_data) + 0.3 * np.random.rand()

with tf.python_io.TFRecordWriter(OUT_FILE) as tf_writer:
    for xs, ys in zip(x_data, y_data):
        x_org = np.array(xs, dtype=np.float32)
        y_org = np.array(ys, dtype=np.float32)
        x_raw = x_org.tostring()
        y_raw = y_org.tostring()
        example = tf.train.Example(features=tf.train.Features(feature={
            'x_raw': _bytes_feature(x_raw),
            'y_raw': _bytes_feature(y_raw)}))
        tf_writer.write(example.SerializeToString())

for xs, ys in zip(x_data, y_data):
    print('%.5f, %.5f' % (xs, ys))

```

### テスト読込み処理

```test_readTFRecord_170708.py
import numpy as np
import tensorflow as tf

# codingrule: PEP8

INP_FILE = 'input_170708.tfrecords'

record_iterator = tf.python_io.tf_record_iterator(path=INP_FILE)

for record in record_iterator:
    example = tf.train.Example()
    example.ParseFromString(record)

    x_raw = (example.features.feature['x_raw']
             .bytes_list
             .value[0])
    x_1d = np.fromstring(x_raw, dtype=np.float32)
    x_org = x_1d.reshape([1, -1])

    y_raw = (example.features.feature['y_raw']
             .bytes_list
             .value[0])
    y_1d = np.fromstring(y_raw, dtype=np.float32)
    y_org = y_1d.reshape([1, -1])

    print(x_org, y_org)

```

```
$ python3 test_readTFRecord_170708.py | head
[[ 0.90197307]] [[-0.52444661]]
[[ 0.39993107]] [[ 0.64140004]]
[[ 0.75747746]] [[-0.94563216]]
[[ 0.16201401]] [[ 0.9043051]]
[[ 0.34640592]] [[ 0.8753475]]
[[ 0.45442739]] [[ 0.33570865]]
[[ 0.72813314]] [[-0.93731195]]
[[ 0.00909476]] [[ 0.11037734]]
[[ 0.50905269]] [[-0.00358463]]
[[ 0.36261058]] [[ 0.81321895]]
```

テスト読込みはできた。

## v0.1 

### sessionでの使用 > 失敗

```learn_sineCurve_170708.py
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim

"""
v0.1 Jul. 08, 2017
  - only read [.tfrecord]
    + add inputs_xy()
    + add read_and_decode()
"""

# codingrule: PEP8

IN_FILE = 'input_170708.tfrecords'


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
           'x_raw': tf.FixedLenFeature([], tf.string),
           'y_raw': tf.FixedLenFeature([], tf.string),
        })
    x_raw = tf.decode_raw(features['x_raw'], tf.float32)
    y_raw = tf.decode_raw(features['y_raw'], tf.float32)

    x_org = tf.reshape(x_raw, [1])
    y_org = tf.reshape(y_raw, [1])

    return x_org, y_org


def inputs_xy():
    filename = IN_FILE
    filequeue = tf.train.string_input_producer(
        [filename], num_epochs=1)

    x_org, y_org = read_and_decode(filequeue)
    return x_org, y_org

x_orgs, y_orgs = inputs_xy()
batch_size = 1  # 4
x_batch, y_batch = tf.train.shuffle_batch([x_orgs, y_orgs],
                                          batch_size,
                                          capacity=10,
                                          min_after_dequeue=7)
init_op = tf.initialize_all_variables()


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

    try:
        ax, ay = sess.run([x_batch, y_batch])
        print(ax, ay)
    finally:
        coord.request_stop()

```

以下のようなエラーが出る。

`
tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_0_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 1, current size 0)
	 [[Node: shuffle_batch = QueueDequeueManyV2[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)]]
`

current size 0となる理由について調べているが、今のところ、よくわからない。


## v0.2 > current size 0エラーの対処

### 変更箇所

[TensorFlow ファイルからの入力のいろいろ](http://qiita.com/learn_tensorflow/items/3e46b2512a1bab73f5b2)
を参考に以下の変更をした。

```
# init_op = tf.initialize_all_variables()
init_op = [tf.initialize_all_variables(), tf.initialize_local_variables()]
```

### code 

```learn_sineCurve_170708.py
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim

"""
v0.2 Jul. 08, 2017
  - fix bug > OutOfRangeError (current size 0)
    + use [tf.initialize_local_variables()]
v0.1 Jul. 08, 2017
  - only read [.tfrecord]
    + add inputs_xy()
    + add read_and_decode()
"""

# codingrule: PEP8

IN_FILE = 'input_170708.tfrecords'


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
           'x_raw': tf.FixedLenFeature([], tf.string),
           'y_raw': tf.FixedLenFeature([], tf.string),
        })
    x_raw = tf.decode_raw(features['x_raw'], tf.float32)
    y_raw = tf.decode_raw(features['y_raw'], tf.float32)

    x_org = tf.reshape(x_raw, [1])
    y_org = tf.reshape(y_raw, [1])

    return x_org, y_org


def inputs_xy():
    filename = IN_FILE
    filequeue = tf.train.string_input_producer(
        [filename], num_epochs=1)

    x_org, y_org = read_and_decode(filequeue)
    return x_org, y_org

x_orgs, y_orgs = inputs_xy()
batch_size = 1  # 4
x_batch, y_batch = tf.train.shuffle_batch([x_orgs, y_orgs],
                                          batch_size,
                                          capacity=10,
                                          min_after_dequeue=7)
# init_op = tf.initialize_all_variables()
init_op = [tf.initialize_all_variables(), tf.initialize_local_variables()]

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

    try:
        ax, ay = sess.run([x_batch, y_batch])
        print(ax, ay)
    finally:
        coord.request_stop()

```

### 実行

```
$ python3 learn_sineCurve_170708.py 
...
2017-07-08 11:54:04.261826: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-07-08 11:54:04.261844: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-07-08 11:54:04.261853: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0)
[[ 0.39993107]] [[ 0.64140004]]
```

current size 0エラーが出なくなり、ax, ayの値がprintされるようになりました。

@learn_tensorflow さん
情報感謝です。

別途、参考にしていた[fully_connected_reader.py](https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py)においてもlocal_variablesの初期化をしていたようだ。

```
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
```