Implementing a generator using Python> link> yield and next ()> yield

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

Implementing a generator using Python> link> yield and next ()> yield
Create a web map using Python and GDAL
Create a Mac app using py2app and Python3! !!
Try creating a compressed file using Python and zlib
Derivatives learned using Python-(2) Draw a yield curve (JPYLibor curve)-
Flatten using Python yield from
Building a Python environment on a Mac and using Jupyter lab
Shoot time-lapse from a PC camera using Python and OpenCV
I made a Chatbot using LINE Messaging API and Python
Python a + = b and a = a + b are different
Python tuple comprehensions and generator expressions
I made a Line-bot using Python!
Authentication using tweepy-User authentication and application authentication (Python)
Build a game leaderboard on Alibaba cloud using Python and Redis
Drawing a silverstone curve using python
Process Splunk execution results using Python and save to a file
A little more about references ~ Using Python and Java as examples ~
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Clustering and visualization using Python and CytoScape
Precautions when creating a Python generator
Implementing a simple algorithm in Python 2
[Python] return A [or / and] B
[Python] Read a csv file with a large data size using a generator
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
I tried to create a sample to access Salesforce using Python and Bottle
I want to make a web application using React and Python flask
From Python to using MeCab (and CaboCha)
python> link> strftime () and strptime () behavior / code
Using Python and MeCab with Azure Databricks
Python comprehension (list and generator expressions) [additional]
Python> link> 2D array initialization and assignment
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
[Python] Create a Batch environment using AWS-CDK
Scraping a website using JavaScript in Python
[Python] Scraping a table using Beautiful Soup
A story about Python pop and append
Draw a tree in Python 3 using graphviz
A program that plays rock-paper-scissors using Python
I'm using tox and Python 3.3 with Travis-CI
Reinforcement learning 35 python Local development, paste a link to myModule and import it.
[Python] Smasher tried to make the video loading process a function using a generator
A complete guidebook to using pyenv, pip and python in an offline environment
[Python] A program that finds the minimum and maximum values without using methods
The return value (generator) of a function that combines finally and yield must not be passed directly to next
Organize python modules and packages in a mess
MessagePack-Try to link Java and Python with RPC
Create a GIF file using Pillow in Python
Head orientation estimation using Python and OpenCV + dlib
I tried web scraping using python and selenium
[Python] Split a large Flask file using Blueprint
Notes on installing Python3 and using pip on Windows7
Python development flow using Poetry, Git and Docker
I tried object detection using Python and OpenCV
A nice nimporter that connects nim and python
ffmpeg-Build a python environment and split the video
I wrote a class in Python3 and Java
Why I'm a Java shop and start Python
View drug reviews using a list in Python