(T, C, N)
| 163 | |
| 164 | def 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: |
no test coverage detected