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

Method apply

examples/cnn_ms/train_cnn.py:149–190  ·  view source on GitHub ↗

Performs a single optimization step. Args: param_name(String): the name of the param param_value(Tensor): param values to be update in-place grad(Tensor): param gradients; the values may be updated in this function; can

(self, param_name, param_value, param_grad)

Source from the content-addressed store, hash-verified

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.
151
152 Args:
153 param_name(String): the name of the param
154 param_value(Tensor): param values to be update in-place
155 grad(Tensor): param gradients; the values may be updated
156 in this function; cannot use it anymore
157 """
158 assert param_value.shape == param_grad.shape, ("shape mismatch",
159 param_value.shape,
160 param_grad.shape)
161 self.device_check(param_value, self.step_counter, self.lr_value,
162 self.mom_value, self.dam_value, self.decay_value)
163
164 # derive dtype from input
165 assert param_value.dtype == self.dtype
166
167 # TODO add branch operator
168 # if self.decay_value != 0:
169 if self.weight_decay.init_value != 0:
170 singa.Axpy(self.decay_value.data, param_value.data, param_grad.data)
171
172 if self.momentum.init_value != 0:
173 if param_name not in self.moments:
174 flag = param_value.device.graph_enabled()
175 param_value.device.EnableGraph(False)
176 self.moments[param_name] = tensor.zeros_like(param_value)
177 param_value.device.EnableGraph(flag)
178
179 buf = self.moments[param_name]
180 buf *= self.mom_value
181 alpha = 1.0 - self.dam_value
182 singa.Axpy(alpha.data, param_grad.data, buf.data)
183
184 if self.nesterov:
185 singa.Axpy(self.mom_value.data, buf.data, param_grad.data)
186 else:
187 param_grad = buf
188
189 minus_lr = 0.0 - self.lr_value
190 singa.Axpy(minus_lr.data, param_grad.data, param_value.data)
191
192 def step(self):
193 # increment step counter, lr and moment

Callers 10

train_one_batchMethod · 0.45
train_one_batch_disMethod · 0.45
train_one_batchMethod · 0.45
train_one_batch_disMethod · 0.45
train_one_batchMethod · 0.45
train_one_batch_disMethod · 0.45
train_one_batchMethod · 0.45
train_one_batch_disMethod · 0.45
preprocessFunction · 0.45
call_with_returnsMethod · 0.45

Calls 3

graph_enabledMethod · 0.80
EnableGraphMethod · 0.80
device_checkMethod · 0.45

Tested by

no test coverage detected