Skip to content

API Documentation

BSScheduler

Source code in bs_scheduler\batch_size_schedulers.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class BSScheduler:
    def __init__(self, dataloader: DataLoader, batch_size_manager: Union[BatchSizeManager, None],
                 max_batch_size: Union[int, None], min_batch_size: int, verbose: bool):
        try:
            # Should we allow our users to use us with dataloader == None and just use the batch size managers they
            # provide us with?
            check_isinstance(dataloader, DataLoader)
        except TypeError:
            print("Parameter dataloader is not a DataLoader. If you really need this feature, please open an issue at "
                  "https://github.com/ancestor-mithril/bs-scheduler/issues and describe your use case.")
            raise
        self.dataloader: DataLoader = dataloader
        self.verbose: bool = verbose

        assert max_batch_size is None or isinstance(max_batch_size, int)
        assert isinstance(min_batch_size, int)
        if max_batch_size is None:
            max_batch_size = len(self.dataloader.dataset)
        else:
            if max_batch_size < 0:
                raise ValueError(f"Maximum batch size must be greater than 0, but is {max_batch_size}.")
            max_batch_size = min(len(self.dataloader.dataset), max_batch_size)
        self.max_batch_size: int = max_batch_size

        if min_batch_size < 0:
            raise ValueError(f"Minimum batch size must be greater than 0, but is {min_batch_size}.")
        if min_batch_size > self.max_batch_size:
            raise ValueError(f"Minimum batch size must be smaller than or equal to the maximum batch size "
                             f"({max_batch_size}), but is {min_batch_size}.")
        self.min_batch_size: int = min_batch_size

        if batch_size_manager is None:
            if self.dataloader.batch_sampler is not None:
                batch_size_manager = DefaultBatchSizeManager(self.dataloader)
            else:
                # We require the client to implement a "change_batch_size" method and a "get_batch_size" method for
                # their dataset.
                batch_size_manager = CustomBatchSizeManager(self.dataloader.dataset)
        self.batch_size_manager: BatchSizeManager = batch_size_manager

        self.last_epoch: int = -1
        if not hasattr(self.dataloader, '_base_batch_size'):
            self.dataloader._base_batch_size = self.batch_size
        self._last_bs: int = self.dataloader._base_batch_size
        self._finished: bool = False

        self._init_get_new_bs()

        # Doing the zero-th step.
        self.step()
        # The initial step may make the scheduler to finish during initialization. So we reinitialize self._finished.
        self._finished = False

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader.
        """
        return {key: value for key, value in self.__dict__.items() if
                key not in ('dataloader', '_internal_get_new_bs')}

    def load_state_dict(self, state_dict: dict):
        """ Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        self.__dict__.update(state_dict)
        self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.
        self._init_get_new_bs()

    def set_batch_size(self, new_bs: int):
        """ Forwards the call for setting the new batch size to the batch size manager. If the dataloader batch_size
        member variable is not None, it also modifies it to reflect the change in batch size.

        Args:
            new_bs (int): The new batch sizes that needs to be set.
        """
        if self.dataloader.batch_size is not None:
            # We can't directly do `dataloader.batch_size = new_bs` because the dataloader raises an error if we change
            # the batch size after initialization. But we are still hacking around it.
            self.dataloader.__dict__['batch_size'] = new_bs
        self.batch_size_manager.set_batch_size(new_bs)

    @property
    def batch_size(self) -> int:
        """ Returns the current batch size used by the dataloader as an :class:`int`. """
        return self.batch_size_manager.get_current_batch_size()

    @property
    def finished(self) -> bool:
        """ Returns True if the scheduler has already finished its job or has exceeded the minimum or maximum batch
        size. Otherwise, returns False.
        """
        return self._finished

    @property
    def last_bs(self) -> int:
        """ Returns the last computed batch size by current scheduler. If called before the first call to :meth:`step`
        returns the base batch size.
        """
        return self._last_bs

    def get_new_bs(self) -> int:
        """ Computes the next batch size. Should not be called explicitly in client code, but it doesn't really matter
        if the client does so. Some batch size schedulers use the keyword arguments.
        """
        raise NotImplementedError

    def _init_get_new_bs(self):
        # Setting the correct get_new_bs() dispatch function.
        if inspect.getfullargspec(self.get_new_bs).varkw is None:
            self._internal_get_new_bs = self._internal_bare_dispatch
        else:
            self._internal_get_new_bs = self._internal_kwargs_dispatch

    def _internal_bare_dispatch(self, **kwargs) -> int:
        return self.get_new_bs()

    def _internal_kwargs_dispatch(self, **kwargs) -> int:
        return self.get_new_bs(**kwargs)

    def print_bs(self, new_bs):
        if self.verbose:
            print(f'Adjusting batch size to {new_bs}.')

    def step(self, **kwargs):
        # TODO: Documentation
        # TODO: Check how the dataloader behaves if we change the batch size mid epoch. Write a guideline for this.
        #  Changing the batch size does not impact batch sizes loaded by workers before the change.
        # TODO: Check if changing the batch size needs locking. Because of multiprocessing. Normally it should not.
        if self.finished:
            return  # Stops doing work if already finished.

        self.last_epoch += 1
        new_bs = self._internal_get_new_bs(**kwargs)
        if not self.min_batch_size <= new_bs <= self.max_batch_size:
            self._finished = True
            new_bs = clip(new_bs, min=self.min_batch_size, max=self.max_batch_size)
        if new_bs != self.batch_size:
            self.set_batch_size(new_bs)
            self.print_bs(new_bs)
        self._last_bs = new_bs

batch_size: int property

Returns the current batch size used by the dataloader as an :class:int.

finished: bool property

Returns True if the scheduler has already finished its job or has exceeded the minimum or maximum batch size. Otherwise, returns False.

last_bs: int property

Returns the last computed batch size by current scheduler. If called before the first call to :meth:step returns the base batch size.

get_new_bs()

Computes the next batch size. Should not be called explicitly in client code, but it doesn't really matter if the client does so. Some batch size schedulers use the keyword arguments.

Source code in bs_scheduler\batch_size_schedulers.py
229
230
231
232
233
def get_new_bs(self) -> int:
    """ Computes the next batch size. Should not be called explicitly in client code, but it doesn't really matter
    if the client does so. Some batch size schedulers use the keyword arguments.
    """
    raise NotImplementedError

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
187
188
189
190
191
192
193
194
195
def load_state_dict(self, state_dict: dict):
    """ Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    self.__dict__.update(state_dict)
    self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.
    self._init_get_new_bs()

set_batch_size(new_bs)

Forwards the call for setting the new batch size to the batch size manager. If the dataloader batch_size member variable is not None, it also modifies it to reflect the change in batch size.

Parameters:

Name Type Description Default
new_bs int

The new batch sizes that needs to be set.

required
Source code in bs_scheduler\batch_size_schedulers.py
197
198
199
200
201
202
203
204
205
206
207
208
def set_batch_size(self, new_bs: int):
    """ Forwards the call for setting the new batch size to the batch size manager. If the dataloader batch_size
    member variable is not None, it also modifies it to reflect the change in batch size.

    Args:
        new_bs (int): The new batch sizes that needs to be set.
    """
    if self.dataloader.batch_size is not None:
        # We can't directly do `dataloader.batch_size = new_bs` because the dataloader raises an error if we change
        # the batch size after initialization. But we are still hacking around it.
        self.dataloader.__dict__['batch_size'] = new_bs
    self.batch_size_manager.set_batch_size(new_bs)

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader.

Source code in bs_scheduler\batch_size_schedulers.py
179
180
181
182
183
184
185
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader.
    """
    return {key: value for key, value in self.__dict__.items() if
            key not in ('dataloader', '_internal_get_new_bs')}

BatchSizeManager

Base class for all batch size managers, used for getting and setting the batch size. It is not mandatory to inherit from this, but users must implement :meth:get_current_batch_size and :meth:set_batch_size.

Source code in bs_scheduler\batch_size_schedulers.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class BatchSizeManager:
    """ Base class for all batch size managers, used for getting and setting the batch size. It is not mandatory to
    inherit from this, but users must implement :meth:`get_current_batch_size` and :meth:`set_batch_size`.
    """

    def get_current_batch_size(self) -> int:
        """ Returns the current batch size used by the dataloader as an :class:`int`.
        """
        raise NotImplementedError

    def set_batch_size(self, new_bs: int):
        """ Sets the new value of the batch size.

        Args:
            new_bs (int): The new batch sizes that needs to be set.
        """
        raise NotImplementedError

get_current_batch_size()

Returns the current batch size used by the dataloader as an :class:int.

Source code in bs_scheduler\batch_size_schedulers.py
44
45
46
47
def get_current_batch_size(self) -> int:
    """ Returns the current batch size used by the dataloader as an :class:`int`.
    """
    raise NotImplementedError

set_batch_size(new_bs)

Sets the new value of the batch size.

Parameters:

Name Type Description Default
new_bs int

The new batch sizes that needs to be set.

required
Source code in bs_scheduler\batch_size_schedulers.py
49
50
51
52
53
54
55
def set_batch_size(self, new_bs: int):
    """ Sets the new value of the batch size.

    Args:
        new_bs (int): The new batch sizes that needs to be set.
    """
    raise NotImplementedError

ChainedBSScheduler

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.ChainedScheduler. Chains a list of batch size schedulers. It takes the list of batch size schedulers and performs consucutive step() functions belonging to them by just one call

Parameters:

Name Type Description Default
schedulers Sequence[BSScheduler]

List of chained schedulers.

