チャンク分割用の関数を定義する
アップサートを再現可能な方法でバッチ処理するために、ベクターのリストをチャンクに分割する関数を定義します。
組み込みの itertools モジュールはすでにインポート済みです。
この演習はコースの一部です
Pineconeを使ったAIアプリケーション開発
演習の手順
- 入力の
iterableをイテレータに変換します。 itertoolsモジュールを使って、itをサイズbatch_sizeのチャンクにスライスします。- 現在のチャンクを
yieldします。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def chunks(iterable, batch_size=100):
"""A helper function to break an iterable into chunks of size batch_size."""
# Convert the iterable into an iterator
it = ____
# Slice the iterator into chunks of size batch_size
chunk = tuple(itertools.____(it, ____))
while chunk:
# Yield the chunk
____
chunk = tuple(itertools.islice(it, batch_size))