コード例 #1
0
ファイル: test_laue.py プロジェクト: Hekstra-Lab/careless
def test_laue_LaplaceLikelihood(laue_inputs):
    likelihood = LaplaceLikelihood()(laue_inputs)
    iobs = BaseModel.get_intensities(laue_inputs)
    sigiobs = BaseModel.get_uncertainties(laue_inputs)
    ipred = fake_ipred(laue_inputs)

    l_true = tfd.Laplace(iobs, sigiobs / np.sqrt(2.))

    iconv = likelihood.convolve(ipred)

    test = likelihood.log_prob(ipred).numpy()
    expected = l_true.log_prob(iobs).numpy()

    nobs = BaseModel.get_harmonic_id(laue_inputs).max() + 1

    test = likelihood.log_prob(ipred).numpy()
    expected = l_true.log_prob(iobs).numpy().T

    #The zero padded entries at the end of the input will disagree
    #with the expected values. This is fine, because they will not
    #contribute to the gradient
    test = test[:, :nobs]
    expected = expected[:, :nobs]

    assert np.array_equal(expected.shape, test.shape)
    assert np.allclose(expected, test)

    #Test batches larger than 1
    ipred = np.concatenate((ipred, ipred, ipred), axis=0)
    likelihood.convolve(ipred).numpy()
    test = likelihood.log_prob(ipred).numpy()
    test = test[:, :nobs]
    assert np.array_equiv(expected, test)
コード例 #2
0
        def decoder(state_sample, observation_dist="gaussian"):
            """Compute the data distribution of an observation from its state [1]."""
            check_in(
                "observation_dist",
                observation_dist,
                ("gaussian", "laplace", "bernoulli", "multinomial"),
            )

            timesteps = tf.shape(state_sample)[1]

            if self.pixel_observations:
                # original decoder from [1] for deepmind lab envs
                hidden = tf.layers.dense(state_sample, 1024, None)
                kwargs = dict(strides=2, activation=tf.nn.relu)
                hidden = tf.reshape(hidden, [-1, 1, 1, hidden.shape[-1]])
                # 1 x 1
                hidden = tf.layers.conv2d_transpose(hidden, 128, 5, **kwargs)
                # 5 x 5 x 128
                hidden = tf.layers.conv2d_transpose(hidden, 64, 5, **kwargs)
                # 13 x 13 x 64
                hidden = tf.layers.conv2d_transpose(hidden, 32, 6, **kwargs)
                # 30 x 30 x 32
                mean = 255 * tf.layers.conv2d_transpose(
                    hidden, 3, 6, strides=2, activation=tf.nn.sigmoid)
                # 64 x 64 x 3
                assert mean.shape[1:].as_list() == [64, 64, 3], mean.shape
            else:
                # decoder for gridworlds / structured observations
                hidden = state_sample
                d = self._hidden_layer_size
                for _ in range(4):
                    hidden = tf.layers.dense(hidden, d, tf.nn.relu)
                mean = tf.layers.dense(hidden, np.prod(self.data_shape), None)

            mean = tf.reshape(mean, [-1, timesteps] + list(self.data_shape))

            check_in(
                "observation_dist",
                observation_dist,
                ("gaussian", "laplace", "bernoulli", "multinomial"),
            )
            if observation_dist == "gaussian":
                dist = tfd.Normal(mean, self._obs_stddev)
            elif observation_dist == "laplace":
                dist = tfd.Laplace(mean, self._obs_stddev / np.sqrt(2))
            elif observation_dist == "bernoulli":
                dist = tfd.Bernoulli(probs=mean)
            else:
                mean = tf.reshape(mean, [-1, timesteps] +
                                  [np.prod(list(self.data_shape))])
                dist = tfd.Multinomial(total_count=1, probs=mean)
                reshape = tfp.bijectors.Reshape(
                    event_shape_out=list(self.data_shape))
                dist = reshape(dist)
                return dist

            dist = tfd.Independent(dist, len(self.data_shape))
            return dist
コード例 #3
0
def test_mono_LaplaceLikelihood(mono_inputs):
    likelihood = LaplaceLikelihood()(mono_inputs)
    iobs = BaseModel.get_intensities(mono_inputs)
    sigiobs = BaseModel.get_uncertainties(mono_inputs)

    l_true = tfd.Laplace(
        tf.squeeze(iobs), 
        tf.squeeze(sigiobs)/np.sqrt(2.),
    )
    z = l_true.sample()

    assert np.allclose(likelihood.log_prob(z), l_true.log_prob(z))
コード例 #4
0
ファイル: empirical.py プロジェクト: Hekstra-Lab/careless
 def __init__(self, Fobs, SigFobs, observed=None):
     """
     Parameters
     ----------
     Fobs : array
         numpy array or tf.Tensor containing observed structure factors amplitudes from a reference structure.
     SigFobs : array
         numpy array or tf.Tensor containing error estimates for structure factors amplitudes from a reference structure.
     observed : array (optional)
         boolean numpy array or tf.Tensor which has True for all observed miller indices.
     """
     super().__init__(observed)
     loc = np.array(Fobs, dtype=np.float32)
     scale = np.array(SigFobs, dtype=np.float32) / np.sqrt(2.)
     self.base_dist = tfd.Laplace(loc, scale)
コード例 #5
0
ファイル: laplace.py プロジェクト: danhlephuoc/libspn
 def _create_dist(self):
     if self._softplus_scale:
         return tfd.LaplaceWithSoftplusScale(self._loc_variable,
                                             self._scale_variable)
     return tfd.Laplace(self._loc_variable, self._scale_variable)
コード例 #6
0
ファイル: continuous.py プロジェクト: fonnesbeck/pymc4
 def _init_distribution(conditions, **kwargs):
     loc, scale = conditions["loc"], conditions["scale"]
     return tfd.Laplace(loc=loc, scale=scale, **kwargs)
コード例 #7
0
 def _base_dist(self, mu: TensorLike, b: TensorLike, *args, **kwargs):
     return tfd.Laplace(loc=mu, scale=b)
コード例 #8
0
ファイル: mono.py プロジェクト: Hekstra-Lab/careless
 def call(self, inputs):
     loc, scale = self.get_loc_and_scale(inputs)
     return tfd.Laplace(loc, scale / np.sqrt(2.))
コード例 #9
0
def test_LaplaceReferencePrior(mc_samples):
    p = LaplaceReferencePrior(Fobs[observed], SigFobs[observed], observed)
    q = tfd.Laplace(Fobs, SigFobs / np.sqrt(2.))
    ReferencePrior_test(p, q, mc_samples)
コード例 #10
0
 def dist(self, loc, scale):
     loc = tf.squeeze(loc)
     scale = tf.squeeze(scale)
     return tfd.Laplace(loc, scale / np.sqrt(2.))