required

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 100 if epoch == 0
>>> # bs = 110 if epoch == 1
>>> # bs = 121 if epoch == 2
>>> # bs = 133 if epoch == 3
>>> # bs = 14 if epoch == 4
>>> # bs = 15 if epoch == 5
>>> # bs = 16 if epoch == 6
>>> # bs = 18 if epoch == 7
>>> # ...
>>> scheduler1 = ConstantBS(dataloader, factor=10, milestone=4)
>>> scheduler2 = ExponentialBS(dataloader, gamma=1.1)
>>> scheduler = ChainedBSScheduler([scheduler1, scheduler2])
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
class ChainedBSScheduler(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.ChainedScheduler.
    Chains a list of batch size schedulers. It takes the list of batch size schedulers and performs consucutive
    step() functions belonging to them by just one call

    Args:
        schedulers (Sequence[BSScheduler]): List of chained schedulers.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 100 if epoch == 0
        >>> # bs = 110 if epoch == 1
        >>> # bs = 121 if epoch == 2
        >>> # bs = 133 if epoch == 3
        >>> # bs = 14 if epoch == 4
        >>> # bs = 15 if epoch == 5
        >>> # bs = 16 if epoch == 6
        >>> # bs = 18 if epoch == 7
        >>> # ...
        >>> scheduler1 = ConstantBS(dataloader, factor=10, milestone=4)
        >>> scheduler2 = ExponentialBS(dataloader, gamma=1.1)
        >>> scheduler = ChainedBSScheduler([scheduler1, scheduler2])
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, schedulers: Sequence[BSScheduler]):
        assert isinstance(schedulers, (tuple, list)) and len(schedulers) > 1 and all(
            [isinstance(x, BSScheduler) for x in schedulers])

        dataloader: DataLoader = schedulers[0].dataloader
        batch_size_manger: BatchSizeManager = schedulers[0].batch_size_manager
        for i in range(1, len(schedulers)):
            if schedulers[i].dataloader != dataloader:
                raise ValueError(f"ChainedBSScheduler expects all schedulers to belong to the same dataloader, but got "
                                 f"scheduler at index {i} to be different than the scheduler at index 0.")
            if not isinstance(schedulers[i].batch_size_manager, type(batch_size_manger)):
                raise ValueError(
                    f"ChainedBSScheduler expects all schedulers to have the same batch size manager, but got "
                    f"scheduler at index {i} to have a different batch size manager. Expected type of "
                    f"batch size manager: {type(batch_size_manger).__name__}, got: "
                    f"{type(schedulers[i].batch_size_manager).__name__}.")
            # We do not require equality for min_batch_size and max_batch_size, but maybe we should.

        self.dataloader: DataLoader = dataloader
        self.batch_size_manager: BatchSizeManager = batch_size_manger
        self.schedulers: Tuple[BSScheduler, ...] = tuple(schedulers)
        self._last_bs: int = self.schedulers[-1].last_bs
        self.max_batch_size: int = max([x.max_batch_size for x in self.schedulers])
        self.min_batch_size: int = min([x.min_batch_size for x in self.schedulers])
        self._finished: bool = False
        # self.verbose: bool = False
        # self.last_epoch: int = 0
        self._init_get_new_bs()

    def step(self, **kwargs):
        """ Executes the step() function for all schedulers in order.

        Args:
            **kwargs: All kwargs arguments are passed to each scheduler.
        """
        for scheduler in self.schedulers:
            scheduler.step(**kwargs)
        self._last_bs = self.schedulers[-1].last_bs

    @property
    def finished(self) -> bool:
        """ Returns True if all the schedulers have already finished their job or have exceeded the minimum or maximum
        batch size. Otherwise, returns False.
        """
        if not self._finished:
            self._finished = all([x.finished for x in self.schedulers])
        return self._finished

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
        states will also be saved.
        """
        state_dict = super().state_dict()
        state_dict['schedulers'] = [None] * len(self.schedulers)

        for i, s in enumerate(self.schedulers):
            state_dict['schedulers'][i] = s.state_dict()

        return state_dict

    def load_state_dict(self, state_dict: dict):
        """ Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        schedulers = state_dict.pop('schedulers')
        self.__dict__.update(state_dict)

        state_dict['schedulers'] = schedulers
        for i, s in enumerate(schedulers):
            self.schedulers[i].load_state_dict(s)

        self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.

finished: bool property

Returns True if all the schedulers have already finished their job or have exceeded the minimum or maximum batch size. Otherwise, returns False.

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
def load_state_dict(self, state_dict: dict):
    """ Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    schedulers = state_dict.pop('schedulers')
    self.__dict__.update(state_dict)

    state_dict['schedulers'] = schedulers
    for i, s in enumerate(schedulers):
        self.schedulers[i].load_state_dict(s)

    self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader. The wrapped scheduler states will also be saved.

Source code in bs_scheduler\batch_size_schedulers.py
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
    states will also be saved.
    """
    state_dict = super().state_dict()
    state_dict['schedulers'] = [None] * len(self.schedulers)

    for i, s in enumerate(self.schedulers):
        state_dict['schedulers'][i] = s.state_dict()

    return state_dict

step(**kwargs)

Executes the step() function for all schedulers in order.

Parameters:

Name Type Description Default
**kwargs

All kwargs arguments are passed to each scheduler.

{}
Source code in bs_scheduler\batch_size_schedulers.py
1007
1008
1009
1010
1011
1012
1013
1014
1015
def step(self, **kwargs):
    """ Executes the step() function for all schedulers in order.

    Args:
        **kwargs: All kwargs arguments are passed to each scheduler.
    """
    for scheduler in self.schedulers:
        scheduler.step(**kwargs)
    self._last_bs = self.schedulers[-1].last_bs

ConstantBS

Bases: BSScheduler

Increases the batch size by a constant multiplicative factor until the number of epochs reaches a pre-defined milestone. The batch size is multiplied by the constant factor during initialization and is multiplied again with the inverse of the constant factor when the milestone is reached. If the constant factor makes the batch size increase the image out of bounds, the constant factor is changed automatically such that the batch size remains within bounds.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
factor float

The number we multiply the batch size until the milestone.

required
milestone int

The number of steps that the scheduler increases the learning rate. Default: 5.

5
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 50 if epoch == 0
>>> # bs = 50 if epoch == 1
>>> # bs = 50 if epoch == 2
>>> # bs = 10 if epoch >= 3
>>> scheduler = ConstantBS(dataloader, factor=5, milestone=3)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
class ConstantBS(BSScheduler):
    """ Increases the batch size by a constant multiplicative factor until the number of epochs reaches a pre-defined
    milestone. The batch size is multiplied by the constant factor during initialization and is multiplied again with
    the inverse of the constant factor when the milestone is reached.
    If the constant factor makes the batch size increase the image out of bounds, the constant factor is changed
    automatically such that the batch size remains within bounds.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        factor (float): The number we multiply the batch size until the milestone.
        milestone (int): The number of steps that the scheduler increases the learning rate. Default: 5.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 50 if epoch == 0
        >>> # bs = 50 if epoch == 1
        >>> # bs = 50 if epoch == 2
        >>> # bs = 10 if epoch >= 3
        >>> scheduler = ConstantBS(dataloader, factor=5, milestone=3)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, factor: float, milestone: int = 5,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(milestone, int) and milestone > 0
        assert factor > 0.0
        # Factor is expected to be greater than 1.0, as this should be a warmup process.
        self.factor: float = factor
        self.milestone: int = milestone
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. The value of the batch size is changed once at
        initialization, when the batch size is multiplied with the given factor, and twice when the milestone is
        reached and the batch size is multiplied with the inverse of the given factor. The factor is adjusted during
        initialization such that it does not return a batch size out of bounds.
        """
        if self.last_epoch == 0:
            max_factor = self.max_batch_size / self.batch_size
            min_factor = self.min_batch_size / self.batch_size
            if self.factor > max_factor:
                self.factor = max_factor
            elif self.factor < min_factor:
                self.factor = min_factor
            return rint(self.batch_size * self.factor)

        if self.last_epoch != self.milestone:
            return self.batch_size

        self._finished = True  # My job is done.
        return rint(self.batch_size * (1.0 / self.factor))

get_new_bs()

Returns the next batch size as an :class:int. The value of the batch size is changed once at initialization, when the batch size is multiplied with the given factor, and twice when the milestone is reached and the batch size is multiplied with the inverse of the given factor. The factor is adjusted during initialization such that it does not return a batch size out of bounds.

Source code in bs_scheduler\batch_size_schedulers.py
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. The value of the batch size is changed once at
    initialization, when the batch size is multiplied with the given factor, and twice when the milestone is
    reached and the batch size is multiplied with the inverse of the given factor. The factor is adjusted during
    initialization such that it does not return a batch size out of bounds.
    """
    if self.last_epoch == 0:
        max_factor = self.max_batch_size / self.batch_size
        min_factor = self.min_batch_size / self.batch_size
        if self.factor > max_factor:
            self.factor = max_factor
        elif self.factor < min_factor:
            self.factor = min_factor
        return rint(self.batch_size * self.factor)

    if self.last_epoch != self.milestone:
        return self.batch_size

    self._finished = True  # My job is done.
    return rint(self.batch_size * (1.0 / self.factor))

CosineAnnealingBS

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.CosineAnnealingLR which implements the cosine annealing part of SGDR: Stochastic Gradient Descent with Warm Restarts_. For batch size, we perform reverse annealing and instead of decreasing the batch size to min_batch_size we increase it to max_batch_size.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
total_iters int

The number of steps that the scheduler increases the batch size.

required
base_batch_size Union[int, None]

The base batch size. If None, the base batch size will be retrieved from the dataloader. Default: None.

None
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

.. _SGDR\: Stochastic Gradient Descent with Warm Restarts: https://arxiv.org/abs/1608.03983

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if epoch % 10 == 0
>>> # bs = 19 if epoch % 10 == 1
>>> # bs = 41 if epoch % 10 == 2
>>> # bs = 69 if epoch % 10 == 3
>>> # bs = 91 if epoch % 10 == 4
>>> # bs = 100 if epoch % 10 == 5
>>> # bs = 91 if epoch % 10 == 6
>>> # bs = 67 if epoch % 10 == 7
>>> # bs = 37 if epoch % 10 == 8
>>> # bs = 13 if epoch % 10 == 9
>>> scheduler = CosineAnnealingBS(dataloader, total_iters=5)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
class CosineAnnealingBS(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.CosineAnnealingLR which implements the cosine annealing part of
    `SGDR: Stochastic Gradient Descent with Warm Restarts`_. For batch size, we perform reverse annealing and instead
    of decreasing the batch size to min_batch_size we increase it to max_batch_size.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        total_iters (int): The number of steps that the scheduler increases the batch size.
        base_batch_size (Union[int, None]): The base batch size. If None, the base batch size will be retrieved from
            the dataloader. Default: None.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    .. _SGDR\\: Stochastic Gradient Descent with Warm Restarts: https://arxiv.org/abs/1608.03983

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if epoch % 10 == 0
        >>> # bs = 19 if epoch % 10 == 1
        >>> # bs = 41 if epoch % 10 == 2
        >>> # bs = 69 if epoch % 10 == 3
        >>> # bs = 91 if epoch % 10 == 4
        >>> # bs = 100 if epoch % 10 == 5
        >>> # bs = 91 if epoch % 10 == 6
        >>> # bs = 67 if epoch % 10 == 7
        >>> # bs = 37 if epoch % 10 == 8
        >>> # bs = 13 if epoch % 10 == 9
        >>> scheduler = CosineAnnealingBS(dataloader, total_iters=5)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, total_iters: int, base_batch_size: Union[int, None] = None,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(total_iters, int) and total_iters > 1
        assert base_batch_size is None or (isinstance(base_batch_size, int) and base_batch_size >= min_batch_size)

        self.total_iters: int = total_iters
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)
        self.base_batch_size: int = self.dataloader._base_batch_size if base_batch_size is None else base_batch_size
        assert self.max_batch_size > self.base_batch_size
        self._float_batch_size: float = self.base_batch_size

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
        batch size following a cyclic cosine curve. The implementation is equivalent to
        torch.optim.lr_scheduler.CosineAnnealingLR.get_lr() and instead of `eta_min` we use `self.max_batch_size` and
        we clip the values to be within bounds.
        """
        if self.last_epoch == 0:
            return self.batch_size

        if self.last_epoch == 1 and self.base_batch_size == self.batch_size:
            new_bs = self.max_batch_size + (self.base_batch_size - self.max_batch_size) * (
                    1 + math.cos(self.last_epoch * math.pi / self.total_iters)) / 2
        elif (self.last_epoch - 1 - self.total_iters) % (2 * self.total_iters) == 0:
            new_bs = self.batch_size + (self.base_batch_size - self.max_batch_size) * (
                    1 - math.cos(math.pi / self.total_iters)) / 2
        else:
            new_bs = (1 + math.cos(math.pi * self.last_epoch / self.total_iters)) / (
                    1 + math.cos(math.pi * (self.last_epoch - 1) / self.total_iters)) * (
                             self._float_batch_size - self.max_batch_size) + self.max_batch_size

        self._float_batch_size = new_bs
        return clip(rint(new_bs), min=self.base_batch_size, max=self.max_batch_size)

