예제 #1
0
K, d, N, T = 3, 4, 1000, 5000

# toy data generation
x_train = np.vstack([
    inf.models.Normal(loc=0, scale=1, dim=d).sample(300),
    inf.models.Normal(loc=10, scale=1, dim=d).sample(700)
])

######## Inferpy ##########

# model definition
with inf.ProbModel() as m:

    # prior distributions
    with inf.replicate(size=K):
        mu = inf.models.Normal(loc=0, scale=1, dim=d)
        sigma = inf.models.InverseGamma(
            concentration=1,
            rate=1,
            dim=d,
        )
    p = inf.models.Dirichlet(np.ones(K) / K)

    # define the generative model
    with inf.replicate(size=N):
        z = inf.models.Categorical(probs=p)
        x = inf.models.Normal(mu[z], sigma[z], observed=True, dim=d)

# compile and fit the model with training data
data = {x: x_train}
예제 #2
0
파일: 2.py 프로젝트: curiousTauseef/InferPy

# different ways of declaring 1 batch of 5 Normal distributions

x = inf.models.Normal(loc = 0, scale=1, dim=5)         # x.shape = [5]

x = inf.models.Normal(loc = [0, 0, 0, 0, 0], scale=1)  # x.shape = [5]

x = inf.models.Normal(loc = np.zeros(5), scale=1)      # x.shape = [5]

x = inf.models.Normal(loc = 0, scale=tf.ones(5))       # x.shape = [5]




with inf.replicate(size=10):
    x = inf.models.Normal(loc=0, scale=1, dim=5)       # x.shape = [10,5]

x = inf.models.Normal(loc=0, scale=1, dim=5, batches=10)       # x.shape = [10,5]
x = inf.models.Normal(loc=0, scale=1, dim=5, batches=[2,5])       # x.shape = [10,5]
y = x[7,4]                                              # y.shape = [1]

y2 = x[7]                                               # y2.shape = [5]

y3 = x[7,:]                                             # y2.shape = [5]

y4 = x[:,4]                                             # y4.shape = [10]



z = inf.models.Categorical(logits = np.zeros(5))
예제 #3
0
    def test(self):

        #### learning a 1-dim parameter from 1-dim data

        N = 500
        sampling_mean = [30.]
        sampling_std = 0.000000001
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta = inf.models.Normal(loc=0., scale=1.)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=theta, scale=1., observed=True)

        m.compile()

        x_train = inf.models.Normal(loc=sampling_mean, scale=sampling_std).sample(N)
        data = {x.name: x_train}

        m.fit(data)

        p1 = m.posterior(theta).loc[0]











        #### learning two 1-dim parameter from 2-dim data

        sampling_mean = [30., 10.]
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta1 = inf.models.Normal(loc=0., scale=1., dim=1)
            theta2 = inf.models.Normal(loc=0., scale=1., dim=1)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=[theta1, theta2], scale=1., observed=True)

        m.compile()

        x_train = inf.models.Normal(loc=sampling_mean, scale=sampling_std).sample(N)
        data = {x.name: x_train}


        m.fit(data)

        p3_1 = m.posterior(theta1).loc[0]
        p3_2 = m.posterior(theta2).loc[0]


        #### learning two 1-dim parameter from 2-dim data (with qmodel)

        sampling_mean = [30., 10.]
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta1 = inf.models.Normal(loc=0., scale=1., dim=1)
            theta2 = inf.models.Normal(loc=0., scale=1., dim=1)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=[theta1, theta2], scale=1., observed=True)

        # define the Qmodel

        q_theta1 = inf.Qmodel.new_qvar(theta1)
        q_theta2 = inf.Qmodel.new_qvar(theta2, initializer="ones")

        qmodel = inf.Qmodel([q_theta1, q_theta2])

        m.compile(Q=qmodel)

        x_train = inf.models.Normal(loc=sampling_mean, scale=sampling_std).sample(N)
        data = {x.name: x_train}


        m.fit(data)

        p4_1 = m.posterior(theta1).loc[0]
        p4_2 = m.posterior(theta2).loc[0]

        print(p4_1)
        print(p4_2)




        ## asserts
        self.assertTrue(abs(p1 - 29.66) < 0.1)

        self.assertTrue(abs(p3_1-29.66)<0.1)
        self.assertTrue(abs(p3_2-9.98)<0.1)


        self.assertTrue(abs(p4_1 - 29.66) < 0.1)
        self.assertTrue(abs(p4_2 - 9.98) < 0.1)
