def test_gaussian_posterior(self): priors = [ GaussianPrior(size=1, mean=0.5, var=1.0), GaussianPrior(size=1, mean=-2.5, var=10.0) ] for prior in priors: self._test_function_posterior(prior, self.records)
def test_gaussian_second_moment(self): priors = [ GaussianPrior(size=1, mean=0.5, var=1.0), GaussianPrior(size=1, mean=-0.3, var=0.5) ] for prior in priors: self._test_function_second_moment(prior)
def build_sparse_fft_student(size, prior_var, fft_rho, fft_var, noise_var): x_shape = (size, ) fft_shape = (2, ) + x_shape student = (GaussianPrior(size=size, var=prior_var) @ SIMOVariable( id="x", n_next=2) @ (GaussianChannel(var=noise_var) @ O("y") + (DFTChannel(real=True) + GaussBernouilliPrior( size=fft_shape, var=fft_var, rho=fft_rho)) @ MILeafVariable(id="z", n_prev=2))).to_model() return student
def build_sparse_grad_student(N, rho, noise_var): x_shape = (N, ) z_shape = (1, N) student = (GaussianPrior(size=x_shape) @ SIMOVariable(id="x", n_next=2) @ ( GaussianChannel(var=noise_var) @ O("y") + (GradientChannel(shape=x_shape) + GaussBernoulliPrior( size=z_shape, rho=rho)) @ MILeafVariable(id="z", n_prev=2)) ).to_model() return student
def build_VAE_prior(self, params): """ Build a VAE prior with loaded weights """ shape = self.shape assert self.N == 784 biases, weights = self.load_VAE_prior(params) if params['id'] == '20_relu_400_sigmoid_784_bias': D, N1, N = 20, 400, 28*28 W1, W2 = weights b1, b2 = biases prior_x = (GaussianPrior(size=D) @ V(id="z_0") @ LinearChannel(W1, name="W_1") @ V(id="Wz_1") @ BiasChannel(b1) @ V(id="b_1") @ LeakyReluChannel(0) @ V(id="z_1") @ LinearChannel(W2, name="W_2") @ V(id="Wz_2") @ BiasChannel(b2) @ V(id="b_2") @ HardTanhChannel() @ V(id="z_2") @ ReshapeChannel(prev_shape=self.N, next_shape=self.shape)) else: raise NotImplementedError return prior_x
def math(self): return "coon" def sample(self): return self.x coon = CoonPrior() x_shape = coon.size blur = Blur2DChannel(shape=x_shape, sigma=[10, 10]) noise = GaussianChannel(var=0.1) teacher = MultiLayerModel([coon, blur, noise], ids=["x", "z", "y"]) # teacher.plot() # %% # Basic gaussian denoiser # We use only a gaussian prior on $x$ (the coon image was standardized to have mean=0 and std=1) # basic deconv model prior = GaussianPrior(size=x_shape) basic_deconv = MultiLayerModel([prior, blur, noise], ids=["x", "z", "y"]) scenario = TeacherStudentScenario(teacher, basic_deconv, x_ids=["x", "z"]) scenario.setup(seed=1) # scenario.student.plot() _ = scenario.run_ep(max_iter=100, damping=0) # %% plot_data(scenario.x_true, scenario.observations["y"], scenario.x_pred) compare_hcut(scenario.x_true, scenario.x_pred)
import pytest import numpy as np from numpy.testing import assert_allclose from tramp.priors import (GaussianPrior, GaussBernoulliPrior, BinaryPrior, ExponentialPrior, PositivePrior, GaussianMixturePrior, MAP_L1NormPrior, MAP_L21NormPrior) from tramp.checks.check_limits import check_prior_BO_limit, check_prior_BN_limit from tramp.checks.check_gradients import ( check_prior_grad_BO, check_prior_grad_BO_BN, check_prior_grad_RS, check_prior_grad_FG, check_prior_grad_EP_scalar, check_prior_grad_EP_diagonal, EPSILON) EP_PRIORS = [ GaussianPrior(size=100, isotropic=False), GaussBernoulliPrior(size=100, isotropic=False), BinaryPrior(size=100, isotropic=False), ExponentialPrior(size=100, isotropic=False), PositivePrior(size=100, isotropic=False), GaussianMixturePrior(size=100, isotropic=False) ] @pytest.mark.parametrize("prior", EP_PRIORS + [MAP_L1NormPrior(size=100, isotropic=False)]) def test_separable_prior_vectorization(prior): assert not prior.isotropic N = np.prod(prior.size) ax = np.linspace(1, 2, N) ax = ax.reshape(prior.size) bx = np.linspace(-2, 2, N)
) ).to_model() # teacher.plot() sample = teacher.sample() plot_histograms(sample) plot_data(sample, sample["y"]) # %% # Sparse gradient denoising # sparse grad denoiser grad_shape = (2,) + x_shape sparse_grad = ( GaussianPrior(size=x_shape) @ SIMOVariable(id="x", n_next=2) @ ( noise @ O("y") + ( GradientChannel(shape=x_shape) + GaussBernouilliPrior(size=grad_shape, var=0.7, rho=0.9) ) @ MILeafVariable(id="x'", n_prev=2) ) ).to_model() scenario = TeacherStudentScenario(teacher, sparse_grad, x_ids=["x", "x'"]) scenario.setup(seed=1) # scenario.student.plot() _ = scenario.run_ep(max_iter=100, damping=0.1)
prior = RaccoonPrior() x_shape = prior.size noise = GaussianChannel(var=0.5) grad_shape = (2, ) + x_shape teacher = (prior @ SIMOVariable("x", n_next=2) @ (GradientChannel(x_shape) @ O("x'") + noise @ O("y"))).to_model() sample = teacher.sample() plot_histograms(sample) plot_data(sample, sample["y"]) # %% # Sparse gradient denoising grad_shape = (2, ) + x_shape sparse_grad = (GaussianPrior(size=x_shape) @ SIMOVariable(id="x", n_next=2) @ (noise @ O("y") + (GradientChannel(shape=x_shape) + GaussBernoulliPrior(size=grad_shape, var=0.7, rho=0.9)) @ MILeafVariable(id="x'", n_prev=2))).to_model() scenario = TeacherStudentScenario(teacher, sparse_grad, x_ids=["x", "x'"]) scenario.setup(seed=1) scenario.student.plot() _ = scenario.run_ep(max_iter=100, damping=0.1) plot_data(scenario.x_pred, scenario.observations["y"]) compare_hcut(scenario.x_true, scenario.x_pred, scenario.observations["y"], h=20)