get_new_bs()

Returns the next batch size as an :class:int. Increases the batch size from base batch size to maximum batch size following a cyclic cosine curve. The implementation is equivalent to torch.optim.lr_scheduler.CosineAnnealingLR.get_lr() and instead of eta_min we use self.max_batch_size and we clip the values to be within bounds.

Source code in bs_scheduler\batch_size_schedulers.py
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
    batch size following a cyclic cosine curve. The implementation is equivalent to
    torch.optim.lr_scheduler.CosineAnnealingLR.get_lr() and instead of `eta_min` we use `self.max_batch_size` and
    we clip the values to be within bounds.
    """
    if self.last_epoch == 0:
        return self.batch_size

    if self.last_epoch == 1 and self.base_batch_size == self.batch_size:
        new_bs = self.max_batch_size + (self.base_batch_size - self.max_batch_size) * (
                1 + math.cos(self.last_epoch * math.pi / self.total_iters)) / 2
    elif (self.last_epoch - 1 - self.total_iters) % (2 * self.total_iters) == 0:
        new_bs = self.batch_size + (self.base_batch_size - self.max_batch_size) * (
                1 - math.cos(math.pi / self.total_iters)) / 2
    else:
        new_bs = (1 + math.cos(math.pi * self.last_epoch / self.total_iters)) / (
                1 + math.cos(math.pi * (self.last_epoch - 1) / self.total_iters)) * (
                         self._float_batch_size - self.max_batch_size) + self.max_batch_size

    self._float_batch_size = new_bs
    return clip(rint(new_bs), min=self.base_batch_size, max=self.max_batch_size)

CosineAnnealingBSWithWarmRestarts

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.CosineAnnealingWarmRestarts which implements SGDR: Stochastic Gradient Descent with Warm Restarts_. Unlike torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, which decreases the learning rate for :math:t_{i} iterations and then restarts, we increase the batch size from base_batch_size to max_batch_size in :math:t_{i} + 1 iterations, then the batch size is restarted.

This scheduler can be used after every batch.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
t_0 int

The number of iterations for the first restart is t_0 + 1.

required
base_batch_size Union[int, None]

The base batch size. If None, the base batch size will be retrieved from the dataloader. Default: None.

None
factor int

The factor with which :math:t_{i} is increased after a restart. Default: 1.

1
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

.. _SGDR\: Stochastic Gradient Descent with Warm Restarts: https://arxiv.org/abs/1608.03983

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if last_epoch % 6 == 0
>>> # bs = 19 if last_epoch % 6 == 1
>>> # bs = 41 if last_epoch % 6 == 2
>>> # bs = 69 if last_epoch % 6 == 3
>>> # bs = 91 if last_epoch % 6 == 4
>>> # bs = 100 if last_epoch % 6 == 5
>>> scheduler = CosineAnnealingBSWithWarmRestarts(dataloader, 10)
>>> for epoch in range(100):
>>>     for batch in dataloader:
>>>         train_batch(...)
>>>         scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
class CosineAnnealingBSWithWarmRestarts(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.CosineAnnealingWarmRestarts which implements `SGDR: Stochastic Gradient
    Descent with Warm Restarts`_. Unlike torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, which decreases the
    learning rate for :math:`t_{i}` iterations and then restarts, we increase the batch size from base_batch_size to
    max_batch_size in :math:`t_{i} + 1` iterations, then the batch size is restarted.

    This scheduler can be used after every batch.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        t_0 (int): The number of iterations for the first restart is t_0 + 1.
        base_batch_size (Union[int, None]): The base batch size. If None, the base batch size will be retrieved from
            the dataloader. Default: None.
        factor (int): The factor with which :math:`t_{i}` is increased after a restart. Default: 1.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    .. _SGDR\\: Stochastic Gradient Descent with Warm Restarts: https://arxiv.org/abs/1608.03983

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if last_epoch % 6 == 0
        >>> # bs = 19 if last_epoch % 6 == 1
        >>> # bs = 41 if last_epoch % 6 == 2
        >>> # bs = 69 if last_epoch % 6 == 3
        >>> # bs = 91 if last_epoch % 6 == 4
        >>> # bs = 100 if last_epoch % 6 == 5
        >>> scheduler = CosineAnnealingBSWithWarmRestarts(dataloader, 10)
        >>> for epoch in range(100):
        >>>     for batch in dataloader:
        >>>         train_batch(...)
        >>>         scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, t_0: int, base_batch_size: Union[int, None] = None, factor: int = 1,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(t_0, int) and t_0 > 0
        assert isinstance(factor, int) and factor > 0
        assert base_batch_size is None or (isinstance(base_batch_size, int) and base_batch_size >= min_batch_size)

        self.t_0: int = t_0
        self.t_i: int = t_0
        self.t_cur: int = 0
        self.factor: int = factor
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)
        self.base_batch_size: int = self.dataloader._base_batch_size if base_batch_size is None else base_batch_size
        assert self.max_batch_size > self.base_batch_size

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
        batch and restarts. The implementation is similar to
        torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of `eta_min` we use max_batch_size, and we
        increase the batch size instead of decreasing the learning rate. We clip the values to always remain within
        bound.
        """
        if self.last_epoch == 0:  # Don't do anything at initialization.
            return self.batch_size

        self.t_cur += 1
        if self.t_cur > self.t_i:  # > so that we reach max_batch_size
            self.t_cur -= self.t_i + 1  # + 1 so that we go back to base_batch_size
            self.t_i *= self.factor

        new_bs = self.base_batch_size + (self.max_batch_size - self.base_batch_size) * (
                1 + math.cos(math.pi + math.pi * self.t_cur / self.t_i)) / 2
        return clip(rint(new_bs), min=self.base_batch_size, max=self.max_batch_size)

get_new_bs()

Returns the next batch size as an :class:int. Increases the batch size from base batch size to maximum batch and restarts. The implementation is similar to torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of eta_min we use max_batch_size, and we increase the batch size instead of decreasing the learning rate. We clip the values to always remain within bound.