예제 #4
0
import inferpy as inf


with inf.replicate(size=50,name="A"):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")


with inf.replicate(size=10):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")

    with inf.replicate(size=2, name="C"):
        # Define some random variables here
        print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")
        print(inf.replicate.in_replicate())

# Define some random variables here
print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")


### existing replicate construct can be reused.
### This is done by ommiting the size argument and only setting the name with the name of
### an existing one.


with inf.replicate(name="A"):
    with inf.replicate(name="C"):
        # Define some random variables here
        print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")
예제 #5
0
    def test(self):
        # import the package for using it

        sess = ed.get_session()

        flag_y = True
        f = 2
        # graph

        N = 1000  # number of observations
        # model definition
        with inf.ProbModel() as m:
            # prior (latent variable)
            beta = inf.models.Normal(loc=0, scale=1, name="beta")
            w = inf.models.Normal(loc=0, scale=1, name="w")
            b = inf.models.Normal(loc=0, scale=1, name="b")
            betaz = inf.models.Normal(loc=0, scale=1, name="beta")

            # observed variable
            with inf.replicate(size=N):
                z = inf.models.Normal(loc=betaz,
                                      scale=1,
                                      observed=False,
                                      name="z")
                x = inf.models.Normal(loc=beta + z,
                                      scale=1,
                                      observed=True,
                                      name="x")
                y = inf.models.Normal(loc=w * x + b + z,
                                      scale=1,
                                      observed=True,
                                      name="y")

        # toy data generation

        x_train = inf.models.Normal(loc=10, scale=3, dim=1).sample(N)
        y_train = x_train * f + inf.models.Normal(loc=1, scale=0.1,
                                                  dim=1).sample(N)

        data = {x.name: x_train, y.name: y_train}

        m.compile()
        m.fit(data)

        qbeta = m.posterior(beta)
        qw = m.posterior(w)
        qb = m.posterior(b)
        qz = m.posterior(z)

        x_test = inf.models.Normal(loc=10, scale=3, dim=1).sample(N)
        y_test = x_test * f + inf.models.Normal(loc=1, scale=0.1,
                                                dim=1).sample(N)

        y_pred = m.predict(y, data={x: x_test})

        self.assertTrue(np.max((y_pred - y_test) < 0.5))

        inf.evaluate("log_lik",
                     data={
                         x: x_test,
                         y_pred: y_test
                     },
                     output_key=y_pred)
        inf.evaluate("mean_squared_error",
                     data={
                         x: x_test,
                         y_pred: y_test
                     },
                     output_key=y_pred)
예제 #6
0
import inferpy as inf

with inf.replicate(size=50):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) +
          " times")

with inf.replicate(size=10):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) +
          " times")

    with inf.replicate(size=2):
        # Define some random variables here
        print("Variable replicated " + str(inf.replicate.get_total_size()) +
              " times")
        print(inf.replicate.in_replicate())

# Define some random variables here
print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")

