def test_make_circle_loss(): d = 2 n = 1 N = 2 b = 1 tot = N * b * d * n * 2 # q[0,0]^2 = 0, q[0,1]^2 = 2 # p[0,0]^2 = 1, p[0,1]^2 = 3 # q[1,0]^2 = 4, q[1.1]^2 = 6 # p[1,0]^2 = 5, p[1,1]^2 = 7 # loss = (q[0,0]^2+p[0,0]^2 - (q[1,0]^2+p[1,0]^2))^2 # + (q[0,1]^2+p[0,1]^2 - (q[1,1]^2+p[1,1]^2))^2 # + (q[1,0]^2+p[1,0]^2 - (q[0,0]^2+p[0,0]^2))^2 # + (q[1,1]^2+p[1,1]^2 - (q[0,1]^2+p[0,1]^2))^2 # =2*(q[0,0]^2+p[0,0]^2 - (q[1,0]^2+p[1,0]^2))^2 # +2*(q[0,1]^2+p[0,1]^2 - (q[1,1]^2+p[1,1]^2))^2 z = tf.sqrt(tf.reshape(tf.range(0, tot, dtype=DTYPE), (N, b, d, n, 2))) # = 2*(0 +1 - (4 +5 ))^2 # + 2*(2 +3 - (6 +7 ))^2 expected_loss = tf.constant(2. * ((1 - (4 + 5))**2 + (5 - (6 + 7))**2), dtype=DTYPE) / 4. loss = make_circle_loss(z) assert_allclose(loss, expected_loss) print("test_make_circle_loss passed")
def test_distances_batch(seed): man = Euclidean(10) x = torch.rand(20, 10) x_cpu = x.cpu() dists_ref = np.diag(cdist(x_cpu, x_cpu)) dists = man.dist(x, x) assert_allclose(dists_ref, dists, atol=1e-4)
def test_sphere_dist_poles(): man = Sphere(5) x = torch.zeros(5) x[0] = 1.0 y = torch.zeros(5) y[0] = -1.0 assert_allclose(man.dist(x, y), math.pi)
def testdiag_unitary(): batch_size = 1 d = 2 num_p = 2 n = num_p * d # 4 # q = [0,2,4,6], p =[1,3,5,7] x = tf.reshape(tf.range(0, batch_size * n * 2, dtype=DTYPE), (batch_size, d, num_p, 2)) q, p = extract_q_p(x) q = tf.reshape(q, [batch_size, 1, 1, -1]) p = tf.reshape(p, [batch_size, 1, 1, -1]) phi = tf.constant([0, np.pi / 2, np.pi, 3 * np.pi / 2], dtype=DTYPE) newq, newp = diag_unitary(q, p, phi) # cos(phi) * q - sin(phi) * p #=[1,0,-1,0]*[0,2,4,6] - [0,1,0,-1]*[1,3,5,7] #=[0,0,-4,0] - [0,3,0,-7] # sin(phi) * q + cos(phi) * p #=[0,1,0,-1]*[0,2,4,6] + [1,0,-1,0]*[1,3,5,7] #=[0,2,0,-6] + [1,0,-5,0] expected_q = tf.reshape(tf.constant([0, -3, -4, +7], dtype=DTYPE), [1, 1, 1, n]) expected_p = tf.reshape(tf.constant([1, 2, -5, -6], dtype=DTYPE), [1, 1, 1, n]) assert_allclose(newq, expected_q) assert_allclose(newp, expected_p) print('testdiag_unitary passed')
def test_exp_log(seed, rand_spd, rand_sym, d): spd = SPD(d) x = rand_spd(10, d) u = rand_sym(10, d) y = spd.exp(x, u) assert_allclose(u, spd.log(x, y), atol=1e-4) assert_allclose(spd.norm(x, u), spd.dist(x, y), atol=1e-4)
def test_stein_pdiv(seed, d): spd = SPD(2) xs = spd.rand(10, ir=1.0, out=torch.empty(10, d, d, dtype=torch.float64)) pdivs = spd.stein_pdiv(xs) m = torch.triu_indices(10, 10, 1) ref_pdivs = spd.stein_div(xs[m[0]], xs[m[1]]) assert_allclose(ref_pdivs, pdivs, atol=1e-4)
def test_closed_toda_3(): # Choose points such that q1+q2+q3 = p1+p2+p3 = 0 q = tf.reshape([.1, .2, -.3], [1, 1, 3, 1]) p = tf.reshape([.3, .5, -.8], [1, 1, 3, 1]) h1 = closed_toda(join_q_p(q, p)) # Fourier modes: # a1 = x + i y # = 1/sqrt(3)(1/w .1 + w .2 - .3) = # = 1/sqrt(3)((-1/2 - i sqrt(3)/2) .1 + (-1/2 + i sqrt(3)/2) .2 - .3) x = 1 / tf.sqrt(3.) * (-1 / 2. * (.1 + .2) - .3) # y = 1/tf.sqrt(3.)( tf.sqrt(3.)/2. * (.1 - .2) ) y = 1 / 2. * (-.1 + .2) # b1 = px + i py = 1/sqrt(3)(1/w p1 + w p2 + p3) # = 1/sqrt(3)( (-1/2 - i sqrt(3)/2) .3 + (1/2 + i sqrt(3)/2) .5 - .8) # = 1/sqrt(3)( -1/2 * (.3 + .5) - .8) - i 1/2 (.3 - .5) px = 1 / tf.sqrt(3.) * (-1 / 2. * (.3 + .5) - .8) py = -1 / 2. * (.3 - .5) kin = px**2 + py**2 expected_kin = tf.reduce_sum(tf.square(p)) * .5 assert_allclose(kin, expected_kin) q1 = q[0, 0, 0, 0] q2 = q[0, 0, 1, 0] q3 = q[0, 0, 2, 0] expected_q12 = -2 * y expected_q23 = y - tf.sqrt(3.) * x expected_q31 = y + tf.sqrt(3.) * x assert_allclose(q1 - q2, expected_q12) assert_allclose(q2 - q3, expected_q23) assert_allclose(q3 - q1, expected_q31) h2 = kin + tf.exp(expected_q12) + tf.exp(expected_q23) + tf.exp( expected_q31) assert_allclose(h1, h2) print('test_closed_toda_3 passed')
def test_eig_gradients(seed, rand_sym, n, d, eig): x1 = rand_sym(n, d).requires_grad_() s1 = eig(x1).pow(2).sum() s1.backward() x2 = x1.detach().clone().requires_grad_() s2 = torch.symeig(x2, eigenvectors=True).eigenvalues.pow(2).sum() s2.backward() assert_allclose(tb.sym(x1.grad), tb.sym(x2.grad), atol=1e-4)
def test_cholesky2x2_grad(seed, rand_spd, n): x1 = rand_spd(n, 2).requires_grad_() s1 = fast.cholesky2x2(x1).pow(2).sum() s1.backward() x2 = x1.detach().clone().requires_grad_() s2 = x2.cholesky().pow(2).sum() s2.backward() assert_allclose(tb.sym(x1.grad), x2.grad, atol=1e-4)
def test_highpass(self): iir = gwpy_signal.highpass(100, 1024) utils.assert_zpk_equal(iir, HIGHPASS_IIR_100HZ) fir = gwpy_signal.highpass(100, 1024, type='fir') print(fir) print(HIGHPASS_FIR_100HZ) print(fir - HIGHPASS_FIR_100HZ) utils.assert_allclose(fir, HIGHPASS_FIR_100HZ)
def test_ortho_dist_same_as_logm(seed, n): torch.set_default_dtype(torch.float64) ortho = SpecialOrthogonalGroup(n) xs = ortho.rand_uniform(100) ys = ortho.rand_uniform(100) assert_allclose(logm_dist(xs, ys), ortho.dist(xs, ys), atol=1e-4)
def test_gradient_gras(seed, n, p): gras = Grassmann(n, p) x, y = gras.rand_uniform(2, out=torch.empty(2, n, p, dtype=torch.float64)) x.requires_grad_() dist = 0.5 * gras.dist(x, y, squared=True) grad_e = torch.autograd.grad(dist, x)[0] grad = gras.egrad2rgrad(x, grad_e) assert_allclose(grad.detach(), -gras.log(x.detach(), y), atol=1e-4)
def test_cython_map(rand_graph, n, p): g = rand_graph(nx.erdos_renyi_graph, n, p) n = g.number_of_nodes() pdists = np.random.rand(n * (n - 1) // 2).astype(np.float32) ref_map = py_mean_average_precision(squareform(pdists), g) fp = FastPrecision(g) assert_allclose(fp.mean_average_precision(pdists), ref_map, atol=1e-6)
def test_unit_distance(d, seed): spd = SPD(d) u_vec = torch.randn(spd.dim) u = SPD.from_vec(u_vec / u_vec.norm()) x = torch.eye(d) assert_allclose(1.0, spd.norm(x, u), atol=1e-4) y = spd.exp(x, u) assert_allclose(1.0, spd.dist(x, y), atol=1e-4)
def test_gradient(seed, d): spd = SPD(d) x, y = spd.rand(2, ir=1.0, out=torch.empty(2, d, d, dtype=torch.float64)) x.requires_grad_() dist = 0.5 * spd.dist(x, y, squared=True) grad_e = torch.autograd.grad(dist, x)[0] grad = spd.egrad2rgrad(x, grad_e) assert_allclose(grad.detach(), -spd.log(x.detach(), y), atol=1e-4)
def test_closed_toda(): x = tf.reshape(tf.range(1, 7, dtype=DTYPE), [1, 1, 3, 2]) # q = [1,3,5], p=[2,4,6] h = closed_toda(x) expected_h = tf.constant(1 / 2 * (4 + 16 + 36) + np.exp(1. - 3.) + np.exp(3. - 5.) + np.exp(5. - 1.), dtype=DTYPE) assert_allclose(h, expected_h) print('test_closed_toda passed')
def test_hyp_sph_mapping(seed, n): hyp = Lorentz(3) sph = Sphere(3) x = hyp.rand(n, ir=1e-2, out=torch.empty(n, 3, dtype=torch.float64)) hyp_dists = hyp.dist(hyp.zero(n, out=x.new()), x) y = hyperboloid_to_sphere(x) sph_dists = sph.dist(sph.zero(n, out=x.new()), y) assert_allclose(sph_dists, hyp_dists, atol=1e-4)
def test_h2_to_sspd2(seed, n, d): spd = SPD(2) lorentz = Lorentz(3) x = lorentz.rand(n, ir=1.0).mul_(d) y = h2_to_sspd2(x) assert_allclose(sspd2_to_h2(y), x / d, atol=1e-4) hyp_dists = sspd2_hyp_radius_ * lorentz.pdist(x / d) assert_allclose(spd.pdist(y), hyp_dists, atol=1e-4)
def test_f1_trivial(rand_graph, n, p): g = rand_graph(nx.erdos_renyi_graph, n, p) n = g.number_of_nodes() pdists = compute_graph_pdists(g) fp = FastPrecision(g) means, _ = fp.layer_mean_f1_scores(pdists) assert_allclose(means, np.ones(len(means)), atol=1e-6) assert_allclose(area_under_curve(means), 1, atol=1e-6)
def test_free(): # q=1,3,5; p=2;4;6 # q=7,9,11; p=8;10;12 x = tf.reshape(tf.range(1, 13, dtype=DTYPE), shape=(2, 3, 1, 2)) h = free(x) expected_h = tf.reshape( [1 / 2 * (2**2 + 4**2 + 6**2), 1 / 2 * (8**2 + 10**2 + 12**2)], shape=(2, )) assert_allclose(h, expected_h) print('test_free passed')
def test_f1_reversed_order(rand_graph, n, p): g = rand_graph(nx.erdos_renyi_graph, n, p) n = g.number_of_nodes() pdists = compute_graph_pdists(g) fp = FastPrecision(g) # TODO(ccruceru): Decide if this behaviour makes more sense. means, _ = fp.layer_mean_f1_scores(1 / pdists) assert_allclose(means, np.zeros(len(means)), atol=1e-6) assert_allclose(area_under_curve(means), 0, atol=1e-6)
def test_kepler(): # q=1,3,5; p=2;4;6 # q=7,9,11; p=8;10;12 x = tf.reshape(tf.range(1, 13, dtype=DTYPE), shape=(2, 3, 1, 2)) h = kepler(x) expected_h = tf.reshape([ 1 / 2 * (2**2 + 4**2 + 6**2) + 1. / tf.sqrt(1. + 3**2 + 5**2), 1 / 2 * (8**2 + 10**2 + 12**2) + 1. / tf.sqrt(7.**2 + 9**2 + 11**2) ], shape=(2, )) assert_allclose(h, expected_h) print('test_kepler passed')
def test_distance_formulas(seed, rand_spd, d): spd = SPD(d) x, y = rand_spd(2, d) ref_dist = spd.dist(x, y) # compute :math:`Y^{-1} X` and take its eigenvalues (we have to use # `torch.eig` for this as the resulting matrix might not be symmetric) d1 = torch.solve(y, x)[0].eig()[0][:, 0].log_().pow_(2).sum().sqrt_() assert_allclose(ref_dist, d1, atol=1e-4) d2 = torch.solve(x, y)[0].eig()[0][:, 0].log_().pow_(2).sum().sqrt_() assert_allclose(ref_dist, d2, atol=1e-4)
def testLinearSymplectic(): # 2 samples, 5 particles in 2d. q = even numbers, p = odd numbers x = tf.reshape(tf.range(0, 16, dtype=DTYPE), shape=(2, 2, 2, 2)) model = LinearSymplectic() y = model(x) assert_equal(tf.shape(x), tf.shape(y)) x_inv = model.inverse(y) assert_allclose(x, x_inv, rtol=1e-6, atol=1e-5) # x = tf.reshape(x[0, :, :, :], [1, 2, 2, 2]) assert (is_symplectic(model, x, atol=1e-6)) print('testLinearSymplectic passed')
def testPermute(): z = tf.reshape(tf.range(0, 6, dtype=DTYPE), shape=(2, 3, 1, 1)) # z[0,:] = [0,1,2], z[1,:] = [3,4,5] dim_to_split = 1 na = 1 nb = 3 - na model = Permute(dim_to_split, [na, nb]) # Test call x = model(z) expected_x = tf.constant([1., 2., 0., 4., 5., 3.], shape=(2, 3, 1, 1)) assert_allclose(x, expected_x) assert_allclose(model.log_jacobian_det(z), tf.zeros((2, ), dtype=DTYPE)) print("testPermute passed")
def testOscillatorFlow(): # phi[0] = 0, I[0] = .5; phi[1] = 1; I[1] = 1.5 z = tf.reshape(tf.range(0, 4, dtype=DTYPE), [2, 1, 1, 2]) / 2. expected_x = tf.reshape([ tf.sqrt(2 * .5) * tf.sin(0.), tf.sqrt(2 * .5) * tf.cos(0.), tf.sqrt(2 * 1.5) * tf.sin(1.), tf.sqrt(2 * 1.5) * tf.cos(1.) ], [2, 1, 1, 2]) m = OscillatorFlow() assert_allclose(m(z), expected_x) assert_allclose(m.inverse(m(z)), z) # z = tf.reshape(tf.range(0, 36, dtype=DTYPE), [3, 2, 3, 2]) / 36. m = OscillatorFlow() assert_allclose(m.inverse(m(z)), z) # z = tf.reshape(tf.range(0, 36, dtype=DTYPE), [3, 2, 3, 2]) / 36. m = OscillatorFlow(first_only=True) assert_allclose(m.inverse(m(z)), z) # m = OscillatorFlow() assert (is_symplectic( m, tf.reshape(tf.range(0, 20, dtype=DTYPE), [1, 2, 5, 2]))) print("testOscillatorFlow passed")
def testLinearSymplecticTwoByTwo(): batch_size = 2 d = 3 n = 2 x = tf.reshape(tf.range(0, batch_size * d * n * 2, dtype=DTYPE), (batch_size, d, n, 2)) # Test call # TODO: rand_init=True does not work model = LinearSymplecticTwoByTwo() y = model(x) # Test inverse z = model.inverse(y) assert_allclose(x, z) # Test symplectic x = tf.random_normal((1, d, n, 2), dtype=DTYPE) # assert(is_symplectic(model, x)) print('testLinearSymplecticTwoByTwo passed')
def test_calogero_moser(): x = tf.reshape(tf.range(1, 7, dtype=DTYPE), [1, 1, 3, 2]) # q = [1,3,5], p=[2,4,6] h = calogero_moser(x, 'rational') expected_h = tf.constant(1 / 2 * (4 + 16 + 36 + 1 + 9 + 25) + 1. / (1 - 3)**2 + 1. / (1 - 5)**2 + 1. / (3 - 5)**2, dtype=DTYPE) assert_allclose(h, expected_h) x = tf.reshape([-.1, 1., 3.2, -.2, 1.34, 3.], [1, 1, 3, 2]) # q = [-.1,3.2,1.34], p=[1.,-.2,3.] h = calogero_moser(x, 'rational', omegasq=.3) expected_h = tf.constant( 1/2 * (1.**2 + .2**2 + 3.**2 + .3 * (.1**2 + 3.2**2 + 1.34**2)) + \ 1./(-.1 - 3.2)**2 + 1./(-.1 - 1.34)**2 + 1./(3.2 - 1.34)**2, dtype=DTYPE) assert_allclose(h, expected_h) print('test_calogero_moser passed')
def testChain(): batch_size = 3 x = tf.ones([batch_size, 1, 2]) bijectors = [SymplecticExchange() for i in range(3)] # Test call model = Chain(bijectors) y = model(x) # q,p -> p,-q -> -q,-p -> -p,q expected_y = tf.concat( [-tf.ones([batch_size, 1, 1]), tf.ones([batch_size, 1, 1])], 2) assert_equal(y, expected_y) # Test inverse inverted_y = model.inverse(y) assert_equal(x, inverted_y) # Test inverse stop_at inverted_y = model.inverse(y, stop_at=1) expected_x = tf.concat( [tf.ones([batch_size, 1, 1]), -tf.ones([batch_size, 1, 1])], 2) assert_equal(expected_x, inverted_y) # Test log jacobian determinant z = tf.ones([3, 2, 7, 1]) * 1.2345 # arbitrary n_models = 15 model = Chain([TimesTwoBijector() for i in range(n_models)]) expected_log_jac_det = n_models * tf.log(2.) * 2 * 7 * 1 * tf.ones( (3, ), dtype=tf.float32) assert_allclose(model.log_jacobian_det(z), expected_log_jac_det) # Test set_is_training n_bij = 3 class FakeBijectorWithIsTraining(): def __init__(self): self.is_training = True bijectors = [FakeBijectorWithIsTraining() for i in range(n_bij)] model = Chain(bijectors) model.set_is_training(False) for b in model.bijectors: assert_equal(b.is_training, False) model.set_is_training(True) for b in model.bijectors: assert_equal(b.is_training, True) print('testChain passed')
def test_parameterized_neumann(): # q=1,3; p=2;4 x = tf.reshape(tf.range(1, 5, dtype=DTYPE), shape=(1, 2, 1, 2)) ks = tf.constant([.1, .2], dtype=DTYPE) h = parameterized_neumann(ks)(x) expected_h = tf.constant(0.5*((1*4-3*2)**2) + \ 0.5*(.1*1**2 + .2*3**2)) assert_allclose(h, expected_h) # q=1,3,5; p=2;4;6 # q=7,9,11; p=8;10;12 x = tf.reshape(tf.range(1, 13, dtype=DTYPE), shape=(2, 3, 1, 2)) ks = tf.constant([.1, .2, .3], dtype=DTYPE) h = parameterized_neumann(ks)(x) expected_h = tf.constant([0.5*((1*4-3*2)**2 + (1*6-5*2)**2 + (3*6-5*4)**2) + \ 0.5*(.1*1**2 + .2*3**2 + .3*5**2), 0.5*((7*10-9*8)**2 + (7*12-11*8)**2 + (9*12-10*11)**2) + \ 0.5*(.1*7**2 + .2*9**2 + .3*11**2)]) assert_allclose(h, expected_h) print('test_parameterized_neumann passed')
def test_inject(self): # create a timeseries out of an array of zeros df, nyquist = 1, 2048 data = FrequencySeries(numpy.zeros(df*nyquist + 1), f0=0, df=df, unit='') # create a second timeseries to inject into the first w_nyquist = 1024 sig = FrequencySeries(numpy.ones(df*w_nyquist + 1), f0=0, df=df, unit='') # test that we recover this waveform when we add it to data, # and that the operation does not change the original data new_data = data.inject(sig) assert new_data.unit == data.unit assert new_data.size == data.size ind, = new_data.value.nonzero() assert len(ind) == sig.size utils.assert_allclose(new_data.value[ind], sig.value) utils.assert_allclose(data.value, numpy.zeros(df*nyquist + 1))
def test_ifft(self): # construct a TimeSeries, then check that it is unchanged by # the operation TimeSeries.fft().ifft() ts = TimeSeries([1.0, 0.0, -1.0, 0.0], sample_rate=1.0) assert ts.fft().ifft() == ts utils.assert_allclose(ts.fft().ifft().value, ts.value)
def test_highpass(self): iir = filter_design.highpass(100, 1024) utils.assert_zpk_equal(iir, HIGHPASS_IIR_100HZ) fir = filter_design.highpass(100, 1024, type='fir') utils.assert_allclose(fir, HIGHPASS_FIR_100HZ)
def test_bandpass(self): iir = filter_design.bandpass(100, 200, 1024) utils.assert_zpk_equal(iir, BANDPASS_IIR_100HZ_200HZ) fir = filter_design.bandpass(100, 200, 1024, type='fir') utils.assert_allclose(fir, BANDPASS_FIR_100HZ_200HZ)