Source code in bs_scheduler\batch_size_schedulers.py
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
    batch and restarts. The implementation is similar to
    torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of `eta_min` we use max_batch_size, and we
    increase the batch size instead of decreasing the learning rate. We clip the values to always remain within
    bound.
    """
    if self.last_epoch == 0:  # Don't do anything at initialization.
        return self.batch_size

    self.t_cur += 1
    if self.t_cur > self.t_i:  # > so that we reach max_batch_size
        self.t_cur -= self.t_i + 1  # + 1 so that we go back to base_batch_size
        self.t_i *= self.factor

    new_bs = self.base_batch_size + (self.max_batch_size - self.base_batch_size) * (
            1 + math.cos(math.pi + math.pi * self.t_cur / self.t_i)) / 2
    return clip(rint(new_bs), min=self.base_batch_size, max=self.max_batch_size)

CyclicBS

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.CyclicLR. Sets the batch size according to a cyclical batch size policy, inspired from the cyclical learning rate policy (CLR). The policy cycles the batch size between two boundaries with a constant frequency, similar to a reversed cycle from the method detailed in the paper Cyclical Learning Rates for Training Neural Networks_. The distance between the two boundaries can be scaled on a per-iteration or per-cycle basis.

Cyclical batch size policy changes the batch size after every batch. The step() function should be called after a batch has been used for training.

This class has three built-in policies, as put forth in the paper:

  • "triangular": A basic triangular cycle without amplitude scaling.
  • "triangular2": A basic triangular cycle that scales initial amplitude by half each cycle.
  • "exp_range": A cycle that scales initial amplitude by :math:\gamma^{\text{cycle iterations}} at each cycle iteration.

This implementation was adapted from pytorch/pytorch which was adapted from the github repo: bckenstler/CLR.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
base_batch_size Union[int, None]

Initial batch size which is the lower boundery in the cycle. If None, the base batch size will be retrieved from the dataloader. Default: None.

None
step_size_down int

Number of training iterations in the decreasing half of a cycle. Default: 2000.

2000
step_size_up Union[int, None]

Number of training iterations in the increasing half of a cycle. If step_size_down is None, it is set to step_size_down. Default: None.

None
mode str

One of triangular, triangular2, exp_range. Values correspond to the policies detailed above. If scale_fn is not None, this argument is ignored. Default: 'triangular'.

'triangular'
gamma float

Constant in the 'exp_range' scaling function: gamma ** (cycle iterations). Default: 1.0.

1.0
scale_fn Union[Callable[[int], float], None]

Custom scaling policy defined by a single argument lambda function, where 0 <= scale_fn(x) <= 1 for all x >= 0. If specified, then 'mode' is ignored. Default: None.

None
scale_mode str

One of cycle, iterations. Defines whether scale_fn is evaluated on cycle number of cycle iterations (training iterations since the start of the cycle). When scale_fn is None, scale_mode is automatically set to 'iterations' if mode is 'exp_range' and 'cycle' otherwhise. Default: 'cycle'.

'cycle'
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper batch size boundary in the cycle. Functionally, it defines the cycle amplitude (upper_batch_size_bound - base_batch_size). The batch size at any cycle is the sum of base_batch_size and some scaling of the amplitude; therefore, upper_batch_size_bound may not actually be reached depending on scaling function. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> scheduler = CyclicBS(dataloader)
>>> for epoch in range(100):
>>>     for batch in dataloader:
>>>         train_batch(...)
>>>         scheduler.step()

.. _Cyclical Learning Rates for Training Neural Networks: https://arxiv.org/abs/1506.01186 .. _pytorch/pytorch: https://github.com/pytorch/pytorch .. _bckenstler/CLR: https://github.com/bckenstler/CLR

Source code in bs_scheduler\batch_size_schedulers.py
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
class CyclicBS(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.CyclicLR. Sets the batch size according to a cyclical batch size policy,
    inspired from the cyclical learning rate policy (CLR). The policy cycles the batch size between two boundaries with
    a constant frequency, similar to a reversed cycle from the method detailed in the paper `Cyclical Learning Rates
    for Training Neural Networks`_. The distance between the two boundaries can be scaled on a per-iteration or
    per-cycle basis.

    Cyclical batch size policy changes the batch size after every batch. The step() function should be called after a
    batch has been used for training.

    This class has three built-in policies, as put forth in the paper:

    * "triangular": A basic triangular cycle without amplitude scaling.
    * "triangular2": A basic triangular cycle that scales initial amplitude by half each cycle.
    * "exp_range": A cycle that scales initial amplitude by :math:`\\gamma^{\\text{cycle iterations}}` at each cycle
        iteration.

    This implementation was adapted from `pytorch/pytorch`_ which was adapted from the github repo: `bckenstler/CLR`_.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        base_batch_size (Union[int, None]): Initial batch size which is the lower boundery in the cycle. If None, the
            base batch size will be retrieved from the dataloader. Default: None.
        step_size_down (int): Number of training iterations in the decreasing half of a cycle. Default: 2000.
        step_size_up (Union[int, None]): Number of training iterations in the increasing half of a cycle. If
            step_size_down is None, it is set to step_size_down. Default: None.
        mode (str): One of `triangular`, `triangular2`, `exp_range`. Values correspond to the policies detailed above.
            If scale_fn is not None, this argument is ignored. Default: 'triangular'.
        gamma (float): Constant in the 'exp_range' scaling function: gamma ** (cycle iterations). Default: 1.0.
        scale_fn (Union[Callable[[int], float], None]): Custom scaling policy defined by a single argument lambda
            function, where 0 <= scale_fn(x) <= 1 for all x >= 0. If specified, then 'mode' is ignored. Default: None.
        scale_mode (str): One of `cycle`, `iterations`. Defines whether scale_fn is evaluated on cycle number of cycle
            iterations (training iterations since the start of the cycle). When scale_fn is None, scale_mode is
            automatically set to 'iterations' if mode is 'exp_range' and 'cycle' otherwhise. Default: 'cycle'.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper batch size boundary in the cycle. Functionally, it defines the cycle
            amplitude (upper_batch_size_bound - base_batch_size). The batch size at any cycle is the sum of
            base_batch_size and some scaling of the amplitude; therefore, upper_batch_size_bound may not actually be
            reached depending on scaling function. If None or greater than the lenght of the dataset wrapped by the
            dataloader, max_batch_size is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> scheduler = CyclicBS(dataloader)
        >>> for epoch in range(100):
        >>>     for batch in dataloader:
        >>>         train_batch(...)
        >>>         scheduler.step()

    .. _Cyclical Learning Rates for Training Neural Networks: https://arxiv.org/abs/1506.01186
    .. _pytorch/pytorch: https://github.com/pytorch/pytorch
    .. _bckenstler/CLR: https://github.com/bckenstler/CLR
    """

    def __init__(self, dataloader: DataLoader, base_batch_size: Union[int, None] = None,
                 step_size_down: int = 2000, step_size_up: Union[int, None] = None, mode: str = 'triangular',
                 gamma: float = 1.0, scale_fn: Union[Callable[[int], float], None] = None, scale_mode: str = 'cycle',
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert base_batch_size is None or (isinstance(base_batch_size, int) and base_batch_size >= min_batch_size)
        assert isinstance(step_size_down, int) and step_size_down > 0
        assert step_size_up is None or (isinstance(step_size_up, int) and step_size_up > 0)
        assert isinstance(gamma, (int, float)) and gamma > 0.0
        assert scale_fn is None or callable(scale_fn)
        assert scale_mode in ('cycle', 'iterations')

        if mode not in ('triangular', 'triangular2', 'exp_range') and scale_fn is None:
            raise ValueError("CyclicBS requires either a valid mode or passing a custom scale_fn.")
        self.mode: str = mode

        if step_size_up is None:
            step_size_up = step_size_down
        self.total_size: float = float(step_size_down + step_size_up)
        self.step_ratio: float = step_size_down / self.total_size
        self.gamma: float = float(gamma)

        self._scale_fn_custom: Union[Callable[[int], float], None] = scale_fn
        self.scale_mode: str = scale_mode
        self._init_scale_fn()

        self.base_batch_size: Union[int, None] = base_batch_size
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)
        self.base_batch_size: int = self.dataloader._base_batch_size if base_batch_size is None else base_batch_size
        assert self.min_batch_size < self.base_batch_size

    def _init_scale_fn(self):
        if self._scale_fn_custom is not None:
            self.scale_fn = self._scale_fn_custom
        elif self.mode == 'triangular':
            self.scale_fn = self._triangular_scale_fn
            self.scale_mode = 'cycle'
        elif self.mode == 'triangular2':
            self.scale_fn = self._triangular2_scale_fn
            self.scale_mode = 'cycle'
        elif self.mode == 'exp_range':
            self.scale_fn = partial(self._exp_range_scale_fn, self.gamma)
            self.scale_mode = 'iterations'

    @staticmethod
    def _triangular_scale_fn(x: int) -> float:
        return 1.0

    @staticmethod
    def _triangular2_scale_fn(x: int) -> float:
        return 1.0 / (2.0 ** (x - 1))

    @staticmethod
    def _exp_range_scale_fn(gamma: float, x: int) -> float:
        return gamma ** x

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. The value of the batch size cycles from base_batch_size to
        max_batch_size and back, while being scaled at each iteration.
        """
        if self.last_epoch == 0:  # Return base batch size or current batch size at initialization.
            return self.base_batch_size if self.base_batch_size is not None else self.batch_size

        ratio = self.last_epoch / self.total_size
        cycle = math.floor(1 + ratio)
        x = 1.0 + ratio - cycle
        if x <= self.step_ratio:
            scale_factor = x / self.step_ratio
        else:
            scale_factor = (x - 1) / (self.step_ratio - 1)

        base_height = (self.base_batch_size - self.min_batch_size) * scale_factor
        if self.scale_mode == 'cycle':
            base_height *= self.scale_fn(cycle)
        else:
            base_height *= self.scale_fn(self.last_epoch)

        return rint(self.base_batch_size - base_height)

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
        states will also be saved.
        """
        state_dict = super().state_dict()
        state_dict.pop('scale_fn')
        if self._scale_fn_custom is not None:
            state_dict.pop('_scale_fn_custom')
        return state_dict

    def load_state_dict(self, state_dict: dict):
        """ Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        super().load_state_dict(state_dict)
        self._init_scale_fn()

get_new_bs()

Returns the next batch size as an :class:int. The value of the batch size cycles from base_batch_size to max_batch_size and back, while being scaled at each iteration.

Source code in bs_scheduler\batch_size_schedulers.py
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. The value of the batch size cycles from base_batch_size to
    max_batch_size and back, while being scaled at each iteration.
    """
    if self.last_epoch == 0:  # Return base batch size or current batch size at initialization.
        return self.base_batch_size if self.base_batch_size is not None else self.batch_size

    ratio = self.last_epoch / self.total_size
    cycle = math.floor(1 + ratio)
    x = 1.0 + ratio - cycle
    if x <= self.step_ratio:
        scale_factor = x / self.step_ratio
    else:
        scale_factor = (x - 1) / (self.step_ratio - 1)

    base_height = (self.base_batch_size - self.min_batch_size) * scale_factor
    if self.scale_mode == 'cycle':
        base_height *= self.scale_fn(cycle)
    else:
        base_height *= self.scale_fn(self.last_epoch)

    return rint(self.base_batch_size - base_height)

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
1357
1358
1359
1360
1361
1362
1363
1364
def load_state_dict(self, state_dict: dict):
    """ Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    super().load_state_dict(state_dict)
    self._init_scale_fn()

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader. The wrapped scheduler states will also be saved.

Source code in bs_scheduler\batch_size_schedulers.py
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
    states will also be saved.
    """
    state_dict = super().state_dict()
    state_dict.pop('scale_fn')
    if self._scale_fn_custom is not None:
        state_dict.pop('_scale_fn_custom')
    return state_dict

ExponentialBS

Bases: BSScheduler

Increases the batch size by a gamma every epoch.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
gamma float

Multiplicative factor of batch size growth.

required
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if epoch == 0
>>> # bs = 11 if epoch == 1
>>> # bs = 12 if epoch == 2
>>> # bs = 13 if epoch == 3
>>> # ...
>>> scheduler = ExponentialBS(dataloader, gamma=1.1)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
class ExponentialBS(BSScheduler):
    """ Increases the batch size by a gamma every epoch.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        gamma (float): Multiplicative factor of batch size growth.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if epoch == 0
        >>> # bs = 11 if epoch == 1
        >>> # bs = 12 if epoch == 2
        >>> # bs = 13 if epoch == 3
        >>> # ...
        >>> scheduler = ExponentialBS(dataloader, gamma=1.1)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, gamma: float, batch_size_manager: Union[BatchSizeManager, None] = None,
                 max_batch_size: Union[int, None] = None, min_batch_size: int = 1, verbose: bool = False):
        assert gamma > 0.0
        # Gamma is expected to be greater than 1.0 for batch size growth. It can be lower than 1.0 for batch size decay.
        self.gamma: float = gamma
        self.float_bs: Union[float, None] = None
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. The current batch size is multiplied by gamma each epoch
        except the first one.
        """
        if self.last_epoch == 0:
            return self.batch_size

        if self.float_bs is None or rint(self.float_bs) != self.batch_size:
            # Using rint instead of int because otherwise we will increas the BS faster
            self.float_bs = self.batch_size

        self.float_bs *= self.gamma
        return rint(self.float_bs)

get_new_bs()

Returns the next batch size as an :class:int. The current batch size is multiplied by gamma each epoch except the first one.

