MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / forward

Method forward

imperative/python/megengine/module/batchnorm.py:165–214  ·  view source on GitHub ↗
(self, inp)

Source from the content-addressed store, hash-verified

163 )
164
165 def forward(self, inp):
166 self._check_input_ndim(inp)
167
168 inp_shape = inp.shape
169 _ndims = len(inp_shape)
170 if _ndims != 4:
171 new_shape = Tensor([1, 1, 1, 1], device=inp.device)
172 origin_shape = inp_shape
173 if _ndims == 2:
174 new_shape[:2] = origin_shape[:2]
175 elif _ndims == 3:
176 new_shape[:3] = origin_shape[:3]
177 else:
178 raise ValueError(
179 "expected 2D, 3D or 4D input (got {}D input)".format(len(inp_shape))
180 )
181
182 inp = inp.reshape(new_shape)
183
184 if self.training and self.track_running_stats:
185 exponential_average_factor = self.momentum
186 else:
187 exponential_average_factor = 0.0 # useless
188
189 _weight = self.weight
190 _bias = self.bias
191
192 if self.freeze:
193 if _weight is not None:
194 _weight = _weight.detach()
195 if _bias is not None:
196 _bias = _bias.detach()
197
198 output = sync_batch_norm(
199 inp,
200 self.running_mean,
201 self.running_var,
202 _weight,
203 _bias,
204 training=(self.training and not self.freeze)
205 or ((self.running_mean is None) and (self.running_var is None)),
206 momentum=exponential_average_factor,
207 eps=self.eps,
208 group=self.group,
209 )
210
211 if _ndims != 4:
212 output = output.reshape(origin_shape)
213
214 return output
215
216
217class BatchNorm1d(_BatchNorm):

Callers

nothing calls this directly

Calls 6

_check_input_ndimMethod · 0.95
sync_batch_normFunction · 0.85
TensorClass · 0.50
formatMethod · 0.45
reshapeMethod · 0.45
detachMethod · 0.45

Tested by

no test coverage detected