Created
February 16, 2022 10:05
-
-
Save ZekunZh/7b2dac1df61f1ba1f4262ab4ceabee5d to your computer and use it in GitHub Desktop.
Revisions
-
ZekunZh created this gist
Feb 16, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ def iter_batches(iterable, batch_size): """Iterates over the given iterable in batches. Args: iterable: an iterable batch_size: the desired batch size, or None to return the contents in a single batch Returns: a generator that emits tuples of elements of the requested batch size from the input """ it = iter(iterable) while True: chunk = tuple(itertools.islice(it, batch_size)) if not chunk: return yield chunk