Reputation: 31
I meet a question about the while true loop in python. The code is below
def batched(iterator, batch_size):
while True:
data = np.zeros(batch_size)
target = np.zeros(batch_size)
for index in range(batch_size):
data[index], target[index] = next(iterator)
yield data, target
batches = batched(examples, params.batch_size)
for index, batch in enumerate(batches):
.......
.......
there is no break in the loop, and it will keep true forever , so how can this loop can stop. ps. the code is the examples in the book TensorFlow for Machine Intelligence .
Upvotes: 2
Views: 2169
Reputation: 399
I'm writing this answer because the comments section seemed a little too constricting.
First of all, reading up about Iterators
in python will clear up your confusion.
I'll try to explain anyways! :D
def batched(iterator, batch_size):
while True:
data = np.zeros(batch_size)
target = np.zeros(batch_size)
for index in range(batch_size):
data[index], target[index] = next(iterator)
yield data, target
batched
is a generator/iterator function. The key here is yield
. When the program flow hits yield
, the function "returns" the values (data, target
in our case). But the catch is, only when the function is called the next time, the flow continues from where it had yielded
the last time. In our case, the next time the function is called, the loop starts atop.
Now, as you might be misguided by the while True:
loop without a break
ing condition, batched
is not a blocking function call.
Also, do note that,
batches = batched(examples, params.batch_size)
is not a function call. You just get a pointer to the iterator when you execute the above line. The function runs only when it's iterated over!
For you, it's being done here:
for index, batch in enumerate(batches):
Here again, the for
loop would fall into the infinite loop if the terminator condition is not handled within the loop. Meaning, the loop needs to be terminated manually with a break
inside the for
loop.
Upvotes: 3
Reputation: 3535
This is a Python generator, it's not like a normal function that you'd call once, wait until it's finished and get the return value. It's used to generate data on the go, and these results come from yield
keyword.
So every time the infinite loop hits the yield keyword, it returns the current data and target, then it goes on. The simplest use of a generator is in a for loop:
for data, target in batched(iterator, batch_size):
# do Something
The while loop breaks, once this for loop breaks (not fetches any more data).
You'll find some examples here: https://wiki.python.org/moin/Generators#line-60
I couldn't find any example where they use an infinite loop inside the generator, but the concept is simple: your function can produce an infinite amount of results (could Fibonacci numbers), and it's handing over the control to the for loop which breaks if necessary.
Upvotes: 1