始める無料で始める

チャンク分割用の関数を定義する

アップサートを再現可能な方法でバッチ処理するために、ベクターのリストをチャンクに分割する関数を定義します。

組み込みの 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))
コードを編集して実行