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))
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_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_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))