def test_elbo(): mu0 = 1.5 sigma = 1.0 y_obs = np.array([1.6, 1.4]) post_mu = np.array([1.88], dtype=theano.config.floatX) post_sd = np.array([1], dtype=theano.config.floatX) # Create a model for test with Model() as model: mu = Normal('mu', mu=mu0, sd=sigma) Normal('y', mu=mu, sd=1, observed=y_obs) # Create variational gradient tensor mean_field = MeanField(model=model) with change_flags(compute_test_value='off'): elbo = -KL(mean_field)()(10000) mean_field.shared_params['mu'].set_value(post_mu) mean_field.shared_params['rho'].set_value(np.log(np.exp(post_sd) - 1)) f = theano.function([], elbo) elbo_mc = f() # Exact value elbo_true = (-0.5 * ( 3 + 3 * post_mu ** 2 - 2 * (y_obs[0] + y_obs[1] + mu0) * post_mu + y_obs[0] ** 2 + y_obs[1] ** 2 + mu0 ** 2 + 3 * np.log(2 * np.pi)) + 0.5 * (np.log(2 * np.pi) + 1)) np.testing.assert_allclose(elbo_mc, elbo_true, rtol=0, atol=1e-1)
def test_intensity(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, udeg=2) np.random.seed(11) lat = 180 * (np.random.random(10) - 0.5) lon = 360 * (np.random.random(10) - 0.5) y = [1.0] + list(np.random.randn(8)) u = [-1.0] + list(np.random.randn(2)) f = [np.pi] theta = 0.0 alpha = 0.1 tau = 0.5 delta = 0.0 def intensity(lat, lon, y, u, f, theta, alpha, tau, delta): return map.ops.intensity(lat, lon, y, u, f, theta, alpha, tau, delta, np.array(True)) verify_grad( intensity, (lat, lon, y, u, f, theta, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_flow_forward_apply(flow_spec): z0 = pm.tt_rng().normal(size=(10, 20)) flow = flow_spec(dim=20, z0=z0) with change_flags(compute_test_value='off'): dist = flow.forward shape_dist = dist.shape.eval() assert tuple(shape_dist) == (10, 20)
def test_rv(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, rv=True) theta = np.linspace(0, 30, 10) xo = np.linspace(-1.5, 1.5, len(theta)) yo = np.ones_like(xo) * 0.3 zo = 1.0 * np.ones_like(xo) ro = 0.1 inc = 85.0 * np.pi / 180.0 obl = 30.0 * np.pi / 180.0 veq = 0.5 alpha = 0.3 tau = 0.5 delta = 0.0 y = np.ones(9) u = [-1.0] # Just rotation verify_grad( map.ops.rv, (theta, xo, yo, zo, 0.0, inc, obl, y, u, veq, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, ) # Just occultation verify_grad( map.ops.rv, ( theta, xo / 3, yo, zo, ro, inc, obl, y, u, veq, alpha, tau, delta, ), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, ) # Rotation + occultation verify_grad( map.ops.rv, (theta, xo, yo, zo, ro, inc, obl, y, u, veq, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_flow_det_shape(flow_spec): with change_flags(compute_test_value='off'): z0 = pm.tt_rng().normal(size=(10, 20)) flow = flow_spec(dim=20, z0=z0) det = flow.logdet det_dist = det.shape.eval() assert tuple(det_dist) == (10,)
def test_norm_grad(): with change_flags(compute_test_value="off"): z = 0.001 op = AlphaBetaOp(20) get_alpha = lambda z: op(z)[0] get_beta = lambda z: op(z)[1] theano.gradient.verify_grad(get_alpha, [z], n_tests=1, rng=np.random) theano.gradient.verify_grad(get_beta, [z], n_tests=1, rng=np.random)
def test_sqrt_grad(): with change_flags(compute_test_value="off"): np.random.seed(0) Q = np.random.randn(10, 10) Q = Q @ Q.T theano.gradient.verify_grad(lambda x: tt.sum(matrix_sqrt(x)), (Q, ), n_tests=1, rng=np.random)
def test_validate_input_types_gpuarray_backend(): from theano.sandbox.rng_mrg import mrg_uniform from theano.gpuarray.type import gpuarray_shared_constructor from theano.configparser import change_flags with change_flags(compute_test_value="raise"): rstate = np.zeros((7, 6), dtype="int32") rstate = gpuarray_shared_constructor(rstate) mrg_uniform.new(rstate, ndim=None, dtype="float32", size=(3, ))
def test_validate_input_types_gpuarray_backend(): from theano.sandbox.rng_mrg import mrg_uniform from theano.gpuarray.type import gpuarray_shared_constructor from theano.configparser import change_flags with change_flags(compute_test_value="raise"): rstate = numpy.zeros((7, 6), dtype="int32") rstate = gpuarray_shared_constructor(rstate) mrg_uniform.new(rstate, ndim=None, dtype="float32", size=(3,))
def test_flow_det(flow_spec): z0 = tt.arange(0, 20).astype('float32') flow = flow_spec(dim=20, z0=z0.dimshuffle('x', 0)) with change_flags(compute_test_value='off'): z1 = flow.forward.flatten() J = tt.jacobian(z1, z0) logJdet = tt.log(tt.abs_(tt.nlinalg.det(J))) det = flow.logdet[0] np.testing.assert_allclose(logJdet.eval(), det.eval(), atol=0.0001)
def test_rTA1L_grad(): with change_flags(compute_test_value="off"): op = rTA1LOp(ydeg=1, udeg=3) theano.gradient.verify_grad( lambda u1, u2, u3: op([u1, u2, u3]), [0.5, 0.25, 0.1], n_tests=1, rng=np.random, )
def setup(self, oversample=1, ntries=1): self.ntries = ntries # Don't setup unless the user actually calls this function, # since there's quite a bit of overhead if self._do_setup or (oversample != self.oversample): self.oversample = oversample # Create the grid using healpy if available # Require at least `oversample * l ** 2 points` s = np.random.RandomState(0) if hp is None: npts = oversample * self.ydeg**2 self.lat_grid = ( np.arccos(2 * s.rand(npts) - 1) * 180.0 / np.pi - 90.0) self.lon_grid = (s.rand(npts) - 0.5) * 360 else: nside = 1 while hp.nside2npix(nside) < oversample * self.ydeg**2: nside += 1 theta, phi = hp.pix2ang(nside=nside, ipix=range(hp.nside2npix(nside))) self.lat_grid = 0.5 * np.pi - theta self.lon_grid = phi - np.pi # Add a little noise for stability self.lat_grid += 1e-4 * s.randn(len(self.lat_grid)) self.lon_grid += 1e-4 * s.randn(len(self.lon_grid)) self.P_grid = self.P( tt.as_tensor_variable(self.lat_grid), tt.as_tensor_variable(self.lon_grid), ).eval() # Set up the cost & grad function for the nonlinear solver u0 = np.zeros(self.udeg + 1) u0[0] = -1.0 u0 = tt.as_tensor_variable(u0) f0 = np.zeros((self.fdeg + 1)**2) f0[0] = np.pi f0 = tt.as_tensor_variable(f0) latlon = tt.dvector() y = tt.dvector() with change_flags(compute_test_value="off"): self.I = theano.function( [latlon, y], [ self.intensity(latlon[0], latlon[1], y, u0, f0, 0.0, 0.0)[0], *theano.grad( self.intensity(latlon[0], latlon[1], y, u0, f0, 0.0, 0.0)[0], [latlon], ), ], ) self._do_setup = False
def test_sT(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2) verify_grad( map.ops.sT, (np.linspace(0.01, 1.09, 30), 0.1), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_rT_reflected(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, reflected=True) bterm = np.linspace(-1, 1, 10)[1:-1] verify_grad( map.ops.rT, (bterm, ), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_flux_reflected(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, reflected=True) theta = np.linspace(0, 30, 10) xs = np.linspace(-1.5, 1.5, len(theta)) ys = np.ones_like(xs) * 0.3 zs = -1.0 * np.ones_like(xs) ro = 0.0 inc = 85.0 * np.pi / 180.0 obl = 30.0 * np.pi / 180.0 y = np.ones(9) u = [-1.0] f = [np.pi] alpha = 0.1 tau = 0.5 delta = 0.0 Rs = 1.0 sigr = 30 * np.pi / 180 def func(theta, xs, ys, zs, Rs, ro, inc, obl, u, f, alpha, tau, delta): return tt.dot( map.ops.X( theta, xs, ys, zs, Rs, xs, ys, zs, ro, inc, obl, u, f, alpha, tau, delta, sigr, ), y, ) # Just rotation verify_grad( func, (theta, xs, ys, zs, Rs, ro, inc, obl, u, f, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_eigh_grad(): with change_flags(compute_test_value="off"): np.random.seed(0) Q = np.random.randn(10, 10) Q = Q @ Q.T eigh = EighOp() # Test the eigenvalues theano.gradient.verify_grad(lambda x: tt.sum(eigh(x)[0]), (Q, ), n_tests=1, rng=np.random) # Test the eigenvectors theano.gradient.verify_grad(lambda x: tt.sum(eigh(x)[1]), (Q, ), n_tests=1, rng=np.random)
def test_flux_ylm_ld(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, udeg=2) theta = np.linspace(0, 30, 10) xo = np.linspace(-1.5, 1.5, len(theta)) yo = np.ones_like(xo) * 0.3 zo = 1.0 * np.ones_like(xo) ro = 0.1 inc = 85.0 * np.pi / 180.0 obl = 30.0 * np.pi / 180.0 y = np.ones(9) np.random.seed(14) u = [-1.0] + list(np.random.randn(2)) f = [np.pi] alpha = 0.1 tau = 0.5 delta = 0.0 func = lambda *args: tt.dot(map.ops.X(*args), y) # Just rotation verify_grad( func, (theta, xo, yo, zo, 0.0, inc, obl, u, f, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, ) # Just occultation verify_grad( func, (theta, xo / 3, yo, zo, ro, inc, obl, u, f, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, ) # Rotation + occultation verify_grad( func, (theta, xo, yo, zo, ro, inc, obl, u, f, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def _get_func(self): """ Return a function that makes a value from an integer. The integer value is assumed to be a valid pointer for the type and no check is done to ensure that. """ from theano.scalar import get_scalar_type if self._fn is None: with change_flags(compute_test_value='off'): v = get_scalar_type('int64')() self._fn = theano.function([v], _make_cdata(self)(v), profile=False) return self._fn
def test_pT(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2) map[1:, :] = 1 x = np.array([0.13]) y = np.array([0.25]) z = np.sqrt(1 - x**2 - y**2) verify_grad( map.ops.pT, (x, y, z), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_tensordotRz_grad(ydeg=2, abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): op = tensordotRzOp(ydeg) theta = (np.array([0.0, 15.0, 30.0, 45.0, 60.0, 75.0, 90.0]) * np.pi / 180.0) M = np.ones((len(theta), (ydeg + 1)**2)) theano.gradient.verify_grad( op, (M, theta), n_tests=1, abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, rng=np.random, )
def test_overflow_cpu(): # run with THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 rng = MRG_RandomStreams(np.random.randint(1234)) fct = rng.uniform with change_flags(compute_test_value='off'): # should raise error as the size overflows sizes = [(2**31, ), (2**32, ), (2**15, 2**16,), (2, 2**15, 2**15)] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=True) # should not raise error sizes = [(2**5, ), (2**5, 2**5), (2**5, 2**5, 2**5)] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=False) # should support int32 sizes sizes = [(np.int32(2**10), ), (np.int32(2), np.int32(2**10), np.int32(2**10))] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=False)
def test_config_context(): # TODO: use custom config instance for the test root = configparser.config configparser.AddConfigVar( "test_config_context", "A config var from a test case.", configparser.StrParam("test_default"), root=root, ) assert hasattr(root, "test_config_context") assert root.test_config_context == "test_default" with configparser.change_flags(test_config_context="new_value"): assert root.test_config_context == "new_value" assert root.test_config_context == "test_default"
def test_diffrot(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): np.random.seed(0) with change_flags(compute_test_value="off"): map = starry.Map(ydeg=3, drorder=1) y = np.random.randn(4, map.Ny) wta = [0.1, 0.5, 1.0, 2.0] # radians verify_grad( map.ops.tensordotD, (y, wta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_spot_spectral(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=5, nw=2) amp = [-0.01, -0.02] sigma = 0.1 lat = 30 * np.pi / 180 lon = 45 * np.pi / 180 verify_grad( map.ops.spotYlm, (amp, sigma, lat, lon), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_F(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, udeg=2, rv=True) np.random.seed(11) u = np.random.randn(3) u[0] = -1 f = np.random.randn(16) verify_grad( map.ops.F, (u, f), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_Rx_grad(ydeg=5, theta=np.pi / 3, abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): op = RxOp(ydeg) theano.gradient.verify_grad( lambda theta: op(theta)[0], (theta, ), n_tests=1, abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, rng=np.random, )
def test_sT_reflected(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(ydeg=2, reflected=True) b = np.array([0.5]) theta = np.array([0.5]) bo = np.array([0.75]) ro = 0.5 sigr = 30 * np.pi / 180 verify_grad( map.ops.sT, (b, theta, bo, ro, sigr), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def _get_func(self): """ Return a function that makes a value from an integer. The integer value is assumed to be a valid pointer for the type and no check is done to ensure that. """ from theano.scalar import get_scalar_type if self._fn is None: with change_flags(compute_test_value='off'): v = get_scalar_type('int64')() self._fn = theano.function([v], _make_cdata(self)(v), mode=theano.Mode(optimizer=None), profile=False) return self._fn
def test_diffrot(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): np.random.seed(0) with change_flags(compute_test_value="off"): map = starry.Map(ydeg=5) y = np.random.randn(4, map.Ny) alpha = 1.0 tau = 0.5 delta = 0.0 theta = [0.1, 0.5, 1.0, 2.0] # radians verify_grad( map.ops.tensordotD, (y, theta, alpha, tau, delta), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_overflow_cpu(): # run with THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 rng = MRG_RandomStreams(np.random.randint(1234)) fct = rng.uniform with change_flags(compute_test_value='off'): # should raise error as the size overflows sizes = [(2**31, ), (2**32, ), ( 2**15, 2**16, ), (2, 2**15, 2**15)] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=True) # should not raise error sizes = [(2**5, ), (2**5, 2**5), (2**5, 2**5, 2**5)] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=False) # should support int32 sizes sizes = [(np.int32(2**10), ), (np.int32(2), np.int32(2**10), np.int32(2**10))] rng_mrg_overflow(sizes, fct, config.mode, should_raise_error=False)
def test_flux_quad_ld(abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): with change_flags(compute_test_value="off"): map = starry.Map(udeg=2) xo = np.linspace(-1.5, 1.5, 10) yo = np.ones_like(xo) * 0.3 zo = 1.0 * np.ones_like(xo) ro = 0.1 np.random.seed(14) u = np.array([-1.0] + list(np.random.randn(2))) func = lambda *args: map.ops.flux(*args) verify_grad( func, (xo, yo, zo, ro, u), abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, n_tests=1, )
def test_config_hash(): # TODO: use custom config instance for the test root = configparser.config configparser.AddConfigVar( "test_config_hash", "A config var from a test case.", configparser.StrParam("test_default"), root=root, ) h0 = configparser.get_config_hash() with configparser.change_flags(test_config_hash="new_value"): assert root.test_config_hash == "new_value" h1 = configparser.get_config_hash() h2 = configparser.get_config_hash() assert h1 != h0 assert h2 == h0
def test_get_test_values_success(): """Tests that `get_test_values` returns values when available (and the debugger is on).""" for mode in ["ignore", "warn", "raise"]: with change_flags(compute_test_value=mode): x = tt.vector() x.tag.test_value = np.zeros((4, ), dtype=config.floatX) y = np.zeros((5, 5)) iters = 0 for x_val, y_val in op.get_test_values(x, y): assert x_val.shape == (4, ) assert y_val.shape == (5, 5) iters += 1 assert iters == 1
def test_special_tensordotRz_grad(ydeg=2, abs_tol=1e-5, rel_tol=1e-5, eps=1e-7): np.random.seed(0) with change_flags(compute_test_value="off"): op = special_tensordotRzOp(ydeg) theta = (np.array([0.0, 15.0, 30.0, 45.0, 60.0, 75.0, 90.0]) * np.pi / 180.0) M = np.random.random(((ydeg + 1)**2, (ydeg + 1)**2)) T = np.random.random(((ydeg + 1)**2, (ydeg + 1)**2)) theano.gradient.verify_grad( lambda M, theta: op(T, M, theta), (M, theta), n_tests=1, abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, rng=np.random, )
def test_sqrt_grad_low_rank(): # NOTE: For ydeg > 2 the *numerical* gradient gets # very unstable, so I'm not sure how to test this! ydeg = 2 # Let's compute the sqrt of the latitude integral Q alpha = 55.0 beta = 7.5 def Q(alpha, beta): return LatitudeIntegralOp(ydeg)(alpha, beta)[3] def U(alpha, beta): return matrix_sqrt(Q(alpha, beta), neig=2 * ydeg + 1) with change_flags(compute_test_value="off"): theano.gradient.verify_grad(U, (alpha, beta), n_tests=1, eps=1e-4, rng=np.random)
def test_latitude_grad( ydeg=3, a=defaults["a"], b=defaults["b"], abs_tol=1e-5, rel_tol=1e-5, eps=1e-7, ): with change_flags(compute_test_value="off"): op = LatitudeIntegralOp(ydeg) # Get Beta params a1 = -5 a2 = 5 b1 = -5 b2 = 5 alpha = np.exp(a * (a2 - a1) + a1) beta = np.exp(b * (b2 - b1) + b1) # d/dq theano.gradient.verify_grad( lambda alpha, beta: op(alpha, beta)[0], (alpha, beta), n_tests=1, abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, rng=np.random, ) # d/dQ theano.gradient.verify_grad( lambda alpha, beta: op(alpha, beta)[3], (alpha, beta), n_tests=1, abs_tol=abs_tol, rel_tol=rel_tol, eps=eps, rng=np.random, )
size = comm.Get_size() if size != 2: stderr.write("mpiexec failed to create a world with two nodes.\n" "Closing with success message.") stdout.write("True") exit(0) shape = (2, 2) dtype = 'float32' scheduler = sort_schedule_fn(*mpi_cmps) mode = theano.Mode(optimizer=None, linker=theano.OpWiseCLinker(schedule=scheduler)) with change_flags(compute_test_value='off'): if rank == 0: x = theano.tensor.matrix('x', dtype=dtype) y = x + 1 send_request = send(y, 1, 11) z = recv(shape, dtype, 1, 12) f = theano.function([x], [send_request, z], mode=mode) xx = np.random.rand(*shape).astype(dtype) expected = (xx + 1) * 2 _, zz = f(xx) same = np.linalg.norm(zz - expected) < .001
def test_validate_input_types_gpuarray_backend(): with change_flags(compute_test_value="raise"): rstate = np.zeros((7, 6), dtype="int32") rstate = gpuarray_shared_constructor(rstate) rng_mrg.mrg_uniform.new(rstate, ndim=None, dtype="float32", size=(3,))