class_docstr: This BatchSampler batches examples together by grouping them by their length.
  
  Every example in the batch have approximately the same length and
  thus padding is minimized.
  This enables faster training on datasets
  where length of examples can vary significantly (e.g Librispeech).
  Inspired by: https://www.tensorflow.org/api_docs/python/tf/data/experimental/bucket_by_sequence_length
  
  Dynamic batching is performed by specifying a max_batch_length which is the
  upper limit for the sum of the length of examples in a batch:
  e.g., if ex1 has length 4, ex2 length 5 and if max_batch_length is set to 6
  ex1 and ex2 will be placed, alone, in two distinct batches.
  
  Length for each example can be obtained in two manners.
  If the input dataset is a DynamicItemDataset it can be obtained by specifying a
  length_func. Default assumes a "duration" entry is in the annotation.
  Length for each example can also be passed to this class upon instantiation
  by specifying a list containing the length for each example and passing it to
  lengths_list.
  
  Examples are grouped together by defining a set of possible discrete intervals
  (buckets). Examples whose length fall into these intervals can be batched together.
  
  The number of buckets can be specified by using the arg num_buckets.
  There is usually an optimal range for the value of this argument.
  
  If num_buckets == 1, all examples can be batched together. You have maximum randomization
  but your training speed will be slower due to the fact that a large amount of the values will be padding
  as long and short examples can be batched together.
  As the number of buckets grows only examples with similar
  length can be grouped together.
  This trades-off speed with randomization.
  TLDR: Low number -> better randomization, High number -> faster training.
  NOTE THAT: if set too high the training speed will decrease. If num_buckets -> number of examples in the dataset the batch size
  will be small impacting training speed and possibly performance.
  
  The buckets can also be specified by passing a list to the bucket_boundaries
  argument instead of specifying a left_bucket_length and a bucket_length_multiplier.
  
  Example
  -------
  >>> import torch
  >>> import speechbrain as sb
  >>> from speechbrain.dataio.sampler import DynamicBatchSampler
  >>> from speechbrain.dataio.dataset import DynamicItemDataset
  >>> from speechbrain.dataio.dataloader import SaveableDataLoader
  >>> from speechbrain.dataio.batch import PaddedBatch
  >>> import numpy as np
  >>> item_lengths = sorted([np.random.randint(10, 100) for x in range(20)])
  >>> dataset = {"ex_{}".format(x) : {"wav" :torch.randn(x)} for x in item_lengths}
  >>> dataset = DynamicItemDataset(dataset)
  >>> dataset.set_output_keys(["wav"])
  >>> length_func = lambda x : len(x) # trivial in this example
  >>> bsampler = DynamicBatchSampler(dataset, 20, 4, length_func, shuffle=False, batch_ordering='descending')
  >>> dataloader = SaveableDataLoader(dataset, batch_sampler=bsampler, collate_fn=PaddedBatch)
  >>> for i, b in enumerate(dataloader):
  ...     data, length = b["wav"]
  >>> assert data.shape[-1] == max(item_lengths)
  
  Arguments
  ---------
  dataset : torch.utils.data.Dataset
      Pytorch Dataset from which elements will be sampled.
  max_batch_length : int
      Upper limit for the sum of the length of examples in a batch.
      Should be chosen based on your GPU memory.
  num_buckets : int
      Number of discrete buckets used to group examples together.
      If num_buckets == 1, all examples can be batched together. As the number of buckets grows only examples with similar
      length can be grouped together. This trades-off speed with randomization.
      Low number -> better randomization, High number -> faster training.
      However if set too high the training speed will decrease. If num_buckets -> number of examples in the dataset the batch size
      will be small impacting training speed and possibly performance.
      NOTE: you have either to specify manually the bucket_boundaries or the number of buckets.
  length_func : callable
      Function used to get length of each example from the dataset.
      This argument can be used only when the dataset is a Speechbrain DynamicItemDataset object.
      Can be anything: e.g. lambda x: x["duration"]*16000 returns number of samples
      if duration key in the annotation is in seconds and the file has 16kHz sampling freq.
  shuffle : bool
      Whether or not shuffle examples between each epoch.
  batch_ordering : string
      If ``random``, batches are randomly permuted; otherwise ``ascending`` or ``descending`` sorted by length.
  max_batch_ex: int
      If set, it limits the maximum number of examples that can be in a batch superseeding max_batch_length
      in instances where the amount of examples will exceeed the value specified here.
      E.g. you have a lot of short examples and the batch size for those will be too high, you can use this argument
      to limit the batch size for these short examples.
  bucket_boundaries : list
      Overrides bucket_length_multiplier and left_bucket_length by specifying manually
      the buckets right boundaries.
  lengths_list: list
      Overrides length_func by passing a list containing the length of each example
      in the dataset. This argument must be set when the dataset is a plain
      Pytorch Dataset object and not a DynamicItemDataset object as length_func
      cannot be used on Pytorch Datasets.
  epoch : int
      The epoch to start at.
  drop_last : bool
       If ``True``, the sampler will drop the last examples which
       have not been grouped.
  verbose: bool
      If ``True``, log also the stats for each batch at the first epoch.
