def test_div(self): a1, n1 = self.prepare((2, 3)) a2 = A([[3, 2, 2], [4, 5, 4]], dtype=float) n2 = Nplike(a2) res = A([[0, 1.0 / 2.0, 1.0], [0.75, 0.8, 1.25]]) array_almost_equal((n1 / n2).eval(), res) array_almost_equal((n1 / a2).eval(), res)
def test_sub(self): a1, n1 = self.prepare((2, 3)) a2 = A([[3, 2, 2], [4, 5, 4]]) n2 = Nplike(a2) res = A([[-3, -1, 0], [-1, -1, 1]]) are((n1 - n2).eval(), res) are((n1 - a2).eval(), res)
def test_mul(self): a1, n1 = self.prepare((2, 3)) a2 = A([[3, 2, 2], [4, 5, 4]]) n2 = Nplike(a2) res = A([[0, 2, 4], [12, 20, 20]]) are((n1 * n2).eval(), res) are((n1 * a2).eval(), res)
def test_add(self): a1, n1 = self.prepare((2, 3)) a2 = A([[3, 2, 2], [4, 5, 4]]) n2 = Nplike(a2) res = A([[3, 3, 4], [7, 9, 9]]) are((n1 + n2).eval(), res) are((n1 + a2).eval(), res)
def test_3D(self): inp = Nplike( A([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[2, 3, 4], [5, 6, 7], [8, 9, 1]]])) resmax = a_pool(inp, inp.shape, (2, 2), mode="max") resavg = a_pool(inp, inp.shape, (2, 2), mode="avg") array_almost_equal( resmax.eval(), A([[[5.0, 6.0], [8.0, 9.0]], [[6.0, 7.0], [9.0, 9.0]]])) array_almost_equal( resavg.eval(), A([[[3.0, 4.0], [6.0, 7.0]], [[4.0, 5.0], [7.0, 5.75]]]))
def test_2D2(self): inp = Nplike(A([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])) resmax = a_pool(inp, inp.shape, (3, 3), mode="max") resavg = a_pool(inp, inp.shape, (3, 3), mode="avg") array_almost_equal(resmax.eval(), A([[[9.0]]])) array_almost_equal(resavg.eval(), A([[[5.0]]]))
def nplike(x): return Nplike(A(x))
def test_simple2(self): inp = Nplike(A([[[1, 2], [3, 4]]])) resmax = a_pool(inp, inp.shape, (2, 2), mode="max") resavg = a_pool(inp, inp.shape, (2, 2), mode="avg") array_almost_equal(resmax.eval(), A([[[4.0]]])) array_almost_equal(resavg.eval(), A([[[2.5]]]))
def test_exp(self): a = (np.arange(6) + 0.0).reshape((2, 3)) n = Nplike(a) array_almost_equal(np.exp(a), n.exp().eval())
def test_reciprocal(self): a = (np.arange(6) + 1.0).reshape((2, 3)) n = Nplike(a) array_almost_equal(n.reciprocal().eval(), 1.0 / a)
def test_rdiv(self): a = A([[3, 2, 2], [4, 5, 4]], dtype=float) n = Nplike(a) res = A([[1.0 / 3.0, 0.5, 0.5], [0.25, 0.2, 0.25]]) array_almost_equal((1.0 / n).eval(), res)
def test_setitem(self): a, n = self.prepare((2, 3)) n[0:2, 0:2] = Nplike(A([[11, 12], [13, 14]])) are(n.eval(), A([[11, 12, 2], [13, 14, 5]]))
def test_from_shape2(self): n = Nplike.from_shape((2, 3), neutral=False) array_almost_equal(n.eval(), np.ones((2, 3)))
def test_from_shape1(self): n = Nplike.from_shape((2, 3), neutral=True) array_almost_equal(n.eval(), np.zeros((2, 3)))
def prepare(self, shp): a = np.arange(np.prod(shp)).reshape(shp) n = Nplike(a) return a, n