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

Method forward

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

Source from the content-addressed store, hash-verified

63 raise NotImplementedError
64
65 def forward(self, inp):
66 self._check_input_ndim(inp)
67 if self._track_running_stats_saved == False:
68 assert (
69 self.track_running_stats == False
70 ), "track_running_stats can not be initilized to False and changed to True later"
71
72 _weight = self.weight
73 _bias = self.bias
74
75 if self.freeze:
76 if _weight is not None:
77 _weight = _weight.detach()
78 if _bias is not None:
79 _bias = _bias.detach()
80
81 # fastpath excution for freeze
82 scale = (self.running_var + self.eps) ** (-0.5)
83 if _weight is not None:
84 scale *= _weight
85 bias = -self.running_mean * scale
86 if _bias is not None:
87 bias += _bias
88 return inp * scale + bias
89
90 if self.training and self.track_running_stats:
91 exponential_average_factor = self.momentum
92 else:
93 exponential_average_factor = 0.0 # useless
94
95 output = batch_norm(
96 inp,
97 self.running_mean if self.track_running_stats else None,
98 self.running_var if self.track_running_stats else None,
99 _weight,
100 _bias,
101 training=self.training
102 or ((self.running_mean is None) and (self.running_var is None)),
103 momentum=exponential_average_factor,
104 eps=self.eps,
105 )
106
107 return output
108
109 def _module_info_string(self) -> str:
110 s = (

Callers

nothing calls this directly

Calls 3

_check_input_ndimMethod · 0.95
batch_normFunction · 0.90
detachMethod · 0.45

Tested by

no test coverage detected