class_docstr_tok: ["This","BatchSampler","batches","examples","together","by","grouping","them","by","their","length",".","Every","example","in","the","batch","have","approximately","the","same","length","and","thus","padding","is","minimized",".","This","enables","faster","training","on","datasets","where","length","of","examples","can","vary","significantly","(","e.g","Librispeech",")",".","Inspired","by",":","https",":","\/\/www.tensorflow.org\/api_docs\/python\/tf\/data\/experimental\/bucket_by_sequence_length","Dynamic","batching","is","performed","by","specifying","a","max_batch_length","which","is","the","upper","limit","for","the","sum","of","the","length","of","examples","in","a","batch",":","e.g.",",","if","ex1","has","length","4",",","ex2","length","5","and","if","max_batch_length","is","set","to","6","ex1","and","ex2","will","be","placed",",","alone",",","in","two","distinct","batches",".","Length","for","each","example","can","be","obtained","in","two","manners",".","If","the","input","dataset","is","a","DynamicItemDataset","it","can","be","obtained","by","specifying","a","length_func",".","Default","assumes","a","``","duration","''","entry","is","in","the","annotation",".","Length","for","each","example","can","also","be","passed","to","this","class","upon","instantiation","by","specifying","a","list","containing","the","length","for","each","example","and","passing","it","to","lengths_list",".","Examples","are","grouped","together","by","defining","a","set","of","possible","discrete","intervals","(","buckets",")",".","Examples","whose","length","fall","into","these","intervals","can","be","batched","together",".","The","number","of","buckets","can","be","specified","by","using","the","arg","num_buckets",".","There","is","usually","an","optimal","range","for","the","value","of","this","argument",".","If","num_buckets","==","1",",","all","examples","can","be","batched","together",".","You","have","maximum","randomization","but","your","training","speed","will","be","slower","due","to","the","fact","that","a","large","amount","of","the","values","will","be","padding","as","long","and","short","examples","can","be","batched","together",".","As","the","number","of","buckets","grows","only","examples","with","similar","length","can","be","grouped","together",".","This","trades-off","speed","with","randomization",".","TLDR",":","Low","number","-",">","better","randomization",",","High","number","-",">","faster","training",".","NOTE","THAT",":","if","set","too","high","the","training","speed","will","decrease",".","If","num_buckets","-",">","number","of","examples","in","the","dataset","the","batch","size","will","be","small","impacting","training","speed","and","possibly","performance",".","The","buckets","can","also","be","specified","by","passing","a","list","to","the","bucket_boundaries","argument","instead","of","specifying","a","left_bucket_length","and","a","bucket_length_multiplier",".","Example","--","--","--","-",">",">",">","import","torch",">",">",">","import","speechbrain","as","sb",">",">",">","from","speechbrain.dataio.sampler","import","DynamicBatchSampler",">",">",">","from","speechbrain.dataio.dataset","import","DynamicItemDataset",">",">",">","from","speechbrain.dataio.dataloader","import","SaveableDataLoader",">",">",">","from","speechbrain.dataio.batch","import","PaddedBatch",">",">",">","import","numpy","as","np",">",">",">","item_lengths","=","sorted","(","[","np.random.randint","(","10",",","100",")","for","x","in","range","(","20",")","]",")",">",">",">","dataset","=","{","``","ex_","{","}","''",".format","(","x",")",":","{","``","wav","''",":","torch.randn","(","x",")","}","for","x","in","item_lengths","}",">",">",">","dataset","=","DynamicItemDataset","(","dataset",")",">",">",">","dataset.set_output_keys","(","[","``","wav","''","]",")",">",">",">","length_func","=","lambda","x",":","len","(","x",")","#","trivial","in","this","example",">",">",">","bsampler","=","DynamicBatchSampler","(","dataset",",","20",",","4",",","length_func",",","shuffle=False",",","batch_ordering='descending","'",")",">",">",">","dataloader","=","SaveableDataLoader","(","dataset",",","batch_sampler=bsampler",",","collate_fn=PaddedBatch",")",">",">",">","for","i",",","b","in","enumerate","(","dataloader",")",":","...","data",",","length","=","b","[","``","wav","''","]",">",">",">","assert","data.shape","[","-1","]","==","max","(","item_lengths",")","Arguments","--","--","--","--","-","dataset",":","torch.utils.data.Dataset","Pytorch","Dataset","from","which","elements","will","be","sampled",".","max_batch_length",":","int","Upper","limit","for","the","sum","of","the","length","of","examples","in","a","batch",".","Should","be","chosen","based","on","your","GPU","memory",".","num_buckets",":","int","Number","of","discrete","buckets","used","to","group","examples","together",".","If","num_buckets","==","1",",","all","examples","can","be","batched","together",".","As","the","number","of","buckets","grows","only","examples","with","similar","length","can","be","grouped","together",".","This","trades-off","speed","with","randomization",".","Low","number","-",">","better","randomization",",","High","number","-",">","faster","training",".","However","if","set","too","high","the","training","speed","will","decrease",".","If","num_buckets","-",">","number","of","examples","in","the","dataset","the","batch","size","will","be","small","impacting","training","speed","and","possibly","performance",".","NOTE",":","you","have","either","to","specify","manually","the","bucket_boundaries","or","the","number","of","buckets",".","length_func",":","callable","Function","used","to","get","length","of","each","example","from","the","dataset",".","This","argument","can","be","used","only","when","the","dataset","is","a","Speechbrain","DynamicItemDataset","object",".","Can","be","anything",":","e.g",".","lambda","x",":","x","[","``","duration","''","]","*","16000","returns","number","of","samples","if","duration","key","in","the","annotation","is","in","seconds","and","the","file","has","16kHz","sampling","freq",".","shuffle",":","bool","Whether","or","not","shuffle","examples","between","each","epoch",".","batch_ordering",":","string","If","``","random","``",",","batches","are","randomly","permuted",";","otherwise","``","ascending","``","or","``","descending","``","sorted","by","length",".","max_batch_ex",":","int","If","set",",","it","limits","the","maximum","number","of","examples","that","can","be","in","a","batch","superseeding","max_batch_length","in","instances","where","the","amount","of","examples","will","exceeed","the","value","specified","here",".","E.g",".","you","have","a","lot","of","short","examples","and","the","batch","size","for","those","will","be","too","high",",","you","can","use","this","argument","to","limit","the","batch","size","for","these","short","examples",".","bucket_boundaries",":","list","Overrides","bucket_length_multiplier","and","left_bucket_length","by","specifying","manually","the","buckets","right","boundaries",".","lengths_list",":","list","Overrides","length_func","by","passing","a","list","containing","the","length","of","each","example","in","the","dataset",".","This","argument","must","be","set","when","the","dataset","is","a","plain","Pytorch","Dataset","object","and","not","a","DynamicItemDataset","object","as","length_func","can","not","be","used","on","Pytorch","Datasets",".","epoch",":","int","The","epoch","to","start","at",".","drop_last",":","bool","If","``","True","``",",","the","sampler","will","drop","the","last","examples","which","have","not","been","grouped",".","verbose",":","bool","If","``","True","``",",","log","also","the","stats","for","each","batch","at","the","first","epoch","."]
code: def __iter__(self):
      for batch in self._batches:
          yield batch
      if (
          self._shuffle_ex
      ):  # re-generate examples if ex_ordering == "random"
          self._generate_batches()
      if self._batch_ordering == "random":
          # we randomly permute the batches only --> faster
          self._permute_batches()
  
imports: import torch
  import logging
  from operator import itemgetter
  from torch.utils.data import RandomSampler, WeightedRandomSampler, DistributedSampler, Sampler
  import numpy
  from typing import List
  from speechbrain.dataio.dataset import DynamicItemDataset
  from collections import Counter
  from scipy.stats import lognorm
label_desc: Use image node_id 6 for calling the DynamicBatchSampler obj's underlying member method code with example usage: obj.__iter__() without return types