예제 #1
0
    def __init__(
        self,
        shape: Union[int, List[int]],
        weight_posterior: Type[BaseDistribution] = Deterministic,
        bias_posterior: Type[BaseDistribution] = Deterministic,
        weight_prior: BaseDistribution = Normal(0, 1),
        bias_prior: BaseDistribution = Normal(0, 1),
        weight_initializer: Dict[str, Callable] = {"loc": xavier},
        bias_initializer: Dict[str, Callable] = {"loc": xavier},
        name="BatchNormalization",
    ):

        # Create the parameters
        self.weight = Parameter(
            shape=shape,
            posterior=weight_posterior,
            prior=weight_prior,
            initializer=weight_initializer,
            name=name + "_weight",
        )
        self.bias = Parameter(
            shape=shape,
            posterior=bias_posterior,
            prior=bias_prior,
            initializer=bias_initializer,
            name=name + "_bias",
        )
예제 #2
0
 def __init__(self):
     self.a_list = [
         Parameter(name="TestParam4"),
         Parameter(name="TestParam5"),
     ]
     self.a_dict = {
         "a": Parameter(name="TestParam6"),
         "b": Parameter(name="TestParam7"),
     }
예제 #3
0
 def __init__(self, d: int, heteroscedastic: bool = False):
     self.heteroscedastic = heteroscedastic
     if heteroscedastic:
         self.weights = Parameter([d, 2], name='weights')
         self.bias = Parameter([1, 1], name='bias')
     else:
         self.weights = Parameter([d, 1], name='weights')
         self.bias = Parameter([1, 1], name='bias')
         self.std = ScaleParameter([1, 1], name='std')
예제 #4
0
 def __init__(self, d: int, d_o: int = 1, heteroscedastic: bool = False):
     self.heteroscedastic = heteroscedastic
     if heteroscedastic:
         self.d_o = d_o
         self.weights = Parameter([d, d_o * 2], name="weights")
         self.bias = Parameter([1, d_o * 2], name="bias")
     else:
         self.weights = Parameter([d, d_o], name="weights")
         self.bias = Parameter([1, d_o], name="bias")
         self.std = ScaleParameter([1, d_o], name="std")
예제 #5
0
 def __init__(self):
     self.a_list = [
         Parameter(name="TestParam5"),
         Parameter(name="TestParam6"),
         TestModule1(),
     ]
     self.a_dict = {
         "b": Parameter(name="TestParam7"),
         "a": Parameter(name="TestParam8"),
         "c": TestModule2(),
     }
예제 #6
0
    def __init__(self, d_in: int, d_out: int = 1, name: str = "Dense"):

        # Check types
        if d_in < 1:
            raise ValueError("d_in must be >0")
        if d_out < 1:
            raise ValueError("d_out must be >0")

        # Create the parameters
        self.d_in = d_in
        self.d_out = d_out
        self.weights = Parameter(shape=[d_in, d_out], name=name + "_weights")
        self.bias = Parameter(shape=[1, d_out], name=name + "_bias")
예제 #7
0
    def __init__(self, d_in: int, d_out: int = 1, name: str = 'Dense'):

        # Check types
        if d_in < 1:
            raise ValueError('d_in must be >0')
        if d_out < 1:
            raise ValueError('d_out must be >0')

        # Create the parameters
        self.d_in = d_in
        self.d_out = d_out
        self.weights = Parameter(shape=[d_in, d_out], name=name+'_weights')
        self.bias = Parameter(shape=[1, d_out], name=name+'_bias')
예제 #8
0
    def __init__(
        self,
        k: Union[int, List[int]],
        d: Union[int, List[int]],
        posterior: Type[BaseDistribution] = Deterministic,
        prior: BaseDistribution = Normal(0, 1),
        initializer: Dict[str, Callable] = {"loc": xavier},
        name: str = "Embedding",
    ):

        # Convert to list if not already
        if isinstance(k, int):
            k = [k]
        if isinstance(d, int):
            d = [d]

        # Check values
        if len(k) != len(d):
            raise ValueError("d and k must be the same length")
        if any(e < 1 for e in k):
            raise ValueError("k must be >0")
        if any(e < 1 for e in d):
            raise ValueError("d must be >0")

        # Create the parameters
        self.embeddings = [
            Parameter(
                shape=[k[i], d[i]],
                posterior=posterior,
                prior=prior,
                initializer=initializer,
                name=name + "_" + str(i),
            ) for i in range(len(d))
        ]
