3 from .optimizer
import Optimizer
7 """Implements the resilient backpropagation algorithm. 10 params (iterable): iterable of parameters to optimize or dicts defining 12 lr (float, optional): learning rate (default: 1e-2) 13 etas (Tuple[float, float], optional): pair of (etaminus, etaplis), that 14 are multiplicative increase and decrease factors 16 step_sizes (Tuple[float, float], optional): a pair of minimal and 17 maximal allowed step sizes (default: (1e-6, 50)) 20 def __init__(self, params, lr=1e-2, etas=(0.5, 1.2), step_sizes=(1e-6, 50)):
22 raise ValueError(
"Invalid learning rate: {}".format(lr))
23 if not 0.0 < etas[0] < 1.0 < etas[1]:
24 raise ValueError(
"Invalid eta values: {}, {}".format(etas[0], etas[1]))
26 defaults = dict(lr=lr, etas=etas, step_sizes=step_sizes)
27 super(Rprop, self).__init__(params, defaults)
29 def step(self, closure=None):
30 """Performs a single optimization step. 33 closure (callable, optional): A closure that reevaluates the model 37 if closure
is not None:
40 for group
in self.param_groups:
41 for p
in group[
'params']:
46 raise RuntimeError(
'Rprop does not support sparse gradients')
52 state[
'prev'] = torch.zeros_like(p.data)
53 state[
'step_size'] = grad.new().resize_as_(grad).fill_(group[
'lr'])
55 etaminus, etaplus = group[
'etas']
56 step_size_min, step_size_max = group[
'step_sizes']
57 step_size = state[
'step_size']
61 sign = grad.mul(state[
'prev']).sign()
62 sign[sign.gt(0)] = etaplus
63 sign[sign.lt(0)] = etaminus
67 step_size.mul_(sign).clamp_(step_size_min, step_size_max)
72 grad[sign.eq(etaminus)] = 0
75 p.data.addcmul_(-1, grad.sign(), step_size)
77 state[
'prev'].copy_(grad)
def step(self, closure=None)