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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
187 188 189 190 191 192 193 194 195 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 |
|
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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: |
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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'
|
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'
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
1357 1358 1359 1360 1361 1362 1363 1364 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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'
|
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'
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
1199 1200 1201 1202 1203 1204 1205 1206 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
316 317 318 319 320 321 322 323 324 325 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
380 381 382 383 384 385 386 387 388 389 |
|
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 |
|
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'
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|
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 |
|
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: |
required |
Source code in bs_scheduler\batch_size_schedulers.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
|
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 |
|
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 |
|
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 |
None
|
min_batch_size |
int
|
Lower limit for the batch size which must be greater than 0. Default: 1. |
1
|
verbose |
bool
|
If |
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 |
|
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 |
|