|
| 1 | +import math |
1 | 2 | import unittest |
2 | 3 | import functools |
3 | 4 | from copy import deepcopy |
|
8 | 9 | from torch.optim import SGD |
9 | 10 | from torch.autograd import Variable |
10 | 11 | from torch import sparse |
11 | | -from torch.optim.lr_scheduler import LambdaLR, StepLR, MultiStepLR, ExponentialLR, ReduceLROnPlateau |
| 12 | +from torch.optim.lr_scheduler import LambdaLR, StepLR, MultiStepLR, ExponentialLR, CosineAnnealingLR, ReduceLROnPlateau |
12 | 13 | from common import TestCase, run_tests |
13 | 14 |
|
14 | 15 |
|
@@ -460,117 +461,127 @@ def test_step_lr(self): |
460 | 461 | # lr = 0.05 if epoch < 3 |
461 | 462 | # lr = 0.005 if 30 <= epoch < 6 |
462 | 463 | # lr = 0.0005 if epoch >= 9 |
| 464 | + epochs = 10 |
463 | 465 | single_targets = [0.05] * 3 + [0.005] * 3 + [0.0005] * 3 + [0.00005] * 3 |
464 | | - targets = [single_targets, list(map(lambda x: x * 10, single_targets))] |
| 466 | + targets = [single_targets, list(map(lambda x: x * epochs, single_targets))] |
465 | 467 | scheduler = StepLR(self.opt, gamma=0.1, step_size=3) |
466 | | - epochs = 10 |
467 | 468 | self._test(scheduler, targets, epochs) |
468 | 469 |
|
469 | 470 | def test_multi_step_lr(self): |
470 | 471 | # lr = 0.05 if epoch < 2 |
471 | 472 | # lr = 0.005 if 2 <= epoch < 5 |
472 | 473 | # lr = 0.0005 if epoch < 9 |
473 | 474 | # lr = 0.00005 if epoch >= 9 |
| 475 | + epochs = 10 |
474 | 476 | single_targets = [0.05] * 2 + [0.005] * 3 + [0.0005] * 4 + [0.00005] * 3 |
475 | | - targets = [single_targets, list(map(lambda x: x * 10, single_targets))] |
| 477 | + targets = [single_targets, list(map(lambda x: x * epochs, single_targets))] |
476 | 478 | scheduler = MultiStepLR(self.opt, gamma=0.1, milestones=[2, 5, 9]) |
477 | | - epochs = 10 |
478 | 479 | self._test(scheduler, targets, epochs) |
479 | 480 |
|
480 | 481 | def test_exp_lr(self): |
481 | | - single_targets = [0.05 * (0.9 ** x) for x in range(10)] |
482 | | - targets = [single_targets, list(map(lambda x: x * 10, single_targets))] |
| 482 | + epochs = 10 |
| 483 | + single_targets = [0.05 * (0.9 ** x) for x in range(epochs)] |
| 484 | + targets = [single_targets, list(map(lambda x: x * epochs, single_targets))] |
483 | 485 | scheduler = ExponentialLR(self.opt, gamma=0.9) |
| 486 | + self._test(scheduler, targets, epochs) |
| 487 | + |
| 488 | + def test_cos_anneal_lr(self): |
484 | 489 | epochs = 10 |
| 490 | + eta_min = 1e-10 |
| 491 | + single_targets = [eta_min + (0.05 - eta_min) * |
| 492 | + (1 + math.cos(x / epochs * math.pi)) / 2 |
| 493 | + for x in range(epochs)] |
| 494 | + targets = [single_targets, list(map(lambda x: x * epochs, single_targets))] |
| 495 | + scheduler = CosineAnnealingLR(self.opt, T_max=epochs, eta_min=eta_min) |
485 | 496 | self._test(scheduler, targets, epochs) |
486 | 497 |
|
487 | 498 | def test_reduce_lr_on_plateau1(self): |
| 499 | + epochs = 10 |
488 | 500 | for param_group in self.opt.param_groups: |
489 | 501 | param_group['lr'] = 0.5 |
490 | 502 | targets = [[0.5] * 20] |
491 | 503 | metrics = [10 - i * 0.0167 for i in range(20)] |
492 | 504 | scheduler = ReduceLROnPlateau(self.opt, threshold_mode='abs', mode='min', |
493 | 505 | threshold=0.01, patience=5, cooldown=5) |
494 | | - epochs = 10 |
495 | 506 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
496 | 507 |
|
497 | 508 | def test_reduce_lr_on_plateau2(self): |
| 509 | + epochs = 22 |
498 | 510 | for param_group in self.opt.param_groups: |
499 | 511 | param_group['lr'] = 0.5 |
500 | 512 | targets = [[0.5] * 6 + [0.05] * 7 + [0.005] * 7 + [0.0005] * 2] |
501 | 513 | metrics = [10 - i * 0.0165 for i in range(22)] |
502 | 514 | scheduler = ReduceLROnPlateau(self.opt, patience=5, cooldown=0, threshold_mode='abs', |
503 | 515 | mode='min', threshold=0.1) |
504 | | - epochs = 22 |
505 | 516 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
506 | 517 |
|
507 | 518 | def test_reduce_lr_on_plateau3(self): |
| 519 | + epochs = 22 |
508 | 520 | for param_group in self.opt.param_groups: |
509 | 521 | param_group['lr'] = 0.5 |
510 | 522 | targets = [[0.5] * (2 + 6) + [0.05] * (5 + 6) + [0.005] * 4] |
511 | 523 | metrics = [-0.8] * 2 + [-0.234] * 20 |
512 | 524 | scheduler = ReduceLROnPlateau(self.opt, mode='max', patience=5, cooldown=5, |
513 | 525 | threshold_mode='abs') |
514 | | - epochs = 22 |
515 | 526 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
516 | 527 |
|
517 | 528 | def test_reduce_lr_on_plateau4(self): |
| 529 | + epochs = 20 |
518 | 530 | for param_group in self.opt.param_groups: |
519 | 531 | param_group['lr'] = 0.5 |
520 | 532 | targets = [[0.5] * 20] |
521 | 533 | metrics = [1.5 * (1.025 ** i) for i in range(20)] # 1.025 > 1.1**0.25 |
522 | 534 | scheduler = ReduceLROnPlateau(self.opt, mode='max', patience=3, |
523 | 535 | threshold_mode='rel', threshold=0.1) |
524 | | - epochs = 20 |
525 | 536 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
526 | 537 |
|
527 | 538 | def test_reduce_lr_on_plateau5(self): |
| 539 | + epochs = 20 |
528 | 540 | for param_group in self.opt.param_groups: |
529 | 541 | param_group['lr'] = 0.5 |
530 | 542 | targets = [[0.5] * 6 + [0.05] * (5 + 6) + [0.005] * 4] |
531 | 543 | metrics = [1.5 * (1.005 ** i) for i in range(20)] |
532 | 544 | scheduler = ReduceLROnPlateau(self.opt, mode='max', threshold_mode='rel', |
533 | 545 | threshold=0.1, patience=5, cooldown=5) |
534 | | - epochs = 20 |
535 | 546 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
536 | 547 |
|
537 | 548 | def test_reduce_lr_on_plateau6(self): |
| 549 | + epochs = 20 |
538 | 550 | for param_group in self.opt.param_groups: |
539 | 551 | param_group['lr'] = 0.5 |
540 | 552 | targets = [[0.5] * 20] |
541 | 553 | metrics = [1.5 * (0.85 ** i) for i in range(20)] |
542 | 554 | scheduler = ReduceLROnPlateau(self.opt, mode='min', threshold_mode='rel', |
543 | 555 | threshold=0.1) |
544 | | - epochs = 20 |
545 | 556 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
546 | 557 |
|
547 | 558 | def test_reduce_lr_on_plateau7(self): |
| 559 | + epochs = 20 |
548 | 560 | for param_group in self.opt.param_groups: |
549 | 561 | param_group['lr'] = 0.5 |
550 | 562 | targets = [[0.5] * 6 + [0.05] * (5 + 6) + [0.005] * 4] |
551 | 563 | metrics = [1] * 7 + [0.6] + [0.5] * 12 |
552 | 564 | scheduler = ReduceLROnPlateau(self.opt, mode='min', threshold_mode='rel', |
553 | 565 | threshold=0.1, patience=5, cooldown=5) |
554 | | - epochs = 20 |
555 | 566 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
556 | 567 |
|
557 | 568 | def test_reduce_lr_on_plateau8(self): |
| 569 | + epochs = 20 |
558 | 570 | for param_group in self.opt.param_groups: |
559 | 571 | param_group['lr'] = 0.5 |
560 | 572 | targets = [[0.5] * 6 + [0.4] * 14, [0.5] * 6 + [0.3] * 14] |
561 | 573 | metrics = [1.5 * (1.005 ** i) for i in range(20)] |
562 | 574 | scheduler = ReduceLROnPlateau(self.opt, mode='max', threshold_mode='rel', min_lr=[0.4, 0.3], |
563 | 575 | threshold=0.1, patience=5, cooldown=5) |
564 | | - epochs = 20 |
565 | 576 | self._test_reduce_lr_on_plateau(scheduler, targets, metrics, epochs) |
566 | 577 |
|
567 | 578 | def test_lambda_lr(self): |
| 579 | + epochs = 10 |
568 | 580 | self.opt.param_groups[0]['lr'] = 0.05 |
569 | 581 | self.opt.param_groups[1]['lr'] = 0.4 |
570 | | - targets = [[0.05 * (0.9 ** x) for x in range(10)], [0.4 * (0.8 ** x) for x in range(10)]] |
| 582 | + targets = [[0.05 * (0.9 ** x) for x in range(epochs)], [0.4 * (0.8 ** x) for x in range(epochs)]] |
571 | 583 | scheduler = LambdaLR(self.opt, |
572 | 584 | lr_lambda=[lambda x1: 0.9 ** x1, lambda x2: 0.8 ** x2]) |
573 | | - epochs = 10 |
574 | 585 | self._test(scheduler, targets, epochs) |
575 | 586 |
|
576 | 587 | def _test(self, scheduler, targets, epochs=10): |
|
0 commit comments