print(inf.replicate.in_replicate())
예제 #7
0
    def test(self):

        inf.replicate.delete_all()

        self.assertTrue(inf.replicate.in_replicate() == False)
        self.assertTrue(inf.replicate.get_active_replicate() == [])
        self.assertTrue(inf.replicate.get_all_replicate() == [])
        self.assertTrue(inf.replicate.get_total_size() == 1)

        with inf.replicate(size=5):

            print(inf.replicate.get_active_replicate()[-1].name)
            self.assertTrue(inf.replicate.in_replicate() == True)
            self.assertTrue(len(inf.replicate.get_active_replicate()) == 1)
            self.assertTrue(len(inf.replicate.get_all_replicate()) == 1)
            self.assertTrue(inf.replicate.get_total_size() == 5)

            with inf.replicate(size=5, name="A"):
                print(inf.replicate.get_active_replicate()[-1].name)
                self.assertTrue(inf.replicate.in_replicate() == True)
                self.assertTrue(len(inf.replicate.get_active_replicate()) == 2)
                self.assertTrue(len(inf.replicate.get_all_replicate()) == 2)
                self.assertTrue(inf.replicate.get_total_size() == 25)

            with inf.replicate(size=10, name="B"):
                print(inf.replicate.get_active_replicate()[-1].name)
                self.assertTrue(inf.replicate.in_replicate() == True)
                self.assertTrue(len(inf.replicate.get_active_replicate()) == 2)
                self.assertTrue(len(inf.replicate.get_all_replicate()) == 3)
                self.assertTrue(inf.replicate.get_total_size() == 50)

            with inf.replicate(name="A"):
                print(inf.replicate.get_active_replicate()[-1].name)
                with inf.replicate(name="B"):
                    print(inf.replicate.get_active_replicate()[-1].name)
                    self.assertTrue(inf.replicate.in_replicate() == True)
                    self.assertTrue(
                        len(inf.replicate.get_active_replicate()) == 3)
                    self.assertTrue(
                        len(inf.replicate.get_all_replicate()) == 3)
                    self.assertTrue(inf.replicate.get_total_size() == 250)

        with inf.replicate(name="A"):
            print(inf.replicate.get_active_replicate()[-1].name)
            with inf.replicate(name="B"):
                print(inf.replicate.get_active_replicate()[-1].name)
                self.assertTrue(inf.replicate.in_replicate() == True)
                self.assertTrue(len(inf.replicate.get_active_replicate()) == 2)
                self.assertTrue(len(inf.replicate.get_all_replicate()) == 3)
                self.assertTrue(inf.replicate.get_total_size() == 50)

                with inf.replicate(size=2):
                    print(inf.replicate.get_active_replicate()[-1].name)
                    self.assertTrue(inf.replicate.in_replicate() == True)
                    self.assertTrue(
                        len(inf.replicate.get_active_replicate()) == 3)
                    self.assertTrue(
                        len(inf.replicate.get_all_replicate()) == 4)
                    self.assertTrue(inf.replicate.get_total_size() == 100)

        self.assertTrue(inf.replicate.in_replicate() == False)
        self.assertTrue(len(inf.replicate.get_active_replicate()) == 0)
        self.assertTrue(len(inf.replicate.get_all_replicate()) == 4)
        self.assertTrue(inf.replicate.get_total_size() == 1)
예제 #8
0
    def test(self):

        #### learning a 1-dim parameter from 1-dim data

        N = 500
        sampling_mean = [30.]
        sampling_std = 0.000000001
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta = inf.models.Normal(loc=0., scale=1.)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=theta, scale=1., observed=True)

        m.compile()

        x_train = inf.models.Normal(loc=sampling_mean,
                                    scale=sampling_std).sample(N)
        data = {x.name: x_train}

        m.fit(data)

        p1 = m.posterior(theta).loc[0]

        #### learning a 2-dim parameter from 2-dim data

        sampling_mean = [30., 10.]
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta = inf.models.Normal(loc=0., scale=1., dim=2)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=theta, scale=1., observed=True)

        m.compile()

        x_train = inf.models.Normal(loc=sampling_mean,
                                    scale=sampling_std).sample(N)
        data = {x.name: x_train}

        m.fit(data)

        p2_1 = m.posterior(theta).loc[0]
        p2_2 = m.posterior(theta).loc[1]

        #### learning two 1-dim parameter from 2-dim data

        sampling_mean = [30., 10.]
        sess = ed.util.get_session()

        with inf.ProbModel() as m:
            theta1 = inf.models.Normal(loc=0., scale=1., dim=1)
            theta2 = inf.models.Normal(loc=0., scale=1., dim=1)

            with inf.replicate(size=N):
                x = inf.models.Normal(loc=[theta1, theta2],
                                      scale=1.,
                                      observed=True)

        m.compile()

        x_train = inf.models.Normal(loc=sampling_mean,
                                    scale=sampling_std).sample(N)
        data = {x.name: x_train}

        m.fit(data)

        p3_1 = m.posterior(theta1).loc[0]
        p3_2 = m.posterior(theta2).loc[0]

        ## asserts

        print(p1)
        print(p2_1)
        print(p2_2)
        print(p3_1)
        print(p3_2)

        self.assertTrue(abs(p1 - 29.66) < 0.1)

        self.assertTrue(abs(p2_1 - 29.66) < 0.1)
        self.assertTrue(abs(p2_2 - 9.98) < 0.1)

        self.assertTrue(abs(p3_1 - 29.66) < 0.1)
        self.assertTrue(abs(p3_2 - 9.98) < 0.1)