Source code in bs_scheduler\batch_size_schedulers.py
671
672
673
674
675
676
677
678
679
680
681
682
683
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. The current batch size is multiplied by gamma each epoch
    except the first one.
    """
    if self.last_epoch == 0:
        return self.batch_size

    if self.float_bs is None or rint(self.float_bs) != self.batch_size:
        # Using rint instead of int because otherwise we will increas the BS faster
        self.float_bs = self.batch_size

    self.float_bs *= self.gamma
    return rint(self.float_bs)

IncreaseBSOnPlateau

Bases: BSScheduler

The inverse of torch.optim.lr_scheduler.ReduceLROnPlateau. Increases the batch size when a metric has stopped improving. Models often benefit from increasing the batch size by a factor once the learning stagnates. This scheduler receives a metric value and if no improvement is seen for a given number of epochs, the batch size is increased. Unfortunately, this class is not compatible with the other batch size schedulers as its step() function needs to receive the metric value.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
mode str

One of min, max. In min mode, the batch size will be increased when the metric value has stopped decreasing; in max mode, the batch size will be increased when the metric value has stopped increasing. Default: 'min'.

'min'
factor float

Factor by which the batch size will be increased. Default: 2.0.

2.0
patience int

Number of epochs with no improvement after which the batch size will be increased. Default: 10.

10
threshold float

Threshold for measuring the new metric value, to only focus on significant changes. Default: 1e-4.

0.0001
threshold_mode str

One of rel, abs. In rel mode, dynamic_threshold = best * ( 1 + threshold ) in 'max' mode or best * ( 1 - threshold ) in min mode. In abs mode, dynamic_threshold = best + threshold in 'max' mode or best - threshold in min mode. Default: 'rel'.

'rel'
cooldown int

Number of epochs to wait before resuming normal operation after the batch size has been reduced. Default: 0.

0
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> scheduler = IncreaseBSOnPlateau(dataloader)
>>> for epoch in range(100):
>>>     train(...)
>>>     val_loss = validate(...)
>>>     scheduler.step(metric=val_loss)
Source code in bs_scheduler\batch_size_schedulers.py
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
class IncreaseBSOnPlateau(BSScheduler):
    """ The inverse of torch.optim.lr_scheduler.ReduceLROnPlateau.
    Increases the batch size when a metric has stopped improving. Models often benefit from increasing the batch size
    by a factor once the learning stagnates. This scheduler receives a metric value and if no improvement is seen for a
    given number of epochs, the batch size is increased.
    Unfortunately, this class is not compatible with the other batch size schedulers as its step() function needs to
    receive the metric value.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        mode (str): One of `min`, `max`. In `min` mode, the batch size will be increased when the metric value has
            stopped decreasing; in `max` mode, the batch size will be increased when the metric value has stopped
            increasing. Default: 'min'.
        factor (float): Factor by which the batch size will be increased. Default: 2.0.
        patience (int): Number of epochs with no improvement after which the batch size will be increased. Default: 10.
        threshold (float): Threshold for measuring the new metric value, to only focus on significant changes.
            Default: 1e-4.
        threshold_mode (str): One of `rel`, `abs`. In `rel` mode, dynamic_threshold = best * ( 1 + threshold ) in 'max'
            mode or best * ( 1 - threshold ) in `min` mode. In `abs` mode, dynamic_threshold = best + threshold in 'max'
            mode or best - threshold in `min` mode. Default: 'rel'.
        cooldown (int): Number of epochs to wait before resuming normal operation after the batch size has been reduced.
            Default: 0.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> scheduler = IncreaseBSOnPlateau(dataloader)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     val_loss = validate(...)
        >>>     scheduler.step(metric=val_loss)
    """

    def __init__(self, dataloader: DataLoader, mode: str = 'min', factor: float = 2.0, patience: int = 10,
                 threshold: float = 1e-4, threshold_mode: str = 'rel', cooldown: int = 0,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)
        assert isinstance(factor, (int, float)) and factor != 1.0 and factor >= 0.0
        # Factor is expected to be greater than 1, but we do not forbid batch size decay.
        assert isinstance(patience, int) and patience >= 0
        assert isinstance(threshold, (int, float)) and threshold > 0.0
        assert isinstance(cooldown, int) and cooldown >= 0

        self.mode: str = mode
        self.factor: float = float(factor)
        self.patience: int = patience
        self.threshold: float = float(threshold)
        self.threshold_mode: str = threshold_mode
        self.cooldown: int = cooldown
        self.cooldown_counter: int = 0
        self.mode_worse: float = torch.inf if mode == 'min' else -torch.inf
        self.best: float = self.mode_worse
        self.num_bad_epochs: int = 0

        self.last_epoch = 0  # setting last epoch to 0

        self._init_is_better(mode, threshold_mode)
        self._reset()

    @property
    def in_cooldown(self) -> bool:
        """ Returns True if scheduler is in cooldown, False otherwise.
        """
        return self.cooldown_counter > 0

    def _reset(self):
        """ Resets num_bad_epochs counter and cooldown counter."""
        self.best = self.mode_worse
        self.cooldown_counter = 0
        self.num_bad_epochs = 0

    @staticmethod
    def is_better_min_rel(a: float, best: float, threshold: float) -> bool:
        return a < best * (1.0 - threshold)

    @staticmethod
    def is_better_min_abs(a: float, best: float, threshold: float) -> bool:
        return a < best - threshold

    @staticmethod
    def is_better_max_rel(a: float, best: float, threshold: float) -> bool:
        return a > best * (1.0 + threshold)

    @staticmethod
    def is_better_max_abs(a: float, best: float, threshold: float) -> bool:
        return a > best + threshold

    def _init_is_better(self, mode: str, threshold_mode: str):
        if mode not in ('min', 'max'):
            raise ValueError(f'Mode {mode} is unknown!')
        if threshold_mode not in ('rel', 'abs'):
            raise ValueError(f'Threshold mode {mode} is unknown!')

        if mode == 'min' and threshold_mode == 'rel':
            self.is_better = self.is_better_min_rel
        elif mode == 'min' and threshold_mode == 'abs':
            self.is_better = self.is_better_min_abs
        elif mode == 'max' and threshold_mode == 'rel':
            self.is_better = self.is_better_max_rel
        else:  # mode == 'min' and threshold_mode == 'abs':
            self.is_better = self.is_better_max_abs

    def get_new_bs(self, **kwargs) -> int:
        """ Returns the next batch size as an :class:`int`. Receives a metric and increases the batch size by a give
        factor if the metric has not been improved for `patience` epochs. After increasing the batch size, the
        scheduler goes through a cooldown period in which bad epochs are ignored.

        Args:
            **kwargs: All keyword arguments except 'metric' are ignored. The keyword 'metric' must be passed to the
                step() function, otherwise a TypeError would be raised.
        """
        if self.last_epoch == 0:  # Don't do anything at initialization.
            return self.batch_size

        metric = kwargs.pop('metric', None)
        if metric is None:
            raise TypeError("IncreaseBSOnPlateau requires passing a 'metric' keyword argument in the step() function.")

        current = float(metric)
        if self.is_better(current, self.best, self.threshold):
            self.best = current
            self.num_bad_epochs = 0
        else:
            self.num_bad_epochs += 1

        if self.in_cooldown:
            self.cooldown_counter -= 1
            self.num_bad_epochs = 0  # ignore any bad epochs in cooldown.

        if self.num_bad_epochs > self.patience:
            self.cooldown_counter = self.cooldown
            self.num_bad_epochs = 0
            return rint(self.batch_size * self.factor)

        return self.batch_size

    def load_state_dict(self, state_dict: dict):
        """ Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        super().load_state_dict(state_dict)
        self._init_is_better(self.mode, self.threshold_mode)

in_cooldown: bool property

Returns True if scheduler is in cooldown, False otherwise.

get_new_bs(**kwargs)

Returns the next batch size as an :class:int. Receives a metric and increases the batch size by a give factor if the metric has not been improved for patience epochs. After increasing the batch size, the scheduler goes through a cooldown period in which bad epochs are ignored.

Parameters:

Name Type Description Default
**kwargs

All keyword arguments except 'metric' are ignored. The keyword 'metric' must be passed to the step() function, otherwise a TypeError would be raised.

{}
Source code in bs_scheduler\batch_size_schedulers.py
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
def get_new_bs(self, **kwargs) -> int:
    """ Returns the next batch size as an :class:`int`. Receives a metric and increases the batch size by a give
    factor if the metric has not been improved for `patience` epochs. After increasing the batch size, the
    scheduler goes through a cooldown period in which bad epochs are ignored.

    Args:
        **kwargs: All keyword arguments except 'metric' are ignored. The keyword 'metric' must be passed to the
            step() function, otherwise a TypeError would be raised.
    """
    if self.last_epoch == 0:  # Don't do anything at initialization.
        return self.batch_size

    metric = kwargs.pop('metric', None)
    if metric is None:
        raise TypeError("IncreaseBSOnPlateau requires passing a 'metric' keyword argument in the step() function.")

    current = float(metric)
    if self.is_better(current, self.best, self.threshold):
        self.best = current
        self.num_bad_epochs = 0
    else:
        self.num_bad_epochs += 1

    if self.in_cooldown:
        self.cooldown_counter -= 1
        self.num_bad_epochs = 0  # ignore any bad epochs in cooldown.

    if self.num_bad_epochs > self.patience:
        self.cooldown_counter = self.cooldown
        self.num_bad_epochs = 0
        return rint(self.batch_size * self.factor)

    return self.batch_size

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
1199
1200
1201
1202
1203
1204
1205
1206
def load_state_dict(self, state_dict: dict):
    """ Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    super().load_state_dict(state_dict)
    self._init_is_better(self.mode, self.threshold_mode)

LambdaBS

Bases: BSScheduler

Sets the batch size to the initial batch size times a given function. Unlike torch.optim.lr_scheduler.LambdaLR, there is a single batch size for a given dataloader so only one function should be passed as a parameter.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
bs_lambda Callable[[int], float]

A function which computes a multiplicative factor given an integer parameter epoch.

required
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> func = lambda epoch: 1.05 ** epoch
>>> scheduler = LambdaBS(dataloader, bs_lambda=func)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
class LambdaBS(BSScheduler):
    """ Sets the batch size to the initial batch size times a given function. Unlike torch.optim.lr_scheduler.LambdaLR,
    there is a single batch size for a given dataloader so only one function should be passed as a parameter.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        bs_lambda (Callable[[int], float]): A function which computes a multiplicative factor given an integer
            parameter epoch.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> func = lambda epoch: 1.05 ** epoch
        >>> scheduler = LambdaBS(dataloader, bs_lambda=func)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, bs_lambda: Callable[[int], float],
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert callable(bs_lambda)
        self.bs_lambda: Callable[[int], float] = bs_lambda
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader. The batch size lambda
        function will only be saved if they are callable objects and not if they are functions or lambdas.
        """
        state_dict = super().state_dict()
        state_dict['bs_lambda'] = None
        if not isinstance(self.bs_lambda, types.FunctionType):
            state_dict['bs_lambda'] = self.bs_lambda.__dict__.copy()
        return state_dict

    def load_state_dict(self, state_dict: dict):
        """Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        bs_lambda = state_dict.pop('bs_lambda')
        super().load_state_dict(state_dict)
        if bs_lambda is not None:
            self.bs_lambda.__dict__.update(bs_lambda)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. It is calculated as the initial value of the batch size
        times the factor returned by `bs_lambda`.
        """
        return rint(self.dataloader._base_batch_size * self.bs_lambda(self.last_epoch))

get_new_bs()

Returns the next batch size as an :class:int. It is calculated as the initial value of the batch size times the factor returned by bs_lambda.

Source code in bs_scheduler\batch_size_schedulers.py
327
328
329
330
331
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. It is calculated as the initial value of the batch size
    times the factor returned by `bs_lambda`.
    """
    return rint(self.dataloader._base_batch_size * self.bs_lambda(self.last_epoch))

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
316
317
318
319
320
321
322
323
324
325
def load_state_dict(self, state_dict: dict):
    """Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    bs_lambda = state_dict.pop('bs_lambda')
    super().load_state_dict(state_dict)
    if bs_lambda is not None:
        self.bs_lambda.__dict__.update(bs_lambda)

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader. The batch size lambda function will only be saved if they are callable objects and not if they are functions or lambdas.

Source code in bs_scheduler\batch_size_schedulers.py
304
305
306
307
308
309
310
311
312
313
314
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader. The batch size lambda
    function will only be saved if they are callable objects and not if they are functions or lambdas.
    """
    state_dict = super().state_dict()
    state_dict['bs_lambda'] = None
    if not isinstance(self.bs_lambda, types.FunctionType):
        state_dict['bs_lambda'] = self.bs_lambda.__dict__.copy()
    return state_dict

