def axis_dim_id(a): if all_close(a, AXIS3_X.normal): return 0 if all_close(a, AXIS3_Y.normal): return 1 if all_close(a, AXIS3_Z.normal): return 2 raise ValueError(f"Invalid axis for axis dim id: {a}")
def test_init(self): a1 = Axis(normal=Vector([0.0, 1.0, 0.0]), origin=Vector([1.0, 2.0, 0.0])) a2 = Axis(normal=Vector([0.0, 1.0, 0.0])) assert all_close(a1.normal, Vector([0.0, 1.0, 0.0])) assert all_close(a1.origin, Vector([1.0, 2.0, 0.0])) assert all_close(a2.normal, Vector([0.0, 1.0, 0.0])) assert all_close(a2.origin, Vector([0.0, 0.0, 0.0])) self.assertTrue(isinstance(a1, Axis)) self.assertTrue(isinstance(a2, Axis))
def test_fmap(): t = Tensor([[1, 2], [3, 4]]) def func(data): return data * 2 assert all_close(t.fmap(func), [[2, 4], [6, 8]])
def test_residual_basic(clean_config): m1 = Conv2D('conv1', 3, 3) r = Residual('res1', m1) x = tf.ones([32, 64, 64, 3], dtype=tf.float32) res1 = r(x) res2 = m1(x) assert shape(res1) == [32, 64, 64, 3] with tf.Session() as sess: sess.run(tf.global_variables_initializer()) a, b = sess.run([res1, res2]) assert all_close(a, b) is False
def test_random_axis(self, src, tar): # data = np.concatenate([RANDOM_VECTORS, AXIS_VECTORS]) # axis = [Axis(np.array(data[i, :])) # for i in range(data.shape[0])] # for src in axis: # for tar in axis: # v_tar = tar.normal # v_src = src.normal # print(type(v_src)) # print(type(v_tar)) m = axis_to_axis(src, tar) det = np.linalg.det(m.unbox()) assert abs(det) == pytest.approx(1.0) assert all_close(m @ src, tar)
def test_init(self): b1 = Box([10, 10, 10], [1.0, 2.0, 0.0], [0, 1, 0]) b2 = Box([10, 10, 10]) assert all_close(b1.shape, [10, 10, 10]) assert all_close(b1.origin, [1.0, 2.0, 0.0]) assert all_close(b1.normal, [0, 1, 0]) assert all_close(b1.shape, [10, 10, 10]) assert all_close(b2.origin, [0, 0, 0]) assert all_close(b2.normal, [0, 0, 1])
def test_rotate_on_direction(self): a = Axis(normal=Vector([0.0, 1.0, 0.0]), origin=Vector([1.0, 2.0, 0.0])) direction_x = Vector([1.0, 0.0, 0.0]) direction_y = Vector([0.0, 1.0, 0.0]) direction_z = Vector([0.0, 0.0, 1.0]) theta = math.pi / 2 a_rot_x = a.rotate_on_direction(direction_x, theta) a_rot_y = a.rotate_on_direction(direction_y, theta) a_rot_z = a.rotate_on_direction(direction_z, theta) assert all_close(a_rot_x.origin, Vector([1.0, 0.0, 2.0])) assert all_close(a_rot_x.normal, Vector([0.0, 0.0, 1.0])) assert all_close(a_rot_y.origin, Vector([0.0, 2.0, -1.0])) assert all_close(a_rot_y.normal, Vector([0.0, 1.0, 0.0])) assert all_close(a_rot_z.origin, Vector([-2.0, 1.0, 0.0])) assert all_close(a_rot_z.normal, Vector([-1.0, 0.0, 0.0]))
def test_rotate2(self): rot = rotate2(math.pi / 3) assert all_close(rot, [[0.5, -math.sqrt(3) / 2], [math.sqrt(3) / 2, 0.5]])
def test_rotate3(self, v, expect): rot = rotate3(math.pi / 2, v) assert all_close(rot @ POINT_VECTORS[0], expect)
def test_to_tensor_like(): assert all_close(to_tensor_like([1, 2]), np.array([1, 2]))
def test_to_tensor_like_Tensor(): n = np.array([[1, 2, 3], [4, 5, 6]]) assert all_close(to_tensor_like(n) == np.array([[1, 2, 3], [4, 5, 6]]))
def tests_abs_(): assert all_close(abs_(Tensor([[-1, -2], [-3, 4]])), [[1, 2], [3, 4]])
def test_rmatmul(): t1 = Tensor([[1, 2], [1, 2]]) t2 = Tensor([[1, 2], [1, 2]]) assert all_close(t1.__rmatmaul__(t2), [[3, 6], [3, 6]])
def test_Vector_Vector_ndarray(): v = Vector([[1, 1], [1, 1]]) n = np.array([[1, 1], [1, 1]]) assert all_close(project(v, n), np.array([[0.5, 0.5], [0.5, 0.5]]))
def test_add(): a, b = Tensor([1, 2]), Tensor([3, 4]) c = Tensor([4, 6]) assert all_close(a + b, c)
def test_argmax(): assert np.argmax(Tensor([[1, 2], [2, 7]])) == 3 assert all_close(np.argmax(Tensor([[1, 2], [2, 7]]), axis=0), [1, 1]) assert all_close(np.argmax(Tensor([[1, 2], [2, 7]]), axis=1), [1, 1])
def test_unbox(): assert all_close(Tensor([1, 2]).unbox(), [1, 2])
def test_init(): assert all_close(Tensor([1, 2]), [1, 2])
def test_all_close(): assert all_close(Tensor([1]), Tensor([1])) == True assert all_close(Tensor([1]), Tensor([2])) == False
def test_norm(): t = Tensor([-4, -3, -2, -1, 0, 1, 2, 3, 4]) assert all_close(norm(t), 7.745966692414834)
def test_random_vectors(self, v): rot = axis_to_z(v) assert all_close(rot @ v, AXIS3_Z.normal)
def test_unit_ndarry(): assert all_close(unit(np.array([1, 0])), np.array([1, 0], dtype=np.float))
def test_random_vectors(self, v): rot = z_to_axis(Vector(v)) assert all_close(rot @ (AXIS3_Z.normal), v)
def test_square(): assert all_close(square(Tensor([[1, 2], [3, 4]])), np.array([[1, 4], [9, 16]]))
def test_setitem(): t = Tensor([[1, 2], [3, 4]]) t[-1] = [2] assert all_close(t.data[-1], [2, 2])
def test_unit(): assert all_close(unit(Tensor([0, 1])), np.array([0, 1], dtype=np.float64))
def test_abs_ndarray(): assert all_close(abs(np.array([[-1, -2], [-3, 4]])), [[1, 2], [3, 4]])
def test_matmul(): t1 = Tensor([[1, 2], [1, 2]]) t2 = Tensor([[1, 2], [1, 2]]) assert all_close(t1 @ t2, [[3, 6], [3, 6]])
def test_sub_box_shape(): assert all_close(sub_box_shape(Box([1.0, 2.0, 3.0]), [10, 40, 30]), Vector([0.1, 0.05, 0.1]))
def test_square_ndarry(): assert all_close(square(np.array([-1j, 1])), [-1. + 0.j, 1. + 0.j])