예제 #1
0
    def _resample_A(self, stats):

        _, yxT, xxT, _ = stats

        # Sample each row of W
        for d in range(self.D_out):
            # Get sufficient statistics from the data
            Jd = self.J_0 + xxT[d] / self.sigmasq_flat[d]
            hd = self.h_0 + yxT[d] / self.sigmasq_flat[d]
            self.A[d] = sample_gaussian(J=Jd, h=hd)
예제 #2
0
    def _resample_A(self, stats):

        _, yxT, xxT, _ = stats

        # Sample each row of W
        for d in range(self.D_out):
            # Get sufficient statistics from the data
            Jd = self.J_0 + xxT[d] / self.sigmasq_flat[d]
            hd = self.h_0 + yxT[d] / self.sigmasq_flat[d]
            self.A[d] = sample_gaussian(J=Jd, h=hd)
예제 #3
0
 def resample(self,data=[],niter=None):
     niter = niter if niter else self.niter
     if getdatasize(data) == 0:
         self.A = sample_gaussian(J=self.J_0,h=self.h_0.ravel())\
             .reshape(self.h_0.shape)
         self.sigma = sample_invwishart(self.S_0,self.nu_0)
     else:
         yyT, yxT, xxT, n = self._get_statistics(data)
         for itr in range(niter):
             self._resample_A(xxT, yxT, self.sigma)
             self._resample_sigma(xxT, yxT, yyT, n, self.A)
예제 #4
0
    def resample(self):
        W, sigmasq = self.W, self.sigmasq
        J0 = np.eye(self.D_latent)
        h0 = np.zeros(self.D_latent)

        # Sample each latent embedding
        for n in range(self.N):
            Jobs = self.mask[n] / sigmasq
            Jpost = J0 + (W * Jobs[:, None]).T.dot(W)
            hpost = h0 + (self.X[n] * Jobs).dot(W)
            self.Z[n] = sample_gaussian(J=Jpost, h=hpost)
예제 #5
0
    def resample(self):
        W, sigmasq = self.W, self.sigmasq
        J0 = np.eye(self.D_latent)
        h0 = np.zeros(self.D_latent)

        # Sample each latent embedding
        for n in range(self.N):
            Jobs = self.mask[n] / sigmasq
            Jpost = J0 + (W * Jobs[:, None]).T.dot(W)
            hpost = h0 + (self.X[n] * Jobs).dot(W)
            self.Z[n] = sample_gaussian(J=Jpost, h=hpost)
예제 #6
0
 def resample(self, data=[], niter=None):
     niter = niter if niter else self.niter
     if getdatasize(data) == 0:
         self.A = sample_gaussian(J=self.J_0,h=self.h_0.ravel())\
             .reshape(self.h_0.shape)
         self.sigma = sample_invwishart(self.S_0, self.nu_0)
     else:
         yyT, yxT, xxT, n = self._get_statistics(data)
         for itr in range(niter):
             self._resample_A(xxT, yxT, self.sigma)
             self._resample_sigma(xxT, yxT, yyT, n, self.A)
예제 #7
0
    def resample(self, data=[]):
        N, P, D, L, affine = self.N, self.P, self.D, self.L, self.affine

        if not isinstance(data, list):
            if isinstance(data, tuple) and len(data) == 3:
                data = [data]
            else:
                raise Exception("Expected list or length-3 tuple (x, y, z)!")

        # Prior
        big_J = np.tile(self.J_0[:, :, None], (1, 1, P))
        big_h = np.zeros((N * (D + affine), P))

        # Likelihood
        for (x, y, z) in data:
            T = x.shape[0]

            # pad x as necessary
            if self.affine:
                x = np.column_stack((x, np.ones(T)))

            assert x.shape == (T, D + affine)
            assert y.shape == (T, P)

            # make sure z is ints between 0 and L-1
            assert z.shape == (T, )
            assert z.dtype == int and z.min() >= 0 and z.max() < L

            # compute the likelihood for each leaf node
            for l in range(L):
                inds = z == l
                xi = x[inds]
                yi = y[inds]
                xxT = (xi[:, :, None] * xi[:, None, :]).sum(0)
                yxT = (yi[:, :, None] * xi[:, None, :]).sum(0)

                j = self.leaves[l]
                for p in range(P):
                    big_J[j * D:(j + 1) * D, j * D:(j + 1) * D,
                          p] += self.Q[p, p] * xxT
                    big_h[j * D:(j + 1) * D, p] += self.Q[p, p] * yxT[p]

        # Sample As from their Gaussian conditional
        self.As = np.zeros((N, P, D + affine))
        for p in range(P):
            self.As[:, p, :] = sample_gaussian(J=big_J[:, :, p],
                                               h=big_h[:, p]).reshape(
                                                   (N, D + affine))

        # TODO: Resample Q from its conditional... inverse gamma?

        # Set the parameters of the regression object
        for l, regression in zip(self.leaves, self.regressions):
            regression.A = self.As[l]
예제 #8
0
    def _resample_Z(self):
        """
        p(_n | A, kappa, omega) = p(z_n) * p(Az_n + b| kappa/omega, omega^-1)
        = p(Z) p(Z | A.T (kappa/omega-b), A.T omega A)
        """
        from pybasicbayes.util.stats import sample_gaussian
        kappa, omega = self.kappa, self.omega
        A, b = self.A, self.bias

        for n in range(self.N):
            h = np.zeros(self.D)
            J = np.eye(self.D)

            h += A.T.dot((kappa[:, n] / omega[:, n] - b) * omega[:, n])
            J += (A * omega[:, n][:, None]).T.dot(A)

            self.Z[n] = sample_gaussian(J=J, h=h)
