MCPcopy Create free account
hub / github.com/apache/singa / __init__

Method __init__

examples/cnn_ms/train_cnn.py:96–147  ·  view source on GitHub ↗
(self,
                 lr=0.1,
                 momentum=0,
                 dampening=0,
                 weight_decay=0,
                 nesterov=False,
                 dtype=tensor.float32)

Source from the content-addressed store, hash-verified

94 """
95
96 def __init__(self,
97 lr=0.1,
98 momentum=0,
99 dampening=0,
100 weight_decay=0,
101 nesterov=False,
102 dtype=tensor.float32):
103 super(MSSGD, self).__init__(lr, dtype)
104
105 # init momentum
106 if type(momentum) == float or type(momentum) == int:
107 if momentum < 0.0:
108 raise ValueError("Invalid momentum value: {}".format(momentum))
109 self.momentum = Constant(momentum)
110 elif isinstance(momentum, DecayScheduler):
111 self.momentum = momentum
112 momentum = momentum.init_value
113 else:
114 raise TypeError("Wrong momentum type")
115 self.mom_value = self.momentum(self.step_counter).as_type(self.dtype)
116
117 # init dampening
118 if type(dampening) == float or type(dampening) == int:
119 self.dampening = Constant(dampening)
120 elif isinstance(dampening, DecayScheduler):
121 self.dampening = dampening
122 dampening = dampening.init_value
123 else:
124 raise TypeError("Wrong dampening type")
125 self.dam_value = self.dampening(self.step_counter).as_type(self.dtype)
126
127 # init weight_decay
128 if type(weight_decay) == float or type(weight_decay) == int:
129 if weight_decay < 0.0:
130 raise ValueError(
131 "Invalid weight_decay value: {}".format(weight_decay))
132 self.weight_decay = Constant(weight_decay)
133 elif isinstance(weight_decay, DecayScheduler):
134 self.weight_decay = weight_decay
135 else:
136 raise TypeError("Wrong weight_decay type")
137 self.decay_value = self.weight_decay(self.step_counter).as_type(
138 self.dtype)
139
140 # init other params
141 self.nesterov = nesterov
142 self.moments = dict()
143
144 # check value
145 if nesterov and (momentum <= 0 or dampening != 0):
146 raise ValueError(
147 "Nesterov momentum requires a momentum and zero dampening")
148
149 def apply(self, param_name, param_value, param_grad):
150 """Performs a single optimization step.

Callers

nothing calls this directly

Calls 3

ConstantClass · 0.90
typeFunction · 0.85
as_typeMethod · 0.45

Tested by

no test coverage detected