|
22 | 22 | isclose,
|
23 | 23 | kron,
|
24 | 24 | nunique,
|
| 25 | + one_hot, |
25 | 26 | pad,
|
26 | 27 | setdiff1d,
|
27 | 28 | sinc,
|
|
45 | 46 | lazy_xp_function(expand_dims)
|
46 | 47 | lazy_xp_function(kron)
|
47 | 48 | lazy_xp_function(nunique)
|
| 49 | +lazy_xp_function(one_hot) |
48 | 50 | lazy_xp_function(pad)
|
49 | 51 | # FIXME calls in1d which calls xp.unique_values without size
|
50 | 52 | lazy_xp_function(setdiff1d, jax_jit=False)
|
@@ -449,6 +451,98 @@ def test_xp(self, xp: ModuleType):
|
449 | 451 | )
|
450 | 452 |
|
451 | 453 |
|
| 454 | +@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="backend doesn't have arange") |
| 455 | +class TestOneHot: |
| 456 | + @pytest.mark.parametrize("n_dim", range(4)) |
| 457 | + @pytest.mark.parametrize("num_classes", [1, 3, 10]) |
| 458 | + def test_dims_and_classes(self, xp: ModuleType, n_dim: int, num_classes: int): |
| 459 | + shape = tuple(range(2, 2 + n_dim)) |
| 460 | + rng = np.random.default_rng(2347823) |
| 461 | + np_x = rng.integers(num_classes, size=shape) |
| 462 | + x = xp.asarray(np_x) |
| 463 | + y = one_hot(x, num_classes) |
| 464 | + assert y.shape == (*x.shape, num_classes) |
| 465 | + for *i_list, j in ndindex(*shape, num_classes): |
| 466 | + i = tuple(i_list) |
| 467 | + assert float(y[(*i, j)]) == (int(x[i]) == j) |
| 468 | + |
| 469 | + def test_basic(self, xp: ModuleType): |
| 470 | + actual = one_hot(xp.asarray([0, 1, 2]), 3) |
| 471 | + expected = xp.asarray([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) |
| 472 | + xp_assert_equal(actual, expected) |
| 473 | + |
| 474 | + actual = one_hot(xp.asarray([1, 2, 0]), 3) |
| 475 | + expected = xp.asarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0]]) |
| 476 | + xp_assert_equal(actual, expected) |
| 477 | + |
| 478 | + def test_2d(self, xp: ModuleType): |
| 479 | + actual = one_hot(xp.asarray([[2, 1, 0], [1, 0, 2]]), 3, axis=1) |
| 480 | + expected = xp.asarray( |
| 481 | + [ |
| 482 | + [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0]], |
| 483 | + [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]], |
| 484 | + ] |
| 485 | + ) |
| 486 | + xp_assert_equal(actual, expected) |
| 487 | + |
| 488 | + @pytest.mark.skip_xp_backend( |
| 489 | + Backend.ARRAY_API_STRICTEST, reason="backend doesn't support Boolean indexing" |
| 490 | + ) |
| 491 | + def test_abstract_size(self, xp: ModuleType): |
| 492 | + x = xp.arange(5) |
| 493 | + x = x[x > 2] |
| 494 | + actual = one_hot(x, 5) |
| 495 | + expected = xp.asarray([[0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0]]) |
| 496 | + xp_assert_equal(actual, expected) |
| 497 | + |
| 498 | + @pytest.mark.skip_xp_backend( |
| 499 | + Backend.TORCH_GPU, reason="Puts Pytorch into a bad state." |
| 500 | + ) |
| 501 | + def test_out_of_bound(self, xp: ModuleType): |
| 502 | + # Undefined behavior. Either return zero, or raise. |
| 503 | + try: |
| 504 | + actual = one_hot(xp.asarray([-1, 3]), 3) |
| 505 | + except IndexError: |
| 506 | + return |
| 507 | + expected = xp.asarray([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) |
| 508 | + xp_assert_equal(actual, expected) |
| 509 | + |
| 510 | + @pytest.mark.parametrize( |
| 511 | + "int_dtype", |
| 512 | + ["int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"], |
| 513 | + ) |
| 514 | + def test_int_types(self, xp: ModuleType, int_dtype: str): |
| 515 | + dtype = getattr(xp, int_dtype) |
| 516 | + x = xp.asarray([0, 1, 2], dtype=dtype) |
| 517 | + actual = one_hot(x, 3) |
| 518 | + expected = xp.asarray([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) |
| 519 | + xp_assert_equal(actual, expected) |
| 520 | + |
| 521 | + def test_custom_dtype(self, xp: ModuleType): |
| 522 | + actual = one_hot(xp.asarray([0, 1, 2], dtype=xp.int32), 3, dtype=xp.bool) |
| 523 | + expected = xp.asarray( |
| 524 | + [[True, False, False], [False, True, False], [False, False, True]] |
| 525 | + ) |
| 526 | + xp_assert_equal(actual, expected) |
| 527 | + |
| 528 | + def test_axis(self, xp: ModuleType): |
| 529 | + expected = xp.asarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0]]).T |
| 530 | + actual = one_hot(xp.asarray([1, 2, 0]), 3, axis=0) |
| 531 | + xp_assert_equal(actual, expected) |
| 532 | + |
| 533 | + actual = one_hot(xp.asarray([1, 2, 0]), 3, axis=-2) |
| 534 | + xp_assert_equal(actual, expected) |
| 535 | + |
| 536 | + def test_non_integer(self, xp: ModuleType): |
| 537 | + with pytest.raises(TypeError): |
| 538 | + _ = one_hot(xp.asarray([1.0]), 3) |
| 539 | + |
| 540 | + def test_device(self, xp: ModuleType, device: Device): |
| 541 | + x = xp.asarray([0, 1, 2], device=device) |
| 542 | + y = one_hot(x, 3) |
| 543 | + assert get_device(y) == device |
| 544 | + |
| 545 | + |
452 | 546 | @pytest.mark.skip_xp_backend(
|
453 | 547 | Backend.SPARSE, reason="read-only backend without .at support"
|
454 | 548 | )
|
|
0 commit comments