예제 #9
0
    def _resample_Z(self):
        """
        p(_n | A, kappa, omega) = p(z_n) * p(Az_n + b| kappa/omega, omega^-1)
        = p(Z) p(Z | A.T (kappa/omega-b), A.T omega A)
        """
        from pybasicbayes.util.stats import sample_gaussian
        kappa, omega = self.kappa, self.omega
        A,b = self.A, self.bias

        for n in range(self.N):
            h = np.zeros(self.D)
            J = np.eye(self.D)

            h += A.T.dot((kappa[:,n]/omega[:,n] - b) * omega[:,n])
            J += (A * omega[:,n][:,None]).T.dot(A)

            self.Z[n] = sample_gaussian(J=J, h=h)
예제 #10
0
    def _resample_W(self, J_post, h_post):
        """
        Resample the weight of a connection (synapse)
        """
        N, B = self.N, self.B

        a = np.concatenate((np.repeat(self.a, self.B), [1])).astype(np.bool)
        Jp = J_post[np.ix_(a, a)]
        hp = h_post[a]

        # Sample in information form
        W = sample_gaussian(J=Jp, h=hp)

        # Set bias and weights
        self.W *= 0
        self.W[self.a, :] = W[:-1].reshape((-1, B))
        # self.W = np.reshape(W[:-1], (D,N,B))
        self.b = np.reshape(W[-1], (1, ))
예제 #11
0
    def _resample_W(self, J_post, h_post):
        """
        Resample the weight of a connection (synapse)
        """
        N, B = self.N, self.B

        a = np.concatenate((np.repeat(self.a, self.B), [1])).astype(np.bool)
        Jp = J_post[np.ix_(a, a)]
        hp = h_post[a]

        # Sample in information form
        W = sample_gaussian(J=Jp, h=hp)

        # Set bias and weights
        self.W *= 0
        self.W[self.a, :] = W[:-1].reshape((-1,B))
        # self.W = np.reshape(W[:-1], (D,N,B))
        self.b = np.reshape(W[-1], (1,))
예제 #12
0
    def _resample_regression(self):
        """
        p(A | Z, kappa, omega) = p(A) * p(AZ + b | kappa/omega, omega)
        = \prod_t N(Ab_t, 0, I) * N(Z'.T Ab_t | kappa_t/omega_t, omega_t)
        """
        # xy = np.hstack((self.Z, self.S.T))
        # self.regression.resample(xy)

        from pybasicbayes.util.stats import sample_gaussian
        kappa, omega = self.kappa, self.omega
        Z = np.hstack((self.Z, np.ones((self.N,1))))

        for t in xrange(self.T):
            h = np.zeros(self.D+1)
            J = np.eye(self.D+1)

            h += Z.T.dot(kappa[t])
            J += (Z * omega[t][:,None]).T.dot(Z)

            Abt = sample_gaussian(J=J, h=h)
            self.A[t], self.bias[t] = Abt[:-1], Abt[-1]
예제 #13
0
    def _resample_regression(self):
        """
        p(A | Z, kappa, omega) = p(A) * p(AZ + b | kappa/omega, omega)
        = \prod_t N(Ab_t, 0, I) * N(Z'.T Ab_t | kappa_t/omega_t, omega_t)
        """
        # xy = np.hstack((self.Z, self.S.T))
        # self.regression.resample(xy)

        from pybasicbayes.util.stats import sample_gaussian
        kappa, omega = self.kappa, self.omega
        Z = np.hstack((self.Z, np.ones((self.N, 1))))

        for t in xrange(self.T):
            h = np.zeros(self.D + 1)
            J = np.eye(self.D + 1)

            h += Z.T.dot(kappa[t])
            J += (Z * omega[t][:, None]).T.dot(Z)

            Abt = sample_gaussian(J=J, h=h)
            self.A[t], self.bias[t] = Abt[:-1], Abt[-1]
예제 #14
0
 def _resample_A(self, xxT, yxT, sigma):
     sigmainv = np.linalg.inv(sigma)
     J = self.J_0 + np.kron(sigmainv, xxT)
     h = self.h_0 + sigmainv.dot(yxT)
     self.A = sample_gaussian(J=J, h=h.ravel()).reshape(h.shape)
예제 #15
0
 def resample_from_mf(self):
     for n in range(self.N):
         mu_n = self.E_Z[n]
         Sigma_n = self.E_ZZT[n] - np.outer(mu_n, mu_n)
         self.Z[n] = sample_gaussian(mu=mu_n, Sigma=Sigma_n)
예제 #16
0
 def resample_from_mf(self):
     for n in range(self.N):
         mu_n = self.E_Z[n]
         Sigma_n = self.E_ZZT[n] - np.outer(mu_n, mu_n)
         self.Z[n] = sample_gaussian(mu=mu_n, Sigma=Sigma_n)
예제 #17
0
 def _resample_A(self, xxT, yxT, sigma):
     sigmainv = np.linalg.inv(sigma)
     J = self.J_0 + np.kron(sigmainv, xxT)
     h = self.h_0 + sigmainv.dot(yxT)
     self.A = sample_gaussian(J=J,h=h.ravel()).reshape(h.shape)
예제 #18
0
 def resample_from_mf(self):
     for d in range(self.D_out):
         self.A[d] = sample_gaussian(J=self.mf_J_A[d], h=self.mf_h_A[d])
     self.sigmasq_flat = sample_invgamma(self.mf_alpha,
                                         self.mf_beta) * np.ones(self.D_out)
예제 #19
0
 def resample_from_mf(self):
     for d in range(self.D_out):
         self.A[d] = sample_gaussian(J=self.mf_J_A[d], h=self.mf_h_A[d])
     self.sigmasq_flat = sample_invgamma(self.mf_alpha, self.mf_beta) * np.ones(self.D_out)