예제 #9
0
def test_Parameter_1D():
    """Tests a 1D Parameter"""

    # Create 1D parameter
    param = Parameter(shape=5)

    # kl_loss should still be scalar
    kl_loss = param.kl_loss()
    assert isinstance(kl_loss, tf.Tensor)
    assert kl_loss.ndim == 0

    # posterior_mean should return mean
    sample1 = param.posterior_mean()
    sample2 = param.posterior_mean()
    assert sample1.ndim == 1
    assert sample2.ndim == 1
    assert sample1.shape[0] == 5
    assert sample2.shape[0] == 5
    assert np.all(sample1 == sample2)

    # posterior_sample should return samples
    sample1 = param.posterior_sample()
    sample2 = param.posterior_sample()
    assert sample1.ndim == 1
    assert sample2.ndim == 1
    assert sample1.shape[0] == 5
    assert sample2.shape[0] == 5
    assert np.all(sample1 != sample2)

    # posterior_sample should be able to return multiple samples
    sample1 = param.posterior_sample(10)
    sample2 = param.posterior_sample(10)
    assert sample1.ndim == 2
    assert sample2.ndim == 2
    assert sample1.shape[0] == 10
    assert sample1.shape[1] == 5
    assert sample2.shape[0] == 10
    assert sample2.shape[1] == 5
    assert np.all(sample1 != sample2)

    # prior_sample should still be 1D
    prior_sample = param.prior_sample()
    assert prior_sample.ndim == 0
    prior_sample = param.prior_sample(n=7)
    assert prior_sample.ndim == 1
    assert prior_sample.shape[0] == 7

    # n_parameters property
    nparams = param.n_parameters
    assert isinstance(nparams, int)
    assert nparams == 5
예제 #10
0
def test_Parameter_no_prior():
    """Tests a parameter with no prior"""

    # Create parameter with no prior
    param = Parameter(prior=None)

    # kl_loss should return 0
    kl_loss = param.kl_loss()
    assert isinstance(kl_loss, tf.Tensor)
    assert kl_loss.ndim == 0
    assert kl_loss.numpy() == 0.0

    # prior_sample should return nans with prior=None
    prior_sample = param.prior_sample()
    assert prior_sample.ndim == 1
    assert prior_sample.shape[0] == 1
    assert np.all(np.isnan(prior_sample))
    prior_sample = param.prior_sample(n=7)
    assert prior_sample.ndim == 1
    assert prior_sample.shape[0] == 7
    assert np.all(np.isnan(prior_sample))
예제 #11
0
def test_Parameter_float_initializer():
    """Tests a 2D Parameter with a float initializer"""

    # Create 1D parameter
    param = Parameter(shape=[5, 4],
                      name="lala2",
                      initializer={
                          "loc": 1.0,
                          "scale": 2.0
                      })

    # all should have been initialized to 1
    vals = param()
    assert np.all(vals == 1.0)
예제 #12
0
def test_Deterministic():
    """Tests Deterministic distribution"""

    # Create the distribution
    dist = Deterministic()

    # Check default params
    assert dist.loc == 0

    # Call should return backend obj
    assert isinstance(dist(), tfd.Deterministic)

    # Test methods
    assert dist.prob(0).numpy() == 1.0
    assert dist.prob(1).numpy() == 0.0
    assert dist.log_prob(0).numpy() == 0.0
    assert dist.log_prob(1).numpy() == -np.inf
    assert dist.mean().numpy() == 0.0
    assert dist.mode().numpy() == 0.0
    assert dist.cdf(-1).numpy() == 0.0
    assert dist.cdf(1).numpy() == 1.0

    # Test sampling
    samples = dist.sample()
    assert isinstance(samples, tf.Tensor)
    assert samples.ndim == 0
    samples = dist.sample(10)
    assert isinstance(samples, tf.Tensor)
    assert samples.ndim == 1
    assert samples.shape[0] == 10
    samples = dist.sample(tf.constant([10]))
    assert isinstance(samples, tf.Tensor)
    assert samples.ndim == 1
    assert samples.shape[0] == 10

    # Should be able to set params
    dist = Deterministic(loc=3)
    assert dist.loc == 3

    # But only with Tensor-like objs
    with pytest.raises(TypeError):
        dist = Deterministic(loc="lalala")

    # Test using a parameter as an argument
    p = Parameter()
    dist = Deterministic(loc=p)
    dist.sample()