LinearBS

Bases: BSScheduler

Increases the batch size by a linearly changing small multiplicative factor until the number of epochs reaches a pre-defined milestone.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
start_factor float

The number we multiply the batch size in the first epoch. The multiplication factor changes towards end_factor in the following epochs. Default: 3.0.

3.0
end_factor float

The number we multiply the batch size at the end of the linear changing process. Default: 1.0.

1.0
milestone int

The number of steps that the scheduler increases the learning rate. Default: 5.

5
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 60 if epoch == 0
>>> # bs = 50 if epoch == 1
>>> # bs = 40 if epoch == 2
>>> # bs = 30 if epoch == 3
>>> # bs = 20 if epoch == 4
>>> # bs = 10 if epoch >= 5
>>> scheduler = LinearBS(dataloader, start_factor=6.0, end_factor=1.0, milestone=5)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
class LinearBS(BSScheduler):
    """ Increases the batch size by a linearly changing small multiplicative factor until the number of epochs reaches
    a pre-defined milestone.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        start_factor (float): The number we multiply the batch size in the first epoch. The multiplication factor
            changes towards end_factor in the following epochs. Default: 3.0.
        end_factor (float): The number we multiply the batch size at the end of the linear changing process.
                Default: 1.0.
        milestone (int): The number of steps that the scheduler increases the learning rate. Default: 5.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 60 if epoch == 0
        >>> # bs = 50 if epoch == 1
        >>> # bs = 40 if epoch == 2
        >>> # bs = 30 if epoch == 3
        >>> # bs = 20 if epoch == 4
        >>> # bs = 10 if epoch >= 5
        >>> scheduler = LinearBS(dataloader, start_factor=6.0, end_factor=1.0, milestone=5)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, start_factor: float = 3.0, end_factor: float = 1.0, milestone: int = 5,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(milestone, int) and milestone > 0
        assert start_factor > 0.0 and end_factor > 0.0
        # Both start_factor and end_factor are expected to be greater than 1.0, with start_factor > end_factor, as this
        # should be a warmup process. But we do not forbid any other sound combinations.
        self.start_factor: float = start_factor
        self.end_factor: float = end_factor
        self.milestone: int = milestone
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. The current batch size is multiplied by the linear changing
        factor, starting from start_factor to end_factor. After the milestone is reached, the batch size is not changed
        anymore.
        """
        if self.last_epoch > self.milestone:
            self._finished = True  # My job is done.
            return self.batch_size

        if self.last_epoch == 0:
            return rint(self.batch_size * self.start_factor)

        value_range = self.end_factor - self.start_factor
        return rint(self.batch_size * (
                1.0 + value_range / (self.milestone * self.start_factor + (self.last_epoch - 1) * value_range)))

get_new_bs()

Returns the next batch size as an :class:int. The current batch size is multiplied by the linear changing factor, starting from start_factor to end_factor. After the milestone is reached, the batch size is not changed anymore.

Source code in bs_scheduler\batch_size_schedulers.py
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. The current batch size is multiplied by the linear changing
    factor, starting from start_factor to end_factor. After the milestone is reached, the batch size is not changed
    anymore.
    """
    if self.last_epoch > self.milestone:
        self._finished = True  # My job is done.
        return self.batch_size

    if self.last_epoch == 0:
        return rint(self.batch_size * self.start_factor)

    value_range = self.end_factor - self.start_factor
    return rint(self.batch_size * (
            1.0 + value_range / (self.milestone * self.start_factor + (self.last_epoch - 1) * value_range)))

MultiStepBS

Bases: BSScheduler

Multiplies the batch size by gamma once the number of epochs reaches one of the milestones.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
milestones Sequence[int]

Sequence of epoch indices.

required
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if epoch < 30
>>> # bs = 20 if 25 <= epoch < 80
>>> # bs = 40 if 80 <= epoch
>>> scheduler = MultiStepBS(dataloader, milestones=[25, 80], gamma=2.0)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
class MultiStepBS(BSScheduler):
    """ Multiplies the batch size by gamma once the number of epochs reaches one of the milestones.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        milestones (Sequence[int]): Sequence of epoch indices.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if epoch < 30
        >>> # bs = 20 if 25 <= epoch < 80
        >>> # bs = 40 if 80 <= epoch
        >>> scheduler = MultiStepBS(dataloader, milestones=[25, 80], gamma=2.0)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, milestones: Sequence[int], gamma: float = 2.0,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(milestones, (tuple, list))
        assert len(milestones) > 0 and all([x > 0 and isinstance(x, int) for x in milestones])
        assert gamma > 0.0
        # Gamma is expected to be greater than 1, but we do not forbid batch size decay.
        # We do not require milestones to be sorted. However, sorted looks better.
        self.milestones: Counter[int, int] = Counter(milestones)
        self.gamma: float = gamma
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    @property
    def finished(self) -> bool:
        """ Returns True if the scheduler has already finished its job or has exceeded the minimum or maximum batch
        size. Otherwise, returns False.
        """
        if not self._finished:
            # Should we cache max(self.milestones)?
            self._finished = self.last_epoch > max(self.milestones)
        return self._finished

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. It returns the current batch size times gamma each epoch a
        milestone is reached, otherwise it returns the current batch size. Beware that in the event of multiple
        milestones with the same value, the current batch size is multiplied with gamma multiple times.
        """
        if self.last_epoch not in self.milestones:
            return self.batch_size
        return rint(self.batch_size * self.gamma ** self.milestones[self.last_epoch])

finished: bool property

Returns True if the scheduler has already finished its job or has exceeded the minimum or maximum batch size. Otherwise, returns False.

get_new_bs()

Returns the next batch size as an :class:int. It returns the current batch size times gamma each epoch a milestone is reached, otherwise it returns the current batch size. Beware that in the event of multiple milestones with the same value, the current batch size is multiplied with gamma multiple times.

Source code in bs_scheduler\batch_size_schedulers.py
495
496
497
498
499
500
501
502
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. It returns the current batch size times gamma each epoch a
    milestone is reached, otherwise it returns the current batch size. Beware that in the event of multiple
    milestones with the same value, the current batch size is multiplied with gamma multiple times.
    """
    if self.last_epoch not in self.milestones:
        return self.batch_size
    return rint(self.batch_size * self.gamma ** self.milestones[self.last_epoch])

MultiplicativeBS

Bases: BSScheduler

Multiply the batch size by a factor given in the specified function. Unlike torch.optim.lr_scheduler.MultiplicativeLR, there is a single batch size for a given dataloader so only one function should be passed as a parameter.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
bs_lambda Callable[[int], float]

A function which computes a multiplicative factor given an integer parameter epoch.

required
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> func = lambda epoch: 1.05
>>> scheduler = MultiplicativeBS(dataloader, bs_lambda=func)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
class MultiplicativeBS(BSScheduler):
    """ Multiply the batch size by a factor given in the specified function. Unlike
    torch.optim.lr_scheduler.MultiplicativeLR, there is a single batch size for a given dataloader so only one function
    should be passed as a parameter.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        bs_lambda (Callable[[int], float]): A function which computes a multiplicative factor given an integer
            parameter epoch.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> func = lambda epoch: 1.05
        >>> scheduler = MultiplicativeBS(dataloader, bs_lambda=func)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, bs_lambda: Callable[[int], float],
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert callable(bs_lambda)
        self.bs_lambda: Callable[[int], float] = bs_lambda
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader. The batch size lambda
        function will only be saved if they are callable objects and not if they are functions or lambdas.
        """
        state_dict = super().state_dict()
        state_dict['bs_lambda'] = None
        if not isinstance(self.bs_lambda, types.FunctionType):
            state_dict['bs_lambda'] = self.bs_lambda.__dict__.copy()
        return state_dict

    def load_state_dict(self, state_dict: dict):
        """Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        bs_lambda = state_dict.pop('bs_lambda')
        super().load_state_dict(state_dict)
        if bs_lambda is not None:
            self.bs_lambda.__dict__.update(bs_lambda)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. It is calculated as the current value of the batch size
        times the factor returned by `bs_lambda`.
        """
        return rint(self.batch_size * self.bs_lambda(self.last_epoch))

get_new_bs()

Returns the next batch size as an :class:int. It is calculated as the current value of the batch size times the factor returned by bs_lambda.

Source code in bs_scheduler\batch_size_schedulers.py
391
392
393
394
395
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. It is calculated as the current value of the batch size
    times the factor returned by `bs_lambda`.
    """
    return rint(self.batch_size * self.bs_lambda(self.last_epoch))

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
380
381
382
383
384
385
386
387
388
389
def load_state_dict(self, state_dict: dict):
    """Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    bs_lambda = state_dict.pop('bs_lambda')
    super().load_state_dict(state_dict)
    if bs_lambda is not None:
        self.bs_lambda.__dict__.update(bs_lambda)

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader. The batch size lambda function will only be saved if they are callable objects and not if they are functions or lambdas.

