(nx)
| 546 | |
| 547 | |
| 548 | def test_LazyTensor_reduce(nx): |
| 549 | T, a, b = get_LazyTensor(nx) |
| 550 | |
| 551 | T0 = T[:] |
| 552 | s0 = nx.sum(T0) |
| 553 | |
| 554 | # total sum |
| 555 | s = ot.utils.reduce_lazytensor(T, nx.sum, nx=nx) |
| 556 | np.testing.assert_allclose(nx.to_numpy(s), 1) |
| 557 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(s0)) |
| 558 | |
| 559 | s2 = ot.utils.reduce_lazytensor(T, nx.sum) |
| 560 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(s2)) |
| 561 | |
| 562 | s2 = ot.utils.reduce_lazytensor(T, nx.sum, batch_size=500) |
| 563 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(s2)) |
| 564 | |
| 565 | s2 = ot.utils.reduce_lazytensor(T, nx.sum, batch_size=11) |
| 566 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(s2)) |
| 567 | |
| 568 | # sum over axis 0 |
| 569 | s = ot.utils.reduce_lazytensor(T, nx.sum, axis=0, nx=nx) |
| 570 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(b)) |
| 571 | |
| 572 | # sum over axis 1 |
| 573 | s = ot.utils.reduce_lazytensor(T, nx.sum, axis=1, nx=nx) |
| 574 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(a)) |
| 575 | |
| 576 | # test otehr reduction function |
| 577 | s = ot.utils.reduce_lazytensor(T, nx.logsumexp, axis=1, nx=nx) |
| 578 | s2 = nx.logsumexp(T[:], axis=1) |
| 579 | np.testing.assert_allclose(nx.to_numpy(s), nx.to_numpy(s2)) |
| 580 | |
| 581 | # test 3D tensors |
| 582 | def getitem(i, j, k, a, b, c): |
| 583 | return a[i, None, None] * b[None, j, None] * c[None, None, k] |
| 584 | |
| 585 | # create a lazy tensor |
| 586 | n = a.shape[0] |
| 587 | T = ot.utils.LazyTensor((n, n, n), getitem, a=a, b=a, c=a) |
| 588 | |
| 589 | # total sum |
| 590 | s1 = ot.utils.reduce_lazytensor(T, nx.sum, axis=0, nx=nx) |
| 591 | s2 = ot.utils.reduce_lazytensor(T, nx.sum, axis=1, nx=nx) |
| 592 | |
| 593 | np.testing.assert_allclose(nx.to_numpy(s1), nx.to_numpy(s2)) |
| 594 | |
| 595 | with pytest.raises(NotImplementedError): |
| 596 | ot.utils.reduce_lazytensor(T, nx.sum, axis=2, nx=nx, batch_size=10) |
| 597 | |
| 598 | |
| 599 | def test_lowrank_LazyTensor(nx): |
nothing calls this directly
no test coverage detected