| 734 | |
| 735 | |
| 736 | def test_random_backends(nx): |
| 737 | tmp_u = nx.rand() |
| 738 | |
| 739 | assert tmp_u < 1 |
| 740 | |
| 741 | tmp_n = nx.randn() |
| 742 | |
| 743 | nx.seed(0) |
| 744 | M1 = nx.to_numpy(nx.rand(5, 2)) |
| 745 | nx.seed(0) |
| 746 | M2 = nx.to_numpy(nx.rand(5, 2, type_as=tmp_n)) |
| 747 | |
| 748 | assert np.all(M1 >= 0) |
| 749 | assert np.all(M1 < 1) |
| 750 | assert M1.shape == (5, 2) |
| 751 | assert np.allclose(M1, M2) |
| 752 | |
| 753 | nx.seed(0) |
| 754 | M1 = nx.to_numpy(nx.randn(5, 2)) |
| 755 | nx.seed(0) |
| 756 | M2 = nx.to_numpy(nx.randn(5, 2, type_as=tmp_u)) |
| 757 | |
| 758 | nx.seed(42) |
| 759 | v1 = nx.randn() |
| 760 | v2 = nx.randn() |
| 761 | assert v1 != v2 |
| 762 | |
| 763 | nx.seed(0) |
| 764 | M1 = nx.to_numpy(nx.randperm(5)) |
| 765 | nx.seed(0) |
| 766 | M2 = nx.to_numpy(nx.randperm(5, type_as=tmp_u)) |
| 767 | M3 = nx.arange(5) |
| 768 | M4 = nx.sort(nx.randperm(5)) |
| 769 | assert np.allclose(M3, M4) |
| 770 | |
| 771 | with pytest.raises(ValueError, match="size must be"): |
| 772 | res = nx.randperm(size=[5, 12]) |
| 773 | |
| 774 | |
| 775 | def test_gradients_backends(): |