예제 #9
0
import inferpy as inf

# define a 2-dimension Normal distribution of 2·3=6 batches
with inf.replicate(size=2):
	with inf.replicate(size=3):
		x = inf.models.Normal(loc=0., scale=1., dim=2)


# print its parameters
print(x.loc)
print(x.scale)



# the shape of the distribution is (6,2)
print(x.shape)

# get a sample
sample_x = x.sample([4,10])

# the shape of the sample is (4, 10, 6, 2)
print(sample_x.shape)


x.sample(1, tf_run=False)
x.sample(1)

# probability and log probability of the sample
x.prob(sample_x)

x.log_prob(sample_x)
예제 #10
0
    def test(self):
        N = 1
        d = 1

        with inf.replicate(size=N):
            c = inf.models.Categorical(probs=[0.4, 0.6], dim=d)
            m = inf.models.Multinomial(probs=[0.4, 0.2, 0.4],
                                       total_count=5,
                                       dim=d)
            n = inf.models.Normal(0, 1, dim=d)
            mn = inf.models.MultivariateNormalDiag(loc=[0, 0],
                                                   scale_diag=[1, 1],
                                                   dim=d)

        D = [c, m, n, mn]

        self.assertTrue([x.shape for x in D] == [[1], [1, 3], [1], [1, 2]])
        self.assertTrue([x.dim for x in D] == [1, 1, 1, 1])
        self.assertTrue([x.batches for x in D] == [1, 1, 1, 1])
        self.assertTrue([x.event_shape for x in D] == [[], [3], [], [2]])

        ############

        N = 10
        d = 1

        with inf.replicate(size=N):
            c = inf.models.Categorical(probs=[0.4, 0.6], dim=d)
            m = inf.models.Multinomial(probs=[0.4, 0.2, 0.4],
                                       total_count=5,
                                       dim=d)
            n = inf.models.Normal(0, 1, dim=d)
            mn = inf.models.MultivariateNormalDiag(loc=[0, 0],
                                                   scale_diag=[1, 1],
                                                   dim=d)

        D = [c, m, n, mn]

        self.assertTrue(
            [x.shape for x in D] == [[10, 1], [10, 1, 3], [10, 1], [10, 1, 2]])
        self.assertTrue([x.dim for x in D] == [d * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.batches
                         for x in D] == [N * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.event_shape for x in D] == [[], [3], [], [2]])

        ############

        N = 1
        d = 10

        with inf.replicate(size=N):
            c = inf.models.Categorical(probs=[0.4, 0.6], dim=d)
            m = inf.models.Multinomial(probs=[0.4, 0.2, 0.4],
                                       total_count=5,
                                       dim=d)
            n = inf.models.Normal(0, 1, dim=d)
            mn = inf.models.MultivariateNormalDiag(loc=[0, 0],
                                                   scale_diag=[1, 1],
                                                   dim=d)

        D = [c, m, n, mn]

        self.assertTrue([x.shape for x in D] == [[10], [10, 3], [10], [10, 2]])
        self.assertTrue([x.dim for x in D] == [d * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.batches
                         for x in D] == [N * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.event_shape for x in D] == [[], [3], [], [2]])

        ############

        N = 10
        d = 5

        with inf.replicate(size=N):
            c = inf.models.Categorical(probs=[0.4, 0.6], dim=d)
            m = inf.models.Multinomial(probs=[0.4, 0.2, 0.4],
                                       total_count=5,
                                       dim=d)
            n = inf.models.Normal(0, 1, dim=d)
            mn = inf.models.MultivariateNormalDiag(loc=[0, 0],
                                                   scale_diag=[1, 1],
                                                   dim=d)

        D = [c, m, n, mn]

        self.assertTrue(
            [x.shape for x in D] == [[10, 5], [10, 5, 3], [10, 5], [10, 5, 2]])
        self.assertTrue([x.dim for x in D] == [d * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.batches
                         for x in D] == [N * i for i in [1, 1, 1, 1]])
        self.assertTrue([x.event_shape for x in D] == [[], [3], [], [2]])