예제 #13
0
    def __init__(self,
                 k: int,
                 d: int,
                 posterior: Type[BaseDistribution] = Deterministic,
                 prior: BaseDistribution = Normal(0, 1),
                 initializer: Dict[str, Callable] = {'loc': xavier},
                 name: str = 'Embeddings'):

        # Check types
        if k < 1:
            raise ValueError('k must be >0')
        if d < 1:
            raise ValueError('d must be >0')

        # Create the parameters
        self.embeddings = Parameter(shape=[k, d],
                                    posterior=posterior,
                                    prior=prior,
                                    initializer=initializer,
                                    name=name)
예제 #14
0
def test_Parameter_slicing():
    """Tests a slicing Parameters"""

    # Create 1D parameter
    param = Parameter(shape=[2, 3, 4, 5])

    # Should be able to slice!
    sl = param[0].numpy()
    assert sl.ndim == 4
    assert sl.shape[0] == 1
    assert sl.shape[1] == 3
    assert sl.shape[2] == 4
    assert sl.shape[3] == 5

    sl = param[:, :, :, :2].numpy()
    assert sl.ndim == 4
    assert sl.shape[0] == 2
    assert sl.shape[1] == 3
    assert sl.shape[2] == 4
    assert sl.shape[3] == 2

    sl = param[1, ..., :2].numpy()
    assert sl.ndim == 4
    assert sl.shape[0] == 1
    assert sl.shape[1] == 3
    assert sl.shape[2] == 4
    assert sl.shape[3] == 2

    sl = param[...].numpy()
    assert sl.ndim == 4
    assert sl.shape[0] == 2
    assert sl.shape[1] == 3
    assert sl.shape[2] == 4
    assert sl.shape[3] == 5

    sl = param[tf.constant([0]), :, ::2, :].numpy()
    assert sl.ndim == 4
    assert sl.shape[0] == 1
    assert sl.shape[1] == 3
    assert sl.shape[2] == 2
    assert sl.shape[3] == 5
예제 #15
0
 def __init__(self, shape):
     self.mod = TestModule()
     self.p3 = Parameter(name="TestParam3", shape=shape)
예제 #16
0
def test_Parameter_posterior_ci():
    """Tests probflow.parameters.Parameter.posterior_ci"""

    # With a scalar parameter
    param = Parameter()
    lb, ub = param.posterior_ci()
    assert isinstance(lb, np.ndarray)
    assert isinstance(ub, np.ndarray)
    assert lb.ndim == 1
    assert ub.ndim == 1
    assert lb.shape[0] == 1
    assert ub.shape[0] == 1

    # Should error w/ invalid ci or n vals
    with pytest.raises(ValueError):
        lb, ub = param.posterior_ci(ci=-0.1)
    with pytest.raises(ValueError):
        lb, ub = param.posterior_ci(ci=1.1)
    with pytest.raises(ValueError):
        lb, ub = param.posterior_ci(n=0)

    # With a 1D parameter
    param = Parameter(shape=5)
    lb, ub = param.posterior_ci()
    assert isinstance(lb, np.ndarray)
    assert isinstance(ub, np.ndarray)
    assert lb.ndim == 1
    assert ub.ndim == 1
    assert lb.shape[0] == 5
    assert ub.shape[0] == 5

    # With a 2D parameter
    param = Parameter(shape=[5, 4])
    lb, ub = param.posterior_ci()
    assert isinstance(lb, np.ndarray)
    assert isinstance(ub, np.ndarray)
    assert lb.ndim == 2
    assert ub.ndim == 2
    assert lb.shape[0] == 5
    assert lb.shape[1] == 4
    assert ub.shape[0] == 5
    assert ub.shape[1] == 4
예제 #17
0
 def __init__(self):
     self.weight = Parameter(name="Weight")
     self.bias = Parameter(name="Bias")
     self.std = ScaleParameter(name="Std")
예제 #18
0
 def __init__(self, d: int):
     self.weights = Parameter([d, 1], name='weights')
     self.bias = Parameter(name='bias')
     self.std = ScaleParameter(name='std')
