def test_recons_orthogona_impossible(self): lt = faiss.LinearTransform(20, 10, True) rs = np.random.RandomState(10) A = rs.randn(10 * 20).astype('float32') faiss.copy_array_to_vector(A.ravel(), lt.A) faiss.copy_array_to_vector(rs.randn(10).astype('float32'), lt.b) lt.set_is_orthonormal() assert not lt.is_orthonormal x = rs.rand(30, 20).astype('float32') xt = lt.apply_py(x) try: xtt = lt.reverse_transform(xt) except Exception: pass else: self.assertFalse('should do an exception')
def test_recons_orthonormal(self): lt = faiss.LinearTransform(20, 10, True) rs = np.random.RandomState(10) A, _ = np.linalg.qr(rs.randn(20, 20)) A = A[:10].astype('float32') faiss.copy_array_to_vector(A.ravel(), lt.A) faiss.copy_array_to_vector(rs.randn(10).astype('float32'), lt.b) lt.set_is_orthonormal() assert lt.is_orthonormal x = rs.rand(30, 20).astype('float32') xt = lt.apply_py(x) xtt = lt.reverse_transform(xt) xttt = lt.apply_py(xtt) err = ((xt - xttt)**2).sum() self.assertGreater(1e-5, err)