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

Function test_normalize

imperative/python/test/unit/functional/test_math.py:192–223  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

190
191
192def test_normalize():
193 x = Tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
194 y = F.normalize(x, axis=-1)
195 np.testing.assert_equal(
196 y.numpy().round(decimals=1),
197 np.array([[0.3, 0.5, 0.8], [0.5, 0.6, 0.7]]).astype(np.float32),
198 )
199
200 cases = [
201 {"input": np.random.random((2, 3, 12, 12)).astype(np.float32)} for i in range(2)
202 ]
203
204 def np_normalize(x, p=2, axis=None, eps=1e-12):
205 if axis is None:
206 norm = np.sum(x ** p) ** (1.0 / p)
207 else:
208 norm = np.sum(x ** p, axis=axis, keepdims=True) ** (1.0 / p)
209 return x / np.clip(norm, a_min=eps, a_max=np.inf)
210
211 # # Test L-2 norm along all dimensions
212 # opr_test(cases, F.normalize, ref_fn=np_normalize)
213
214 # # Test L-1 norm along all dimensions
215 # opr_test(cases, partial(F.normalize, p=1), ref_fn=partial(np_normalize, p=1))
216
217 # Test L-2 norm along the second dimension
218 opr_test(cases, partial(F.normalize, axis=1), ref_fn=partial(np_normalize, axis=1))
219
220 # Test some norm == 0
221 cases[0]["input"][0, 0, 0, :] = 0
222 cases[1]["input"][0, 0, 0, :] = 0
223 opr_test(cases, partial(F.normalize, axis=3), ref_fn=partial(np_normalize, axis=3))
224
225
226def test_sum_neg_axis():

Callers

nothing calls this directly

Calls 9

TensorClass · 0.90
opr_testFunction · 0.90
normalizeMethod · 0.80
assert_equalMethod · 0.80
arrayMethod · 0.80
reshapeMethod · 0.45
roundMethod · 0.45
numpyMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected