2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-04-16 02:16:40 +00:00
|
|
|
from abc import ABC
|
|
|
|
|
|
|
|
from torch import optim
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigureOptimizersPool(ABC):
|
2020-05-24 22:59:08 +00:00
|
|
|
|
2020-04-16 02:16:40 +00:00
|
|
|
def configure_optimizers(self):
|
|
|
|
"""
|
|
|
|
return whatever optimizers we want here.
|
|
|
|
:return: list of optimizers
|
|
|
|
"""
|
2020-05-24 22:59:08 +00:00
|
|
|
optimizer = optim.Adam(self.parameters(), lr=self.learning_rate)
|
2020-04-16 02:16:40 +00:00
|
|
|
return optimizer
|
|
|
|
|
2020-05-04 20:51:39 +00:00
|
|
|
def configure_optimizers__empty(self):
|
2020-04-16 02:16:40 +00:00
|
|
|
return None
|
|
|
|
|
2020-05-02 12:38:22 +00:00
|
|
|
def configure_optimizers__lbfgs(self):
|
2020-04-16 02:16:40 +00:00
|
|
|
"""
|
|
|
|
return whatever optimizers we want here.
|
|
|
|
:return: list of optimizers
|
|
|
|
"""
|
2020-05-24 22:59:08 +00:00
|
|
|
optimizer = optim.LBFGS(self.parameters(), lr=self.learning_rate)
|
2020-04-16 02:16:40 +00:00
|
|
|
return optimizer
|
|
|
|
|
2020-10-06 13:12:29 +00:00
|
|
|
def configure_optimizers__adagrad(self):
|
|
|
|
optimizer = optim.Adagrad(self.parameters(), lr=self.learning_rate)
|
|
|
|
return optimizer
|
|
|
|
|
2020-05-02 12:38:22 +00:00
|
|
|
def configure_optimizers__multiple_optimizers(self):
|
2020-04-16 02:16:40 +00:00
|
|
|
"""
|
|
|
|
return whatever optimizers we want here.
|
|
|
|
:return: list of optimizers
|
|
|
|
"""
|
|
|
|
# try no scheduler for this model (testing purposes)
|
2020-05-24 22:59:08 +00:00
|
|
|
optimizer1 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
|
|
|
optimizer2 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
2020-04-16 02:16:40 +00:00
|
|
|
return optimizer1, optimizer2
|
|
|
|
|
2020-09-10 21:01:20 +00:00
|
|
|
def configure_optimizers__multiple_optimizers_frequency(self):
|
|
|
|
optimizer1 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
|
|
|
optimizer2 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
|
|
|
return [
|
|
|
|
{'optimizer': optimizer1, 'frequency': 1},
|
|
|
|
{'optimizer': optimizer2, 'frequency': 5}
|
|
|
|
]
|
|
|
|
|
2020-05-02 12:38:22 +00:00
|
|
|
def configure_optimizers__single_scheduler(self):
|
2020-05-24 22:59:08 +00:00
|
|
|
optimizer = optim.Adam(self.parameters(), lr=self.learning_rate)
|
2020-04-16 02:16:40 +00:00
|
|
|
lr_scheduler = optim.lr_scheduler.StepLR(optimizer, 1, gamma=0.1)
|
|
|
|
return [optimizer], [lr_scheduler]
|
|
|
|
|
2020-10-28 16:26:58 +00:00
|
|
|
def configure_optimizers__onecycle_scheduler(self):
|
|
|
|
optimizer = optim.SGD(self.parameters(), lr=self.learning_rate, momentum=0.9)
|
|
|
|
lr_scheduler = optim.lr_scheduler.OneCycleLR(optimizer,
|
|
|
|
max_lr=self.learning_rate,
|
|
|
|
total_steps=10_000)
|
|
|
|
return [optimizer], [lr_scheduler]
|
|
|
|
|
2020-05-02 12:38:22 +00:00
|
|
|
def configure_optimizers__multiple_schedulers(self):
|
2020-05-24 22:59:08 +00:00
|
|
|
optimizer1 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
|
|
|
optimizer2 = optim.Adam(self.parameters(), lr=self.learning_rate)
|
2020-04-16 02:16:40 +00:00
|
|
|
lr_scheduler1 = optim.lr_scheduler.StepLR(optimizer1, 1, gamma=0.1)
|
|
|
|
lr_scheduler2 = optim.lr_scheduler.StepLR(optimizer2, 1, gamma=0.1)
|
|
|
|
|
|
|
|
return [optimizer1, optimizer2], [lr_scheduler1, lr_scheduler2]
|
|
|
|
|
2020-05-10 21:05:34 +00:00
|
|
|
def configure_optimizers__param_groups(self):
|
|
|
|
param_groups = [
|
2020-05-24 22:59:08 +00:00
|
|
|
{'params': list(self.parameters())[:2], 'lr': self.learning_rate * 0.1},
|
|
|
|
{'params': list(self.parameters())[2:], 'lr': self.learning_rate}
|
2020-05-10 21:05:34 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
optimizer = optim.Adam(param_groups)
|
|
|
|
lr_scheduler = optim.lr_scheduler.StepLR(optimizer, 1, gamma=0.1)
|
|
|
|
return [optimizer], [lr_scheduler]
|
2020-08-06 22:34:48 +00:00
|
|
|
|
|
|
|
def configure_optimizers__lr_from_hparams(self):
|
|
|
|
optimizer = optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
|
|
|
|
return optimizer
|