Source code in bs_scheduler\batch_size_schedulers.py
368
369
370
371
372
373
374
375
376
377
378
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader. The batch size lambda
    function will only be saved if they are callable objects and not if they are functions or lambdas.
    """
    state_dict = super().state_dict()
    state_dict['bs_lambda'] = None
    if not isinstance(self.bs_lambda, types.FunctionType):
        state_dict['bs_lambda'] = self.bs_lambda.__dict__.copy()
    return state_dict

OneCycleBS

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.OneCycleLR. Sets the batch size according to the one cycle batch size policy, inspired from the 1cycle learning rate policy. The one cycle batch size policy decreases the batch size from the base_batch_size to some minimum batch size and that it increases it to some maximum batch size bigger than the base_batch_size. This policy is inspired from the policy described in the paper Super-Convergence: Very Fast Training of Neural Networks Using Large Learning Rates_. It only uses two phases (base -> min, min -> max) instead of the three phases described in the paper (base -> min, min -> base, base -> max).

The once cycle batch size policy changes the batch size after every batch. The step() function should be called after a batch has been used for training. But it may also be called after every epoch and the total_steps should be adjusted accordingly.

This scheduler is not chainable.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
total_steps int

The total number of steps in the cycle.

required
decay_percentage float

The fraction of the cycle spend decreasing the batch size. 1 - decay_percentage will be spent increasing the batch size. Default: 0.3.

0.3
base_batch_size Union[int, None]

The base batch size. If None, the base batch size will be retrieved from the dataloader. Default: None.

None
strategy str

One of cos, linear. Specifies the strategy used for annealing the batch size, 'cos' for cosine annealing, 'linear' for linear annealing. Default: 'cos'.

'cos'
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> scheduler = OneCycleBS(dataloader, total_steps=1000)
>>> for epoch in range(100):
>>>     for batch in dataloader:
>>>         train_batch(...)
>>>         scheduler.step()

.. _Super-Convergence\: Very Fast Training of Neural Networks Using Large Learning Rates: https://arxiv.org/abs/1708.07120

Source code in bs_scheduler\batch_size_schedulers.py
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
class OneCycleBS(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.OneCycleLR. Sets the batch size according to the one cycle batch size
    policy, inspired from the 1cycle learning rate policy. The one cycle batch size policy decreases the batch size
    from the base_batch_size to some minimum batch size and that it increases it to some maximum batch size bigger than
    the base_batch_size.
    This policy is inspired from the policy described in the paper `Super-Convergence: Very Fast Training of Neural
    Networks Using Large Learning Rates`_. It only uses two phases (base -> min, min -> max) instead of the three
    phases described in the paper (base -> min, min -> base, base -> max).

    The once cycle batch size policy changes the batch size after every batch. The step() function should be called
    after a batch has been used for training. But it may also be called after every epoch and the total_steps should be
    adjusted accordingly.

    This scheduler is not chainable.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        total_steps (int): The total number of steps in the cycle.
        decay_percentage (float): The fraction of the cycle spend decreasing the batch size. 1 - decay_percentage will
            be spent increasing the batch size. Default: 0.3.
        base_batch_size (Union[int, None]): The base batch size. If None, the base batch size will be retrieved from
            the dataloader. Default: None.
        strategy (str): One of `cos`, `linear`. Specifies the strategy used for annealing the batch size, 'cos' for
            cosine annealing, 'linear' for linear annealing. Default: 'cos'.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
            >>> dataloader = ...
            >>> scheduler = OneCycleBS(dataloader, total_steps=1000)
            >>> for epoch in range(100):
            >>>     for batch in dataloader:
            >>>         train_batch(...)
            >>>         scheduler.step()

    .. _Super-Convergence\\: Very Fast Training of Neural Networks Using Large Learning Rates:
        https://arxiv.org/abs/1708.07120
    """

    def __init__(self, dataloader: DataLoader, total_steps: int, decay_percentage: float = 0.3,
                 base_batch_size: Union[int, None] = None, strategy: str = 'cos',
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(total_steps, int)
        assert isinstance(decay_percentage, float) and 0 < decay_percentage < 1
        assert rint(total_steps * decay_percentage) > 0 and total_steps - rint(total_steps * decay_percentage) > 0
        assert base_batch_size is None or (isinstance(base_batch_size, int) and base_batch_size > min_batch_size)
        assert strategy in ('cos', 'linear')

        self.end_step_1: int = rint(total_steps * decay_percentage)
        self.end_step_2: int = total_steps - self.end_step_1

        self.strategy: str = strategy
        if strategy == 'cos':
            self.anneal_fn = self._annealing_cos
        else:
            self.anneal_fn = self._annealing_linear

        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)
        self.base_batch_size: int = self.dataloader._base_batch_size if base_batch_size is None else base_batch_size
        assert self.max_batch_size > self.base_batch_size

    @staticmethod
    def _annealing_cos(start, end, percentage):
        """ Cosine annealing from start to end as percentage goes from 0.0 to 1.0.
        """
        return end + (start - end) / 2.0 * (1 + math.cos(math.pi * percentage))

    @staticmethod
    def _annealing_linear(start, end, percentage):
        """ Linear annealing from start to end as percentage goes from 0.0 to 1.0.
        """
        return (end - start) * percentage + start

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
        batch and restarts. The implementation is similar to
        torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of `eta_min` we use max_batch_size, and we
        increase the batch size instead of decreasing the learning rate. We clip the values to always remain within
        bound.
        """
        if self.last_epoch == 0:  # Don't do anything at initialization.
            return self.batch_size

        if self.last_epoch <= self.end_step_1:
            # Phase 1
            percentage = self.last_epoch / self.end_step_1
            new_bs = self.anneal_fn(self.base_batch_size, self.min_batch_size, percentage)
        else:
            # Phase 2
            percentage = (self.last_epoch - self.end_step_1) / self.end_step_2
            new_bs = self.anneal_fn(self.min_batch_size, self.max_batch_size, percentage)

            if percentage == 1.0:
                self._finished = True

        return clip(rint(new_bs), min=self.min_batch_size, max=self.max_batch_size)

get_new_bs()

Returns the next batch size as an :class:int. Increases the batch size from base batch size to maximum batch and restarts. The implementation is similar to torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of eta_min we use max_batch_size, and we increase the batch size instead of decreasing the learning rate. We clip the values to always remain within bound.

Source code in bs_scheduler\batch_size_schedulers.py
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. Increases the batch size from base batch size to maximum
    batch and restarts. The implementation is similar to
    torch.optim.lr_scheduler.CosineAnnealingWarmRestarts, but instead of `eta_min` we use max_batch_size, and we
    increase the batch size instead of decreasing the learning rate. We clip the values to always remain within
    bound.
    """
    if self.last_epoch == 0:  # Don't do anything at initialization.
        return self.batch_size

    if self.last_epoch <= self.end_step_1:
        # Phase 1
        percentage = self.last_epoch / self.end_step_1
        new_bs = self.anneal_fn(self.base_batch_size, self.min_batch_size, percentage)
    else:
        # Phase 2
        percentage = (self.last_epoch - self.end_step_1) / self.end_step_2
        new_bs = self.anneal_fn(self.min_batch_size, self.max_batch_size, percentage)

        if percentage == 1.0:
            self._finished = True

    return clip(rint(new_bs), min=self.min_batch_size, max=self.max_batch_size)

PolynomialBS

Bases: BSScheduler

Increases the batch size using a polynomial function in the given total_iters. Unlike torch.optim.lr_scheduler.PolynomialLR whose polynomial factor decays from 1.0 to 0.5 ** power, in this case the polynomial factor decays from 1.5 ** power to 1.0.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
total_iters int

The number of steps that the scheduler increases the batch size.

required
power float

The power of the polynomial. Default: 1.0.

1.0
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if epoch == 0
>>> # bs = 10 * 1.25 if epoch == 1
>>> # bs = 12 * 1.33 if epoch == 2
>>> # bs = 16 * 1.50 if epoch == 3
>>> # bs = 24 * 2.00 if epoch == 4
>>> # bs = 48 if epoch >= 5
>>> scheduler = PolynomialBS(dataloader, total_iters=5, power=1.0)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
class PolynomialBS(BSScheduler):
    """ Increases the batch size using a polynomial function in the given total_iters. Unlike
    torch.optim.lr_scheduler.PolynomialLR whose polynomial factor decays from 1.0 to 0.5 ** power, in this case the
    polynomial factor decays from 1.5 ** power to 1.0.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        total_iters (int): The number of steps that the scheduler increases the batch size.
        power (float): The power of the polynomial. Default: 1.0.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if epoch == 0
        >>> # bs = 10 * 1.25 if epoch == 1
        >>> # bs = 12 * 1.33 if epoch == 2
        >>> # bs = 16 * 1.50 if epoch == 3
        >>> # bs = 24 * 2.00 if epoch == 4
        >>> # bs = 48 if epoch >= 5
        >>> scheduler = PolynomialBS(dataloader, total_iters=5, power=1.0)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, total_iters: int, power: float = 1.0,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(total_iters, int) and total_iters > 1

        self.total_iters: int = total_iters
        self.power: float = power
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. From epoch 1 to total_iters - 1, the current batch size is
        multiplied by an increasing polynomial factor.
        """
        if self.last_epoch == 0 or self.last_epoch >= self.total_iters:
            self._finished = self.last_epoch >= self.total_iters
            return self.batch_size

        remaining_steps = self.total_iters - self.last_epoch
        factor = 2.0 - ((1.0 - remaining_steps / self.total_iters) / (
                1.0 - (remaining_steps - 1) / self.total_iters)) ** self.power
        return rint(self.batch_size * factor)

get_new_bs()

Returns the next batch size as an :class:int. From epoch 1 to total_iters - 1, the current batch size is multiplied by an increasing polynomial factor.

