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

Function test_func

imperative/python/test/unit/functional/test_loss.py:165–201  ·  view source on GitHub ↗
(T, C, N)

Source from the content-addressed store, hash-verified

163
164def test_ctc_loss():
165 def test_func(T, C, N):
166 input = np.random.randn(T, N, C)
167 input = F.softmax(Tensor(input), axis=-1).numpy()
168 # replace nan to 0.2
169 input = np.nan_to_num(input, copy=True, nan=0.2)
170 input_lengths = np.ones(N, dtype=np.int32) * T
171 target_lengths = np.random.randint(low=1, high=T + 1, size=(N,), dtype=np.int32)
172 target = np.random.randint(
173 low=1, high=C, size=(sum(target_lengths)), dtype=np.int32
174 )
175
176 input_mge = Tensor(input)
177 input_lengths_mge = Tensor(input_lengths)
178
179 target_mge = Tensor(target)
180 target_lengths_mge = Tensor(target_lengths)
181
182 blank = np.random.randint(C)
183 for method in ["mean", "sum", "none"]:
184 np_out = ctc_nll_naive_npy(
185 input,
186 input_lengths,
187 target,
188 target_lengths,
189 blank=blank,
190 reduction=method,
191 time_major=True,
192 )
193 mge_out = F.nn.ctc_loss(
194 input_mge,
195 input_lengths_mge,
196 target_mge,
197 target_lengths_mge,
198 blank=blank,
199 reduction=method,
200 )
201 np.testing.assert_allclose(mge_out.numpy(), np_out, rtol=2e-6)
202
203 cases = [[1, 2, 1], [100, 50, 200], [100, 5, 1]]
204 for case in cases:

Callers 1

test_ctc_lossFunction · 0.70

Calls 5

sumFunction · 0.85
ctc_nll_naive_npyFunction · 0.85
onesMethod · 0.80
TensorClass · 0.50
numpyMethod · 0.45

Tested by

no test coverage detected