Пример #1
0
def test_advi_minibatch():
    n = 1000
    sd0 = 2.
    mu0 = 4.
    sd = 3.
    mu = -5.

    data = sd * np.random.RandomState(0).randn(n) + mu

    d = n / sd**2 + 1 / sd0**2
    mu_post = (n * np.mean(data) / sd**2 + mu0 / sd0**2) / d

    data_t = tt.vector()
    data_t.tag.test_value = np.zeros(1, )

    with Model() as model:
        mu_ = Normal('mu', mu=mu0, sd=sd0, testval=0)
        x = Normal('x', mu=mu_, sd=sd, observed=data_t)

    minibatch_RVs = [x]
    minibatch_tensors = [data_t]

    def create_minibatch(data):
        while True:
            data = np.roll(data, 100, axis=0)
            yield data[:100]

    minibatches = [create_minibatch(data)]

    with model:
        advi_fit = advi_minibatch(n=1000,
                                  minibatch_tensors=minibatch_tensors,
                                  minibatch_RVs=minibatch_RVs,
                                  minibatches=minibatches,
                                  total_size=n,
                                  learning_rate=1e-1,
                                  random_seed=1)

        np.testing.assert_allclose(advi_fit.means['mu'], mu_post, rtol=0.1)

        trace = sample_vp(advi_fit, 10000)

    np.testing.assert_allclose(np.mean(trace['mu']), mu_post, rtol=0.4)
    np.testing.assert_allclose(np.std(trace['mu']), np.sqrt(1. / d), rtol=0.4)
Пример #2
0
def test_advi_minibatch():
    n = 1000
    sd0 = 2.
    mu0 = 4.
    sd = 3.
    mu = -5.

    data = sd * np.random.RandomState(0).randn(n) + mu

    d = n / sd**2 + 1 / sd0**2
    mu_post = (n * np.mean(data) / sd**2 + mu0 / sd0**2) / d

    data_t = tt.vector()
    data_t.tag.test_value = np.zeros(1, )

    with Model() as model:
        mu_ = Normal('mu', mu=mu0, sd=sd0, testval=0)
        x = Normal('x', mu=mu_, sd=sd, observed=data_t)

        # mu = Normal('mu', mu=0, sd=1, testval=0)
        # sd = HalfNormal('sd', sd=1)
        # n = Normal('n', mu=mu, sd=sd, observed=data_t)

    minibatch_RVs = [x]
    minibatch_tensors = [data_t]

    def create_minibatch(data):
        while True:
            data = np.roll(data, 100, axis=0)
            yield data[:100]

    minibatches = [create_minibatch(data)]

    means, sds, elbos = advi_minibatch(model=model,
                                       n=1000,
                                       minibatch_tensors=minibatch_tensors,
                                       minibatch_RVs=minibatch_RVs,
                                       minibatches=minibatches,
                                       total_size=n,
                                       learning_rate=1e-1,
                                       seed=1)

    np.testing.assert_allclose(means['mu'], mu_post, rtol=0.1)
Пример #3
0
def test_advi_minibatch():
    n = 1000
    sd0 = 2.
    mu0 = 4.
    sd = 3.
    mu = -5.

    data = sd * np.random.RandomState(0).randn(n) + mu

    d = n / sd**2 + 1 / sd0**2
    mu_post = (n * np.mean(data) / sd**2 + mu0 / sd0**2) / d

    data_t = tt.vector()
    data_t.tag.test_value=np.zeros(1,)

    with Model() as model:
        mu_ = Normal('mu', mu=mu0, sd=sd0, testval=0)
        x = Normal('x', mu=mu_, sd=sd, observed=data_t)
        
    minibatch_RVs = [x]
    minibatch_tensors = [data_t]

    def create_minibatch(data):
        while True:
            data = np.roll(data, 100, axis=0)
            yield data[:100]

    minibatches = [create_minibatch(data)]

    with model:
        advi_fit = advi_minibatch(
            n=1000, minibatch_tensors=minibatch_tensors, 
            minibatch_RVs=minibatch_RVs, minibatches=minibatches, 
            total_size=n, learning_rate=1e-1, random_seed=1
        )

        np.testing.assert_allclose(advi_fit.means['mu'], mu_post, rtol=0.1)

        trace = sample_vp(advi_fit, 10000)

    np.testing.assert_allclose(np.mean(trace['mu']), mu_post, rtol=0.4)
    np.testing.assert_allclose(np.std(trace['mu']), np.sqrt(1. / d), rtol=0.4)
Пример #4
0
def test_advi_minibatch():
    n = 1000
    sd0 = 2.
    mu0 = 4.
    sd = 3.
    mu = -5.

    data = sd * np.random.RandomState(0).randn(n) + mu

    d = n / sd**2 + 1 / sd0**2
    mu_post = (n * np.mean(data) / sd**2 + mu0 / sd0**2) / d

    data_t = tt.vector()
    data_t.tag.test_value=np.zeros(1,)

    with Model() as model:
        mu_ = Normal('mu', mu=mu0, sd=sd0, testval=0)
        x = Normal('x', mu=mu_, sd=sd, observed=data_t)

        # mu = Normal('mu', mu=0, sd=1, testval=0)
        # sd = HalfNormal('sd', sd=1)
        # n = Normal('n', mu=mu, sd=sd, observed=data_t)
        
    minibatch_RVs = [x]
    minibatch_tensors = [data_t]

    def create_minibatch(data):
        while True:
            data = np.roll(data, 100, axis=0)
            yield data[:100]

    minibatches = [create_minibatch(data)]

    means, sds, elbos = advi_minibatch(
        model=model, n=1000, minibatch_tensors=minibatch_tensors, 
        minibatch_RVs=minibatch_RVs, minibatches=minibatches, 
        total_size=n, learning_rate=1e-1, seed=1
    )

    np.testing.assert_allclose(means['mu'], mu_post, rtol=0.1)