Source code in bs_scheduler\batch_size_schedulers.py
859
860
861
862
863
864
865
866
867
868
869
870
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. From epoch 1 to total_iters - 1, the current batch size is
    multiplied by an increasing polynomial factor.
    """
    if self.last_epoch == 0 or self.last_epoch >= self.total_iters:
        self._finished = self.last_epoch >= self.total_iters
        return self.batch_size

    remaining_steps = self.total_iters - self.last_epoch
    factor = 2.0 - ((1.0 - remaining_steps / self.total_iters) / (
            1.0 - (remaining_steps - 1) / self.total_iters)) ** self.power
    return rint(self.batch_size * factor)

SequentialBS

Bases: BSScheduler

Similar to torch.optim.lr_scheduler.SequentialLR. Receives a sequence of schedulers and calls them sequentially given the milestone points that reflect which scheduler is supposed to be called at a fiven epoch

Parameters:

Name Type Description Default
schedulers Sequence[BSScheduler]

Sequence of batch size schedulers. We expect the first scheduler to have been initialized first.

required
milestones Sequence[int]

Sequence of integers that reflects the milestone points. Must be sorted in a non-descending order.

Sequence[int]

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 100 if epoch == 0
>>> # bs = 100 if epoch == 1
>>> # bs = 100 if epoch == 2
>>> # bs = 100 if epoch == 3
>>> # bs = 10 if epoch == 4
>>> # bs = 11 if epoch == 5
>>> # ...
>>> scheduler1 = ConstantBS(dataloader, factor=10, milestone=4)
>>> scheduler2 = ExponentialBS(dataloader, gamma=1.1)
>>> scheduler = SequentialBS(dataloader, schedulers=[scheduler1, scheduler2], milestones=[5])
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
class SequentialBS(BSScheduler):
    """ Similar to torch.optim.lr_scheduler.SequentialLR. Receives a sequence of schedulers and calls them sequentially
    given the milestone points that reflect which scheduler is supposed to be called at a fiven epoch

    Args:
        schedulers (Sequence[BSScheduler]): Sequence of batch size schedulers. We expect the first scheduler to have
            been initialized first.
        milestones (Sequence[int]): Sequence of integers that reflects the milestone points. Must be sorted in a
            non-descending order.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 100 if epoch == 0
        >>> # bs = 100 if epoch == 1
        >>> # bs = 100 if epoch == 2
        >>> # bs = 100 if epoch == 3
        >>> # bs = 10 if epoch == 4
        >>> # bs = 11 if epoch == 5
        >>> # ...
        >>> scheduler1 = ConstantBS(dataloader, factor=10, milestone=4)
        >>> scheduler2 = ExponentialBS(dataloader, gamma=1.1)
        >>> scheduler = SequentialBS(dataloader, schedulers=[scheduler1, scheduler2], milestones=[5])
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, schedulers: Sequence[BSScheduler], milestones=Sequence[int]):

        assert isinstance(schedulers, (tuple, list)) and len(schedulers) >= 2 and all(
            [isinstance(x, BSScheduler) for x in schedulers])
        assert isinstance(milestones, (tuple, list)) and len(milestones) >= 1 and all(
            [isinstance(x, int) for x in milestones]) and milestones[0] > 0
        assert all([milestones[i] >= milestones[i - 1] for i in range(1, len(milestones))]), \
            f"Milestones must be sorted, are {milestones}"

        if len(milestones) != len(schedulers) - 1:
            raise ValueError(f"SequentialBS expects the number of schedulers provided to be one more than the number "
                             f"of milestone points, but got {len(schedulers)} and the number of milestones is "
                             f"{len(milestones)}")

        super().__init__(schedulers[0].dataloader, schedulers[0].batch_size_manager, schedulers[0].max_batch_size,
                         schedulers[0].min_batch_size, verbose=False)

        for i in range(len(schedulers)):
            if schedulers[i].dataloader != self.dataloader:
                raise ValueError(f"SequentialBS expects all schedulers to belong to the same dataloader, but got "
                                 f"scheduler at index {i} to be different than the scheduler at index 0.")
            if not isinstance(schedulers[i].batch_size_manager, type(self.batch_size_manager)):
                raise ValueError(f"SequentialBS expects all schedulers to have the same batch size manager, but got "
                                 f"scheduler at index {i} to have a different batch size manager. Expected type of "
                                 f"batch size manager: {type(self.batch_size_manager).__name__}, got: "
                                 f"{type(schedulers[i].batch_size_manager).__name__}.")

            if schedulers[i].max_batch_size > self.max_batch_size:
                self.max_batch_size = schedulers[i].max_batch_size
            if schedulers[i].min_batch_size < self.min_batch_size:
                self.min_batch_size = schedulers[i].min_batch_size

            # Undoing the steps done by the schedulers.
            schedulers[i]._last_bs = self.dataloader._base_batch_size
            schedulers[i].last_epoch -= 1

        self.set_batch_size(self.dataloader._base_batch_size)  # Set the batch size back to initial value.

        self.schedulers: Tuple[BSScheduler, ...] = tuple(schedulers)
        self.milestones: Tuple[int, ...] = tuple(milestones)
        # Do the initial step again, but only for the first scheduler.
        self.schedulers[0].step()

    @property
    def finished(self) -> bool:
        """ Returns True if all the schedulers have already finished their job or have exceeded the minimum or maximum
        batch size. Otherwise, returns False.
        """
        if not self._finished:
            # The last milestone was reached and the last scheduler is finished.
            self._finished = self.last_epoch > self.milestones[-1] and self.schedulers[-1].finished
        return self._finished

    def step(self, **kwargs):
        """ Performs the step method for each scheduler until a milestone point is reached and a new scheduler is to be
        used. The new scheduler is used as if it is called for the first time.

        Args:
            **kwargs: All kwargs are passed to each scheduler.
        """
        self.last_epoch += 1  # We still increase last_epoch, even though the scheduler has finished its job. It should
        # not really matter.
        if self.last_epoch == 0 or self.finished:
            return
        i = bisect_right(self.milestones, self.last_epoch)
        scheduler = self.schedulers[i]
        if i > 0 and self.milestones[i - 1] == self.last_epoch:
            scheduler.last_epoch = 0
        if not scheduler.finished:
            scheduler.step(**kwargs)
            self._last_bs = scheduler.last_bs

    def state_dict(self) -> dict:
        """ Returns the state of the scheduler as a :class:`dict`.

        It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
        states will also be saved.
        """
        state_dict = super().state_dict()
        state_dict['schedulers'] = [None] * len(self.schedulers)

        for i, s in enumerate(self.schedulers):
            state_dict['schedulers'][i] = s.state_dict()

        return state_dict

    def load_state_dict(self, state_dict: dict):
        """ Loads the schedulers state.

        Args:
            state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
        """
        schedulers = state_dict.pop('schedulers')
        self.__dict__.update(state_dict)

        state_dict['schedulers'] = schedulers
        for i, s in enumerate(schedulers):
            self.schedulers[i].load_state_dict(s)

        self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.

finished: bool property

Returns True if all the schedulers have already finished their job or have exceeded the minimum or maximum batch size. Otherwise, returns False.

load_state_dict(state_dict)

Loads the schedulers state.

Parameters:

Name Type Description Default
state_dict dict

scheduler state. Should be an object returned from a call to :meth:state_dict.

required
Source code in bs_scheduler\batch_size_schedulers.py
801
802
803
804
805
806
807
808
809
810
811
812
813
814
def load_state_dict(self, state_dict: dict):
    """ Loads the schedulers state.

    Args:
        state_dict (dict): scheduler state. Should be an object returned from a call to :meth:`state_dict`.
    """
    schedulers = state_dict.pop('schedulers')
    self.__dict__.update(state_dict)

    state_dict['schedulers'] = schedulers
    for i, s in enumerate(schedulers):
        self.schedulers[i].load_state_dict(s)

    self.set_batch_size(self.last_bs)  # Setting the batch size to the last computed batch size.

state_dict()

Returns the state of the scheduler as a :class:dict.

It contains an entry for every variable in self.dict which is not the dataloader. The wrapped scheduler states will also be saved.

Source code in bs_scheduler\batch_size_schedulers.py
787
788
789
790
791
792
793
794
795
796
797
798
799
def state_dict(self) -> dict:
    """ Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which is not the dataloader. The wrapped scheduler
    states will also be saved.
    """
    state_dict = super().state_dict()
    state_dict['schedulers'] = [None] * len(self.schedulers)

    for i, s in enumerate(self.schedulers):
        state_dict['schedulers'][i] = s.state_dict()

    return state_dict

step(**kwargs)

Performs the step method for each scheduler until a milestone point is reached and a new scheduler is to be used. The new scheduler is used as if it is called for the first time.

Parameters:

Name Type Description Default
**kwargs

All kwargs are passed to each scheduler.

{}
Source code in bs_scheduler\batch_size_schedulers.py
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
def step(self, **kwargs):
    """ Performs the step method for each scheduler until a milestone point is reached and a new scheduler is to be
    used. The new scheduler is used as if it is called for the first time.

    Args:
        **kwargs: All kwargs are passed to each scheduler.
    """
    self.last_epoch += 1  # We still increase last_epoch, even though the scheduler has finished its job. It should
    # not really matter.
    if self.last_epoch == 0 or self.finished:
        return
    i = bisect_right(self.milestones, self.last_epoch)
    scheduler = self.schedulers[i]
    if i > 0 and self.milestones[i - 1] == self.last_epoch:
        scheduler.last_epoch = 0
    if not scheduler.finished:
        scheduler.step(**kwargs)
        self._last_bs = scheduler.last_bs

StepBS

Bases: BSScheduler

Multiplies the batch size by gamma every step_size epochs.

Parameters:

Name Type Description Default
dataloader DataLoader

Wrapped dataloader.

required
step_size int

Period of batch size growth.

required
gamma float

Multiplicative factor of batch size growth. Default: 2.0.

2.0
batch_size_manager Union[BatchSizeManager, None]

If not None, a custom class which manages the batch size, which provides a getter and setter for the batch size. Default: None.

None
max_batch_size Union[int, None]

Upper limit for the batch size so that a batch of size max_batch_size fits in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size is set to len(self.dataloader.dataset). Default: None.

None
min_batch_size int

Lower limit for the batch size which must be greater than 0. Default: 1.

1
verbose bool

If True, prints a message to stdout for each update. Default: False.

False

Examples:

>>> dataloader = ...
>>> # Assuming the base batch size is 10.
>>> # bs = 10 if epoch < 30
>>> # bs = 20 if 30 <= epoch < 60
>>> # bs = 40 if 60 <= epoch < 90
>>> # ...
>>> scheduler = StepBS(dataloader, step_size=30, gamma=2.0)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()
Source code in bs_scheduler\batch_size_schedulers.py
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
class StepBS(BSScheduler):
    """ Multiplies the batch size by gamma every step_size epochs.

    Args:
        dataloader (DataLoader): Wrapped dataloader.
        step_size (int): Period of batch size growth.
        gamma (float): Multiplicative factor of batch size growth. Default: 2.0.
        batch_size_manager (Union[BatchSizeManager, None]): If not None, a custom class which manages the batch size,
            which provides a getter and setter for the batch size. Default: None.
        max_batch_size (Union[int, None]): Upper limit for the batch size so that a batch of size max_batch_size fits
            in the memory. If None or greater than the lenght of the dataset wrapped by the dataloader, max_batch_size
            is set to `len(self.dataloader.dataset)`. Default: None.
        min_batch_size (int): Lower limit for the batch size which must be greater than 0. Default: 1.
        verbose (bool): If ``True``, prints a message to stdout for each update. Default: ``False``.

    Examples:
        >>> dataloader = ...
        >>> # Assuming the base batch size is 10.
        >>> # bs = 10 if epoch < 30
        >>> # bs = 20 if 30 <= epoch < 60
        >>> # bs = 40 if 60 <= epoch < 90
        >>> # ...
        >>> scheduler = StepBS(dataloader, step_size=30, gamma=2.0)
        >>> for epoch in range(100):
        >>>     train(...)
        >>>     validate(...)
        >>>     scheduler.step()
    """

    def __init__(self, dataloader: DataLoader, step_size: int, gamma: float = 2.0,
                 batch_size_manager: Union[BatchSizeManager, None] = None, max_batch_size: Union[int, None] = None,
                 min_batch_size: int = 1, verbose: bool = False):
        assert isinstance(step_size, int) and step_size > 0
        assert gamma > 0.0
        # Gamma is expected to be greater than 1, but we do not forbid batch size decay.
        self.step_size: int = step_size
        self.gamma: float = gamma
        super().__init__(dataloader, batch_size_manager, max_batch_size, min_batch_size, verbose)

    def get_new_bs(self) -> int:
        """ Returns the next batch size as an :class:`int`. It returns the current batch size times gamma each
        step_size epochs, otherwise it returns the current batch size.
        """
        if self.last_epoch == 0 or self.last_epoch % self.step_size != 0:
            return self.batch_size
        return rint(self.batch_size * self.gamma)

get_new_bs()

Returns the next batch size as an :class:int. It returns the current batch size times gamma each step_size epochs, otherwise it returns the current batch size.

Source code in bs_scheduler\batch_size_schedulers.py
437
438
439
440
441
442
443
def get_new_bs(self) -> int:
    """ Returns the next batch size as an :class:`int`. It returns the current batch size times gamma each
    step_size epochs, otherwise it returns the current batch size.
    """
    if self.last_epoch == 0 or self.last_epoch % self.step_size != 0:
        return self.batch_size
    return rint(self.batch_size * self.gamma)