예제 #19
0
 def __init__(self):
     self.weight = Parameter([5, 1], name="Weight")
     self.bias = Parameter([1, 1], name="Bias")
예제 #20
0
 def __init__(self, d_in, d_out):
     self.weight = Parameter([d_in, d_out], name="Weight")
     self.bias = Parameter([1, d_out], name="Bias")
     self.std = ScaleParameter([1, d_out], name="Std")
예제 #21
0
 def __init__(self):
     self.mean = Parameter([1], name="Mean")
     self.std = ScaleParameter([1], name="Std")
예제 #22
0
 def __init__(self, cols):
     self.cols = cols
     self.weight = Parameter([len(cols), 1], name="Weight")
     self.bias = Parameter([1, 1], name="Bias")
     self.std = ScaleParameter([1, 1], name="Std")
예제 #23
0
 def __init__(self):
     self.weight = Parameter([5, 1], name="Weight")
     self.bias = Parameter([1, 1], name="Bias")
     self.act = torch.nn.Softplus()
예제 #24
0
 def __init__(self, d: int):
     self.weights = Parameter([d, 1], name='weights')
     self.bias = Parameter([1, 1], name='bias')
예제 #25
0
 def __init__(self, d: int, k: int = 2):
     self.weights = Parameter([d, k - 1], name='weights')
     self.bias = Parameter([1, k - 1], name='bias')
예제 #26
0
def test_Parameter_2D():
    """Tests a 2D Parameter"""

    # Create 1D parameter
    param = Parameter(shape=[5, 4], name="lala")

    # repr
    pstr = param.__repr__()
    assert pstr == "<pf.Parameter lala shape=[5, 4]>"

    # kl_loss should still be scalar
    kl_loss = param.kl_loss()
    assert isinstance(kl_loss, tf.Tensor)
    assert kl_loss.ndim == 0

    # posterior_mean should return mean
    sample1 = param.posterior_mean()
    sample2 = param.posterior_mean()
    assert sample1.ndim == 2
    assert sample2.ndim == 2
    assert sample1.shape[0] == 5
    assert sample1.shape[1] == 4
    assert sample2.shape[0] == 5
    assert sample2.shape[1] == 4
    assert np.all(sample1 == sample2)

    # posterior_sample should return samples
    sample1 = param.posterior_sample()
    sample2 = param.posterior_sample()
    assert sample1.ndim == 2
    assert sample2.ndim == 2
    assert sample1.shape[0] == 5
    assert sample1.shape[1] == 4
    assert sample2.shape[0] == 5
    assert sample2.shape[1] == 4
    assert np.all(sample1 != sample2)

    # posterior_sample should be able to return multiple samples
    sample1 = param.posterior_sample(10)
    sample2 = param.posterior_sample(10)
    assert sample1.ndim == 3
    assert sample2.ndim == 3
    assert sample1.shape[0] == 10
    assert sample1.shape[1] == 5
    assert sample1.shape[2] == 4
    assert sample2.shape[0] == 10
    assert sample2.shape[1] == 5
    assert sample2.shape[2] == 4
    assert np.all(sample1 != sample2)

    # prior_sample should still be 1D
    prior_sample = param.prior_sample()
    assert prior_sample.ndim == 0
    prior_sample = param.prior_sample(n=7)
    assert prior_sample.ndim == 1
    assert prior_sample.shape[0] == 7

    # n_parameters property
    nparams = param.n_parameters
    assert isinstance(nparams, int)
    assert nparams == 20
예제 #27
0
 def __init__(self):
     self.p1 = Parameter(name="TestParam1")
     self.p2 = Parameter(name="TestParam2", shape=[5, 4])
예제 #28
0
 def __init__(self, d: int):
     self.weights = Parameter([d, 1], name="weights")
     self.bias = Parameter([1, 1], name="bias")
예제 #29
0
 def __init__(self):
     initializer = {"loc": 0, "scale": 0.01}
     self.weight = Parameter(name="Weight", initializer=initializer)
     self.bias = Parameter(name="Bias", initializer=initializer)
예제 #30
0
 def __init__(self, d: int, k: int = 2):
     self.weights = Parameter([d, k - 1], name="weights")
     self.bias = Parameter([1, k - 1], name="bias")