|
8 | 8 |
|
9 | 9 | import numpy as np
|
10 | 10 | import pytest
|
11 |
| -from numpy.testing import assert_allclose, assert_almost_equal, assert_array_equal |
| 11 | +from numpy.testing import assert_almost_equal, assert_array_equal |
12 | 12 |
|
13 | 13 | import theano
|
14 | 14 | import theano.scalar as ts
|
|
113 | 113 | argmax,
|
114 | 114 | argmin,
|
115 | 115 | as_tensor_variable,
|
116 |
| - batched_dot, |
117 | 116 | cast,
|
118 | 117 | choose,
|
119 | 118 | clip,
|
@@ -743,67 +742,6 @@ def test_py_c_match():
|
743 | 742 | bad_runtime=dict(bad1=(rand(5, 7), rand(5, 7)), bad2=(rand(5, 7), rand(8, 3))),
|
744 | 743 | )
|
745 | 744 |
|
746 |
| -TestBatchedDot = makeTester( |
747 |
| - name="BatchedDotTester", |
748 |
| - op=batched_dot, |
749 |
| - expected=( |
750 |
| - lambda xs, ys: np.asarray( |
751 |
| - list( |
752 |
| - x * y if x.ndim == 0 or y.ndim == 0 else np.dot(x, y) |
753 |
| - for x, y in zip(xs, ys) |
754 |
| - ), |
755 |
| - dtype=ts.upcast(xs.dtype, ys.dtype), |
756 |
| - ) |
757 |
| - ), |
758 |
| - checks={}, |
759 |
| - grad=dict( |
760 |
| - correct1=(rand(3, 5, 7), rand(3, 7, 5)), |
761 |
| - correct2=(rand(3, 5, 7), rand(3, 7, 9)), |
762 |
| - correct3=(rand(3, 5, 7), rand(3, 7)), |
763 |
| - correct4=(rand(3, 5), rand(3, 5, 7)), |
764 |
| - correct5=(rand(3), rand(3, 5, 7)), |
765 |
| - correct6=(rand(3, 5), rand(3)), |
766 |
| - correct7=(rand(3, 5), rand(3, 5)), |
767 |
| - correct8=(rand(3), rand(3)), |
768 |
| - correct9=(rand(3, 5, 7, 11), rand(3)), |
769 |
| - correct10=(rand(3, 2, 6, 5), rand(3, 5)), |
770 |
| - correct11=(rand(3, 2, 6, 5), rand(3, 5, 7)), |
771 |
| - correct12=(rand(3, 2, 6, 5), rand(3, 7, 5, 8)), |
772 |
| - mixed1=(rand(3, 5).astype("float32"), rand(3, 5, 7)), |
773 |
| - mixed2=(rand(3, 5).astype("float64"), rand(3, 5, 7)), |
774 |
| - ), |
775 |
| - good=dict( |
776 |
| - correct1=(rand(3, 5, 7), rand(3, 7, 5)), |
777 |
| - correct2=(rand(3, 5, 7), rand(3, 7, 9)), |
778 |
| - correct3=(rand(3, 5, 7), rand(3, 7)), |
779 |
| - correct4=(rand(3, 5), rand(3, 5, 7)), |
780 |
| - correct5=(rand(3), rand(3, 5, 7)), |
781 |
| - correct6=(rand(3, 5), rand(3)), |
782 |
| - correct7=(rand(3, 5), rand(3, 5)), |
783 |
| - correct8=(rand(3), rand(3)), |
784 |
| - correct9=(rand(3, 5, 7, 11), rand(3)), |
785 |
| - correct10=(rand(3, 7, 11, 5), rand(3, 5)), |
786 |
| - correct11=(rand(3, 7, 11, 5), rand(3, 5, 13)), |
787 |
| - correct12=(rand(3, 7, 11, 5), rand(3, 13, 5, 17)), |
788 |
| - mixed1=(rand(3, 5).astype("float32"), rand(3, 5, 7)), |
789 |
| - mixed2=(rand(3, 5).astype("float64"), rand(3, 5, 7)), |
790 |
| - ), |
791 |
| - bad_build=dict( |
792 |
| - no_batch_axis2=(rand(), rand(3, 5)), no_batch_axis3=(rand(3, 5), rand()) |
793 |
| - ), |
794 |
| - bad_runtime=dict( |
795 |
| - batch_dim_mismatch1=(rand(2, 5, 7), rand(3, 7, 9)), |
796 |
| - batch_dim_mismatch2=(rand(3, 5, 7), rand(2, 7, 9)), |
797 |
| - batch_dim_mismatch3=(rand(3), rand(5)), |
798 |
| - bad_dim1=(rand(3, 5, 7), rand(3, 5, 7)), |
799 |
| - bad_dim2=(rand(3, 5, 7), rand(3, 8, 3)), |
800 |
| - bad_dim3=(rand(3, 5), rand(3, 7)), |
801 |
| - bad_dim4=(rand(3, 5, 7, 11), rand(3, 5)), |
802 |
| - bad_dim5=(rand(3, 5, 7, 11), rand(3, 5, 13)), |
803 |
| - bad_dim6=(rand(3, 5, 7, 11), rand(3, 13, 5, 17)), |
804 |
| - ), |
805 |
| -) |
806 |
| - |
807 | 745 |
|
808 | 746 | def _numpy_second(x, y):
|
809 | 747 | return np.broadcast_arrays(x, y)[1]
|
@@ -1595,84 +1533,6 @@ def test_clip_repeat_verify_grad(self):
|
1595 | 1533 | # gradient numerically
|
1596 | 1534 |
|
1597 | 1535 |
|
1598 |
| -def test_batched_dot(): |
1599 |
| - first = tensor3("first") |
1600 |
| - second = tensor3("second") |
1601 |
| - output = batched_dot(first, second) |
1602 |
| - first_val = np.random.rand(10, 10, 20).astype(config.floatX) |
1603 |
| - second_val = np.random.rand(10, 20, 5).astype(config.floatX) |
1604 |
| - result_fn = theano.function([first, second], output) |
1605 |
| - result = result_fn(first_val, second_val) |
1606 |
| - assert result.shape[0] == first_val.shape[0] |
1607 |
| - assert result.shape[1] == first_val.shape[1] |
1608 |
| - assert result.shape[2] == second_val.shape[2] |
1609 |
| - |
1610 |
| - first_mat = dmatrix("first") |
1611 |
| - second_mat = dmatrix("second") |
1612 |
| - output = batched_dot(first_mat, second_mat) |
1613 |
| - first_mat_val = np.random.rand(10, 10).astype(config.floatX) |
1614 |
| - second_mat_val = np.random.rand(10, 10).astype(config.floatX) |
1615 |
| - result_fn = theano.function([first_mat, second_mat], output) |
1616 |
| - result = result_fn(first_mat_val, second_mat_val) |
1617 |
| - |
1618 |
| - assert result.shape[0] == first_mat_val.shape[0] |
1619 |
| - |
1620 |
| - |
1621 |
| -def test_batched_dot_not_contiguous(): |
1622 |
| - def np_genarray(*_shape): |
1623 |
| - size = 1 |
1624 |
| - for dimsize in _shape: |
1625 |
| - size *= dimsize |
1626 |
| - return np.arange(size, dtype=config.floatX).reshape(_shape) |
1627 |
| - |
1628 |
| - X = tensor3() |
1629 |
| - W = tensor3() |
1630 |
| - Z = batched_dot(X, W) |
1631 |
| - f = function([X, W], Z) |
1632 |
| - |
1633 |
| - w = np_genarray(30, 10, 5) |
1634 |
| - reversed_x_container = np_genarray(20, 40, 30) |
1635 |
| - x_container = reversed_x_container.T |
1636 |
| - |
1637 |
| - def check_first_dim(inverted): |
1638 |
| - direction = -1 if inverted else 1 |
1639 |
| - x = x_container[::direction, ::2, ::2] |
1640 |
| - assert x.shape == (30, 20, 10) |
1641 |
| - assert x.strides[0] == direction * np.dtype(config.floatX).itemsize |
1642 |
| - assert not (x.flags["C_CONTIGUOUS"] or x.flags["F_CONTIGUOUS"]) |
1643 |
| - result = f(x, w) |
1644 |
| - ref_result = np.asarray(list(np.dot(u, v) for u, v in zip(x, w))) |
1645 |
| - utt.assert_allclose(ref_result, result) |
1646 |
| - |
1647 |
| - for inverted in (0, 1): |
1648 |
| - check_first_dim(inverted) |
1649 |
| - |
1650 |
| - |
1651 |
| -def test_batched_tensordot(): |
1652 |
| - first = tensor4("first") |
1653 |
| - second = tensor4("second") |
1654 |
| - axes = [[1, 2], [3, 1]] |
1655 |
| - output = tt.batched_tensordot(first, second, axes) |
1656 |
| - first_val = np.random.rand(8, 10, 20, 3).astype(config.floatX) |
1657 |
| - second_val = np.random.rand(8, 20, 5, 10).astype(config.floatX) |
1658 |
| - result_fn = theano.function([first, second], output) |
1659 |
| - result = result_fn(first_val, second_val) |
1660 |
| - assert result.shape[0] == first_val.shape[0] |
1661 |
| - assert result.shape[1] == first_val.shape[3] |
1662 |
| - assert result.shape[2] == second_val.shape[2] |
1663 |
| - |
1664 |
| - first_mat = dmatrix("first") |
1665 |
| - second_mat = dmatrix("second") |
1666 |
| - axes = 1 |
1667 |
| - output = tt.batched_tensordot(first_mat, second_mat, axes) |
1668 |
| - first_mat_val = np.random.rand(10, 4).astype(config.floatX) |
1669 |
| - second_mat_val = np.random.rand(10, 4).astype(config.floatX) |
1670 |
| - result_fn = theano.function([first_mat, second_mat], output) |
1671 |
| - result = result_fn(first_mat_val, second_mat_val) |
1672 |
| - assert result.shape[0] == first_mat_val.shape[0] |
1673 |
| - assert len(result.shape) == 1 |
1674 |
| - |
1675 |
| - |
1676 | 1536 | def test_tensor_values_eq_approx():
|
1677 | 1537 | # test, inf, -inf and nan equal themself
|
1678 | 1538 | a = np.asarray([-np.inf, -1, 0, 1, np.inf, np.nan])
|
@@ -2369,7 +2229,7 @@ def test_outer(self):
|
2369 | 2229 | v1 = np.asarray(np.random.rand(*s1)).astype(config.floatX)
|
2370 | 2230 | v2 = np.asarray(np.random.rand(*s2)).astype(config.floatX)
|
2371 | 2231 | o = tt.outer(x, y).eval({x: v1, y: v2})
|
2372 |
| - assert_allclose(o, np.outer(v1, v2)) |
| 2232 | + utt.assert_allclose(o, np.outer(v1, v2)) |
2373 | 2233 |
|
2374 | 2234 | def test_grad(self):
|
2375 | 2235 | # Test the combined graph of the graph of outer
|
@@ -6719,10 +6579,10 @@ def test_dot(self):
|
6719 | 6579 | x, y = self.vals
|
6720 | 6580 | # Use allclose comparison as a user reported on the mailing
|
6721 | 6581 | # list failure otherwise with array that print exactly the same.
|
6722 |
| - assert_allclose(x.dot(y), X.dot(Y).eval({X: x, Y: y})) |
| 6582 | + utt.assert_allclose(x.dot(y), X.dot(Y).eval({X: x, Y: y})) |
6723 | 6583 | Z = X.dot(Y)
|
6724 | 6584 | z = x.dot(y)
|
6725 |
| - assert_allclose(x.dot(z), X.dot(Z).eval({X: x, Z: z})) |
| 6585 | + utt.assert_allclose(x.dot(z), X.dot(Z).eval({X: x, Z: z})) |
6726 | 6586 |
|
6727 | 6587 | def test_real_imag(self):
|
6728 | 6588 | X, Y = self.vars
|
@@ -6751,7 +6611,7 @@ def test_std(self):
|
6751 | 6611 | # std() is implemented as theano tree and does not pass its
|
6752 | 6612 | # args directly to numpy. This sometimes results in small
|
6753 | 6613 | # difference, so we use allclose test.
|
6754 |
| - assert_allclose(X.std().eval({X: x}), x.std()) |
| 6614 | + utt.assert_allclose(X.std().eval({X: x}), x.std()) |
6755 | 6615 |
|
6756 | 6616 | def test_repeat(self):
|
6757 | 6617 | X, _ = self.vars
|
|
0 commit comments