class_docstr: Assign each data sample to a fixed bucket based on its length.
  The bucket keys are either given or generated from the input sequence lengths.
  
  Parameters
  ----------
  lengths
      The length of the sequences in the input data sample.
  batch_size
      The expected batch size of the sampler.
  num_buckets : int or None, default 10
      The number of buckets. This will not be used if bucket_keys is set.
  bucket_keys
      The keys that will be used to create the buckets. It should usually be the lengths of the
      sequences. If it is None, the bucket_keys will be generated based on the bucket schemes
      given input lengths.
  ratio
      Ratio to scale up the batch size of smaller buckets.
      Assume the :math:`i` th key is :math:`K_i` ,
      the default batch size is :math:`B` , the ratio to scale the batch size is
      :math:`\alpha` and
      the batch size corresponds to the :math:`i` th bucket is :math:`B_i` . We have:
  
      .. math::
  
          B_i = \max(\alpha B \times \frac{\max_j sum(K_j)}{sum(K_i)}, B)
  
      Thus, setting this to a value larger than 0, like 0.5, will scale up the batch size of the
      smaller buckets.
  shuffle
      Whether to shuffle the batches.
  use_average_length
      False: each batch contains batch_size sequences, number of sequence elements varies.
      True: each batch contains batch_size elements, number of sequences varies. In this case,
      ratio option is ignored.
  bucket_scheme
      It is used to generate bucket keys. It supports:
      ConstWidthBucket: all the buckets have the same width
      LinearWidthBucket: the width of ith  bucket follows :math:`w_i = \alpha * i + 1`
      ExpWidthBucket: the width of ith bucket follows
      :math:`w_i` = bucket_len_step :math:`* w_{i-1}`
  seed
      The seed of the bucket sampler
  Examples
  --------
  >>> lengths = [np.random.randint(1, 100) for _ in range(1000)]
  >>> sampler = gluonnlp.data.FixedBucketSampler(lengths, 8, ratio=0.5)
  >>> print(sampler)
  FixedBucketSampler:
  -etc-
