def test_max_(): data = np.array([4, 0, 6, -3, 8, -2]).astype('int') compare_data = np.array([1, -2, 2, -3, 0, -1]).astype('int') tensor = IntTensor(data) compare_to = IntTensor(compare_data) test_value = tensor.max_(compare_to) actual_value = IntTensor(np.array([4, 0, 6, -3, 8, -1])) assert (test_value.equal(actual_value))
def test_int_neg_(): data = np.array([-1, -2, 3, 4, 5, -6]) expected = np.array([1, 2, -3, -4, -5, 6]) a = IntTensor(data) a.neg_() np.testing.assert_almost_equal(a.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity)\
def test_int_sin(): data = np.array([15, 60, 90, 180]) expected = np.array([0.65028784, -0.30481062, 0.89399666, -0.80115264]) a = IntTensor(data) b = a.sin() np.testing.assert_almost_equal(b.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_reciprocal(): data = np.random.rand(3,2) expected = np.reciprocal(data) a = IntTensor(data) b = a.reciprocal() np.testing.assert_almost_equal(b.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change (non-inline) np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_acos(): data = np.array([-1, 0, 1, 1, 2]) expected = np.array([3.14159265, 1.57079633, 0, 0, np.nan]) a = IntTensor(data) b = a.acos() np.testing.assert_almost_equal(b.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_view(): a = IntTensor(np.array([[[9, 3, 1, 0], [6, 8, 6, 6]], [[1, 6, 8, 6], [5, 0, 2, 0]]])) a_v = a.view(-1) a_t_ground = IntTensor(np.array([9, 3, 1, 0, 6, 8, 6, 6, 1, 6, 8, 6, 5, 0, 2, 0])) assert(a_v.equal(a_t_ground)) b_v = a.view(8, 2) b_v_ground = IntTensor(np.array([[9, 3], [1, 0], [6, 8], [6, 6], [1, 6], [8, 6], [5, 0], [2, 0]])) assert(b_v.equal(b_v_ground)) a.view_(4, -1, 2) c_v_ground = IntTensor(np.array([[[9, 3], [1, 0]], [[6, 8], [6, 6]], [[1, 6], [8, 6]], [[5, 0], [2, 0]]])) assert(a.equal(c_v_ground))
def test_int_neg(): data = np.array([-1, -2, 3, 4, 5, -6]) expected = np.array([1, 2, -3, -4, -5, 6]) a = IntTensor(data) b = a.neg() np.testing.assert_almost_equal(b.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change (non-inline) np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_cos(): data = np.array([30, 60, 90, 180]) expected = np.array([.1542515, -0.952413, -0.4480736, -0.5984601]) a = IntTensor(data) b = a.cos() np.testing.assert_almost_equal(b.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change (non-inline) np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_trace(): data = np.random.rand(5,5) expected = data.trace() a = IntTensor(data) b = a.trace() np.testing.assert_almost_equal(b, expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change (non-inline) np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_lt(): data = np.array([1,2,3,4]) compare_data = np.array([2,2,5,1]) tensor = IntTensor(data) compare_to = IntTensor(compare_data) expected = np.array([1,0,1,0]) res = tensor.lt(compare_to) np.testing.assert_almost_equal(res.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) np.testing.assert_almost_equal(tensor.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_unfold(): a = IntTensor( np.array([[-1, 2, 3, 5], [0, 4, 6, 7], [10, 3, 2, -5]], dtype=np.int32)) # Test1 expected_a = IntTensor( np.array( [[[-1, 2, 3, 5], [0, 4, 6, 7]], [[0, 4, 6, 7], [10, 3, 2, -5]]], dtype=np.int32)) actual_a = a.unfold(0, 2, 1) assert (actual_a.equal(expected_a)) # Test2 expected_a = IntTensor( np.array([[[-1, 2, 3], [0, 4, 6], [10, 3, 2]], [[2, 3, 5], [4, 6, 7], [3, 2, -5]]], dtype=np.int32)) actual_a = a.unfold(1, 3, 1) assert (actual_a.equal(expected_a)) # Test3 expected_a = IntTensor( np.array([[[-1, 2], [0, 4], [10, 3]], [[3, 5], [6, 7], [2, -5]]], dtype=np.int32)) actual_a = a.unfold(1, 2, 2) assert (actual_a.equal(expected_a))
def test_int_topk(): data = np.array([[[3, 2], [12, 4]], [[6, 44], [43, 2]]]) a = IntTensor(data) result = a.topk(1) expected = np.array([[[3], [12]], [[43], [44]]]) np.testing.assert_almost_equal(result.to_numpy(), expected, decimal=decimal_accuracy, verbose=verbosity) # a doesn't change (non-inline) np.testing.assert_almost_equal(a.to_numpy(), data, decimal=decimal_accuracy, verbose=verbosity)
def test_int_shape(): a_data = np.array([1, 2, 3, 4]) a_expected = [4] a = IntTensor(a_data) b_data = np.random.rand(3, 2) b_expected = [3, 2] b = IntTensor(b_data) assert (a.shape() == a_expected) assert (b.shape() == b_expected)
def test_int_equal(): data = np.array([1, 2, 3, 4]) compare_not_eq = np.array([2, 2, 5, 1]) compare_eq = np.array([1, 2, 3, 4]) tensor = IntTensor(data) not_eq_tensor = IntTensor(compare_not_eq) eq_tensor = IntTensor(compare_eq) assert (tensor.equal(not_eq_tensor) is False) assert (tensor.equal(eq_tensor))
def test_equal(): a = IntTensor(np.array([0, 0]).astype('int')) b = IntTensor(np.array([0, 0]).astype('int')) different_shape_tensor = IntTensor(np.array([0]).astype('int')) different_value_tensor = IntTensor(np.array([1, 1]).astype('int')) assert (a.equal(b)) assert (b.equal(a)) assert (not a.equal(different_shape_tensor)) assert (not different_shape_tensor.equal(a)) assert (not a.equal(different_value_tensor)) assert (not different_value_tensor.equal(a))
def test_transpose(): a = IntTensor(np.array([[1, 2], [3, 4]])) a_t = a.T() a_t_ground = IntTensor(np.array([[1, 3], [2, 4]])) assert (a_t.equal(a_t_ground)) b = IntTensor(np.array([[1, 2, 3], [4, 5, 6]])) b_t = b.T() b_t_ground = IntTensor(np.array([[1, 4], [2, 5], [3, 6]])) assert (b_t.equal(b_t_ground)) c = IntTensor(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])) c_t = c.T(0, 1) c_t_ground = IntTensor(np.array([[[1, 2], [5, 6]], [[3, 4], [7, 8]]])) assert (c_t.equal(c_t_ground))
def test_unfold_(): # Test1 a = IntTensor( np.array([[-1, 2, 3, 5], [0, 4, 6, 7], [10, 3, 2, -5]], dtype=np.int32)) expected_a = IntTensor( np.array( [[[-1, 2, 3, 5], [0, 4, 6, 7]], [[0, 4, 6, 7], [10, 3, 2, -5]]], dtype=np.int32)) a.unfold_(0, 2, 1) assert (a.equal(expected_a)) # Test2 a = IntTensor( np.array([[-1, 2, 3, 5], [0, 4, 6, 7], [10, 3, 2, -5]], dtype=np.int32)) expected_a = IntTensor( np.array([[[-1, 2, 3], [0, 4, 6], [10, 3, 2]], [[2, 3, 5], [4, 6, 7], [3, 2, -5]]], dtype=np.int32)) a.unfold_(1, 3, 1) assert (a.equal(expected_a)) # Test3 a = IntTensor( np.array([[-1, 2, 3, 5], [0, 4, 6, 7], [10, 3, 2, -5]], dtype=np.int32)) expected_a = IntTensor( np.array([[[-1, 2], [0, 4], [10, 3]], [[3, 5], [6, 7], [2, -5]]], dtype=np.int32)) a.unfold_(1, 2, 2) assert (a.equal(expected_a))