Пример #1
0
  def test_clone(self):
    reg = prior.SpikeAndSlabRegularizer(weight=1.0)
    model1 = tf.keras.models.Sequential([
        tf.keras.layers.Dense(10, activation='relu',
                              kernel_regularizer=reg,
                              bias_regularizer=reg),
        tf.keras.layers.Dense(10)])

    ndata = 256
    data = tf.random.normal((ndata, 10))
    pred1 = model1(data)

    model2 = bnnmodel.clone_model_and_weights(model1, (None, 10))
    model1 = None
    pred2 = model2(data)

    self.assertAllClose(pred1, pred2, msg='Model cloning failed.')
Пример #2
0
 def test_tfd_prior_spike_and_slab(self):
   self._check_regularizer(prior.SpikeAndSlabRegularizer())
Пример #3
0
import scipy.integrate as integrate

import tensorflow.compat.v1 as tf
import tensorflow_probability as tfp

from cold_posterior_bnn.core import prior

tfd = tfp.distributions

REGULARIZER_INSTANCES_LOGPDF = [
    prior.NormalRegularizer(stddev=0.1, weight=0.5),
    prior.ShiftedNormalRegularizer(mean=0.2, stddev=0.1, weight=0.5),
    prior.StretchedNormalRegularizer(offset=0.2, scale=1.2, weight=0.1),
    prior.LaplaceRegularizer(stddev=0.1),
    prior.CauchyRegularizer(scale=0.2),
    prior.SpikeAndSlabRegularizer(scale_spike=0.01, scale_slab=0.3,
                                  mass_spike=0.6, weight=0.8),
]
REGULARIZER_INSTANCES_NOLOGPDF = [
    prior.HeNormalRegularizer(scale=0.8, weight=0.7),
    prior.GlorotNormalRegularizer(scale=0.8, weight=0.7),
    prior.EmpiricalBayesNormal(ig_shape=2.5, ig_scale=0.25, weight=0.3),
    prior.HeNormalEBRegularizer(scale=0.25, weight=0.1),
]
REGULARIZER_INSTANCES = REGULARIZER_INSTANCES_LOGPDF + \
    REGULARIZER_INSTANCES_NOLOGPDF

# Regularizers with scale parameters, scale=1
REGULARIZER_INSTANCES_SCALE1 = [
    ('stddev', prior.NormalRegularizer(stddev=1.0, weight=0.5)),
    ('stddev', prior.ShiftedNormalRegularizer(mean=0., stddev=1.0,
                                              weight=0.5)),