class_docstr_tok: ["Assign","each","data","sample","to","a","fixed","bucket","based","on","its","length",".","The","bucket","keys","are","either","given","or","generated","from","the","input","sequence","lengths",".","Parameters","--","--","--","--","--","lengths","The","length","of","the","sequences","in","the","input","data","sample",".","batch_size","The","expected","batch","size","of","the","sampler",".","num_buckets",":","int","or","None",",","default","10","The","number","of","buckets",".","This","will","not","be","used","if","bucket_keys","is","set",".","bucket_keys","The","keys","that","will","be","used","to","create","the","buckets",".","It","should","usually","be","the","lengths","of","the","sequences",".","If","it","is","None",",","the","bucket_keys","will","be","generated","based","on","the","bucket","schemes","given","input","lengths",".","ratio","Ratio","to","scale","up","the","batch","size","of","smaller","buckets",".","Assume","the",":","math",":","`","i","`","th","key","is",":","math",":","`","K_i","`",",","the","default","batch","size","is",":","math",":","`","B","`",",","the","ratio","to","scale","the","batch","size","is",":","math",":","`","\\alpha","`","and","the","batch","size","corresponds","to","the",":","math",":","`","i","`","th","bucket","is",":","math",":","`","B_i","`",".","We","have",":","..","math",":",":","B_i","=","\\max","(","\\alpha","B","\\times","\\frac","{","\\max_j","sum","(","K_j",")","}","{","sum","(","K_i",")","}",",","B",")","Thus",",","setting","this","to","a","value","larger","than","0",",","like","0.5",",","will","scale","up","the","batch","size","of","the","smaller","buckets",".","shuffle","Whether","to","shuffle","the","batches",".","use_average_length","False",":","each","batch","contains","batch_size","sequences",",","number","of","sequence","elements","varies",".","True",":","each","batch","contains","batch_size","elements",",","number","of","sequences","varies",".","In","this","case",",","ratio","option","is","ignored",".","bucket_scheme","It","is","used","to","generate","bucket","keys",".","It","supports",":","ConstWidthBucket",":","all","the","buckets","have","the","same","width","LinearWidthBucket",":","the","width","of","ith","bucket","follows",":","math",":","`","w_i","=","\\alpha","*","i","+","1","`","ExpWidthBucket",":","the","width","of","ith","bucket","follows",":","math",":","`","w_i","`","=","bucket_len_step",":","math",":","`","*","w_","{","i-1","}","`","seed","The","seed","of","the","bucket","sampler","Examples","--","--","--","--",">",">",">","lengths","=","[","np.random.randint","(","1",",","100",")","for","_","in","range","(","1000",")","]",">",">",">","sampler","=","gluonnlp.data.FixedBucketSampler","(","lengths",",","8",",","ratio=0.5",")",">",">",">","print","(","sampler",")","FixedBucketSampler",":","-etc-"]
code: def __init__(
      self,
      lengths: Union[Sequence[int], Sequence[Sequence[int]]],
      batch_size: int,
      num_buckets: Optional[int] = 10,
      bucket_keys: Optional[
          Union[Sequence[int], Sequence[Sequence[int]]]
      ] = None,
      ratio: float = 0,
      shuffle: bool = False,
      use_average_length: bool = False,
      bucket_scheme: BucketScheme = ConstWidthBucket(),
      seed: Optional[int] = None,
  ):
      assert (
          len(lengths) > 0
      ), "FixedBucketSampler does not support empty lengths."
      assert batch_size > 0, "Batch size must be larger than 0."
      assert ratio >= 0, "batch size scaling ratio cannot be negative."
      self._rng = np.random.RandomState(seed)
      self._batch_size = batch_size
      self._ratio = ratio
      self._lengths = np.array(lengths, dtype=np.int64)
      if self._lengths.ndim == 1:
          self._single_element = True
          attr_num = 1
      else:
          assert self._lengths.ndim == 2, (
              "Elements in lengths must be either int or tuple/list of int. "
              "Received lengths=%s" % str(lengths)
          )
          self._single_element = False
          attr_num = self._lengths.shape[1]
      self._shuffle = shuffle
      self._bucket_scheme = bucket_scheme
      max_lengths = self._lengths.max(axis=0)
      min_lengths = self._lengths.min(axis=0)
      if self._single_element:
          assert (
              min_lengths > 0
          ), "Sequence lengths must all be larger than 0."
      else:
          for _, ele in enumerate(min_lengths):
              assert (
                  ele > 0
              ), "Sequence lengths must all be larger than 0."
      # Generate the buckets
      if bucket_keys is None:
          assert num_buckets > 0, (
              "num_buckets must be set when bucket_keys is None. Received "
              "num_buckets={}".format(num_buckets)
          )
          bucket_keys = bucket_scheme(
              max_lengths, min_lengths, num_buckets
          )
      else:
          if num_buckets is not None:
              warnings.warn(
                  "num_buckets will not be used if bucket_keys is not None. "
                  "bucket_keys={}, num_buckets={}".format(
                      str(bucket_keys), num_buckets
                  )
              )
          assert len(bucket_keys) > 0
          if self._single_element:
              assert isinstance(bucket_keys[0], int)
          else:
              assert isinstance(bucket_keys[0], tuple)
              assert len(bucket_keys[0]) == attr_num
      bucket_keys = sorted(set(bucket_keys))
      # Assign instances to buckets
      bucket_sample_ids = _match_bucket_keys(bucket_keys, self._lengths)
      unused_bucket_keys = [
          key
          for key, sample_ids in zip(bucket_keys, bucket_sample_ids)
          if len(sample_ids) == 0
      ]
      if len(unused_bucket_keys) > 0:
          warnings.warn(
              "Some buckets are empty and will be removed. Unused bucket keys={}".format(
                  str(unused_bucket_keys)
              )
          )
      # Remove empty buckets
      self._bucket_keys = [
          key
          for key, sample_ids in zip(bucket_keys, bucket_sample_ids)
          if len(sample_ids) > 0
      ]
  
      self._bucket_sample_ids = [
          sample_ids
          for sample_ids in bucket_sample_ids
          if len(sample_ids) > 0
      ]
      if not use_average_length:
          scale_up_keys = [
              key if self._single_element else sum(key)
              for key in self._bucket_keys
          ]
          max_scale_up_key = max(scale_up_keys)
          self._bucket_batch_sizes = [
              max(
                  int(
                      max_scale_up_key
                      / float(scale_up_key)
                      * self._ratio
                      * batch_size
                  ),
                  batch_size,
              )
              for scale_up_key in scale_up_keys
          ]
      else:
          if ratio > 0.0:
              warnings.warn(
                  "ratio={} is ignored when use_average_length is True".format(
                      self._ratio
                  )
              )
          bucket_average_lengths, bucket_length_stds = _bucket_stats(
              self._bucket_sample_ids, self._lengths
          )
          self._bucket_batch_sizes = [
              max(int(batch_size / (average_length + length_std)), 1)
              for average_length, length_std in zip(
                  bucket_average_lengths, bucket_length_stds
              )
          ]
      self._batch_infos = []
      for bucket_id, sample_ids, bucket_batch_size in zip(
          range(len(self._bucket_keys) - 1, -1, -1),
          self._bucket_sample_ids[::-1],
          self._bucket_batch_sizes[::-1],
      ):
          for i in range(0, len(sample_ids), bucket_batch_size):
              self._batch_infos.append((bucket_id, i))
      self._sampler_size = len(self._batch_infos)
  
imports: import math
  import random
  import warnings
  import numpy
  import abc
  import logging
  from typing import Union, Sequence, Optional, List
  from ..base import INT_TYPES
label_desc: Use image node_id 1 to create a new FixedBucketSampler object from inherited base classes: BaseSampler with example: obj = FixedBucketSampler(lengths, batch_size, num_buckets, bucket_keys, ratio, shuffle, use_average_length, bucket_scheme, seed)