|
21 | 21 | isclose, |
22 | 22 | kron, |
23 | 23 | nunique, |
| 24 | + one_hot, |
24 | 25 | pad, |
25 | 26 | setdiff1d, |
26 | 27 | sinc, |
|
44 | 45 | lazy_xp_function(expand_dims) |
45 | 46 | lazy_xp_function(kron) |
46 | 47 | lazy_xp_function(nunique) |
| 48 | +lazy_xp_function(one_hot) |
47 | 49 | lazy_xp_function(pad) |
48 | 50 | # FIXME calls in1d which calls xp.unique_values without size |
49 | 51 | lazy_xp_function(setdiff1d, jax_jit=False) |
@@ -448,6 +450,68 @@ def test_xp(self, xp: ModuleType): |
448 | 450 | ) |
449 | 451 |
|
450 | 452 |
|
| 453 | +@pytest.mark.skip_xp_backend( |
| 454 | + Backend.SPARSE, reason="read-only backend without .at support" |
| 455 | +) |
| 456 | +class TestOneHot: |
| 457 | + @pytest.mark.parametrize("n_dim", range(4)) |
| 458 | + @pytest.mark.parametrize("num_classes", range(1, 5, 2)) |
| 459 | + def test_dims_and_classes(self, xp: ModuleType, n_dim: int, num_classes: int): |
| 460 | + shape = tuple(range(2, 2 + n_dim)) |
| 461 | + rng = np.random.default_rng(2347823) |
| 462 | + np_x = rng.integers(num_classes, size=shape) |
| 463 | + x = xp.asarray(np_x) |
| 464 | + y = one_hot(x, num_classes) |
| 465 | + assert y.shape == (*x.shape, num_classes) |
| 466 | + for *i_list, j in ndindex(*shape, num_classes): |
| 467 | + i = tuple(i_list) |
| 468 | + assert y[*i, j] == (x[i] == j) |
| 469 | + |
| 470 | + def test_one_hot(self, xp: ModuleType): |
| 471 | + actual = one_hot(xp.asarray([0, 1, 2]), 3) |
| 472 | + expected = xp.asarray([[1., 0., 0.], |
| 473 | + [0., 1., 0.], |
| 474 | + [0., 0., 1.]]) |
| 475 | + xp_assert_equal(actual, expected) |
| 476 | + |
| 477 | + actual = one_hot(xp.asarray([1, 2, 0]), 3) |
| 478 | + expected = xp.asarray([[0., 1., 0.], |
| 479 | + [0., 0., 1.], |
| 480 | + [1., 0., 0.]]) |
| 481 | + xp_assert_equal(actual, expected) |
| 482 | + |
| 483 | + def test_one_hot_out_of_bound(self, xp: ModuleType): |
| 484 | + # Undefined behavior. Either return zero, or raise. |
| 485 | + try: |
| 486 | + actual = one_hot(xp.asarray([-1, 3]), 3) |
| 487 | + except (IndexError, RuntimeError): |
| 488 | + return |
| 489 | + expected = xp.asarray([[0., 0., 0.], |
| 490 | + [0., 0., 0.]]) |
| 491 | + xp_assert_equal(actual, expected) |
| 492 | + |
| 493 | + def test_one_hot_custom_dtype(self, xp: ModuleType): |
| 494 | + actual = one_hot(xp.asarray([0, 1, 2]), 3, dtype=xp.bool) |
| 495 | + expected = xp.asarray([[True, False, False], |
| 496 | + [False, True, False], |
| 497 | + [False, False, True]]) |
| 498 | + xp_assert_equal(actual, expected) |
| 499 | + |
| 500 | + def test_one_hot_axis(self, xp: ModuleType): |
| 501 | + expected = xp.asarray([[0., 1., 0.], |
| 502 | + [0., 0., 1.], |
| 503 | + [1., 0., 0.]]).T |
| 504 | + actual = one_hot(xp.asarray([1, 2, 0]), 3, axis=0) |
| 505 | + xp_assert_equal(actual, expected) |
| 506 | + |
| 507 | + actual = one_hot(xp.asarray([1, 2, 0]), 3, axis=-2) |
| 508 | + xp_assert_equal(actual, expected) |
| 509 | + |
| 510 | + def test_one_hot_non_integer(self, xp: ModuleType): |
| 511 | + with pytest.raises((TypeError, RuntimeError, IndexError, DeprecationWarning)): |
| 512 | + one_hot(xp.asarray([1.0]), 3) |
| 513 | + |
| 514 | + |
451 | 515 | @pytest.mark.skip_xp_backend( |
452 | 516 | Backend.SPARSE, reason="read-only backend without .at support" |
453 | 517 | ) |
|
0 commit comments