Operating environment
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.
I'm learning a deep learning framework called TensorFlow.
https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/ I came across a description I've never seen in the first code of.
...
def data_iterator():
    """ A simple data iterator """
    batch_idx = 0
    while True:
        # shuffle labels and features
        idxs = np.arange(0, len(features))
        np.random.shuffle(idxs)
        shuf_features = features[idxs]
        shuf_labels = labels[idxs]
        batch_size = 128
        for batch_idx in range(0, len(features), batch_size):
            images_batch = shuf_features[batch_idx:batch_idx+batch_size] / 255.
            images_batch = images_batch.astype("float32")
            labels_batch = shuf_labels[batch_idx:batch_idx+batch_size]
            yield images_batch, labels_batch
iter_ = data_iterator()
while True:
    # get a batch of data
    images_batch_val, labels_batch_val = iter_.next()
    # pass it in as through feed_dict
    _, loss_val = sess.run([train_op, loss_mean], feed_dict={
                    images_batch:images_batch_val,
                    labels_batch:labels_batch_val
                    })
    print loss_val
What I didn't understand at a glance above was "Where is next () in iter_.next () defined?"
There is no definition of next () in the iter_ class data_iterator ().
On the other hand, there is a description of yield. I feel that this came out when I made the software with Unity. http://qiita.com/7of9/items/194e1c7f4b87a79dd129
A Qiita article was found by searching for next () and yield. http://qiita.com/tomotaka_ito/items/35f3eb108f587022fa09#yieldを使ったジェネレータの実装
After all it seems that it can be used in the same way as the yield used in Unity.
Recommended Posts