예제 #1
0
    def _expectation(self, z):
        with torch.no_grad():
            # computing wik
            pdf = df.gaussianPDF(z,
                                 self._mu,
                                 self._sigma,
                                 norm_func=self.zeta_phi.zeta)
            if (pdf.mean() != pdf.mean()):
                print("EXPECTATION : pdf contain not a number elements")
                quit()
            p_pdf = pdf * self._w.unsqueeze(0).expand_as(pdf)

            # it can happens sometime to get (due to machine precision) a node with all pdf to zero
            # in this case we must detect it. There is severall solution, in our case we affect it
            # equally to each gaussian.
            if (p_pdf.sum(-1).min() <= 1e-15):
                if (self._verbose):
                    print("EXPECTATION : pdf.sum(-1) contain zero for ",
                          (p_pdf.sum(-1) <= 1e-15).sum().item(), "items")
                p_pdf[p_pdf.sum(-1) <= 1e-15] = 1

            wik = p_pdf / p_pdf.sum(-1, keepdim=True).expand_as(pdf)
            if (wik.mean() != wik.mean()):
                print("EXPECTATION : wik contain not a number elements")
                quit()
            # print(wik.mean(0))
            if (wik.sum(1).mean() <= 1 - 1e-4
                    and wik.sum(1).mean() >= 1 + 1e-4):
                print("EXPECTATION : wik don't sum to 1")
                print(wik.sum(1))
                quit()
            return wik
예제 #2
0
 def get_density(self, z):
     N, D, M = z.shape + (self._mu.shape[0], )
     pdf = df.gaussianPDF(z,
                          self._mu,
                          self._sigma,
                          norm_func=self.zeta_phi.zeta)
     density = (pdf * self._w.unsqueeze(0).expand_as(pdf)).sum(-1)
     return density
예제 #3
0
 def get_unormalized_probs(self, z):
     N, D, M = z.shape + (self._mu.shape[0], )
     pdf = df.gaussianPDF(z,
                          self._mu,
                          self._sigma,
                          norm_func=self.zeta_phi.zeta)
     p_pdf = pdf * self._w.unsqueeze(0).expand_as(pdf)
     return p_pdf
예제 #4
0
 def predict(self, z):
     N, D, M = z.shape + (self._mu.shape[0], )
     pdf = df.gaussianPDF(z,
                          self._mu,
                          self._sigma,
                          norm_func=self.zeta_phi.zeta)
     p_pdf = pdf * self._w.unsqueeze(0).expand_as(pdf)
     return p_pdf.max(-1)[1]
예제 #5
0
 def get_pik(self, z):
     N, D, M = z.shape + (self._mu.shape[0], )
     pdf = df.gaussianPDF(z,
                          self._mu,
                          self._sigma,
                          norm_func=self.zeta_phi.zeta)
     p_pdf = pdf * self._w.unsqueeze(0).expand_as(pdf)
     if (p_pdf.sum(-1).min() == 0):
         print(
             "EXPECTATION (function get_pik) : pdf.sum(-1) contain zero for ",
             (p_pdf.sum(-1) <= 1e-15).sum().item(), "items")
         p_pdf[p_pdf.sum(-1) == 0] = 1e-8
     wik = p_pdf / p_pdf.sum(-1, keepdim=True).expand_as(pdf)
     return wik
    def get_pik(self, z):

        N, D, M = z.shape + (self._mu.shape[0], )
        pdf = df.gaussianPDF(z,
                             self._mu,
                             self._sigma,
                             norm_func=self.norm_ff,
                             distance=self._distance)

        print("pdf mean", pdf[20])
        p_pdf = pdf * self._w.unsqueeze(0).expand_as(pdf)
        print("ppd", p_pdf.size())
        if (p_pdf.sum(-1).min() == 0):
            print("EXPECTATION : pdf.sum(-1) contain zero -> ",
                  (p_pdf.sum(-1) == 0).sum())
            #same if we set = 1
            p_pdf[p_pdf.sum(-1) == 0] = 1e-8
        wik = p_pdf / p_pdf.sum(-1, keepdim=True).expand_as(pdf)
        # print("wik 1", wik.mean(1))
        # print("wik 2", wik.mean(0))
        # wik[torch.arange(len(wik)), wik.max(1)[1]] = 1
        # wik = wik.long().float()
        # print("wik 2", wik.mean(0))
        return wik
예제 #7
0
from rcome.function_tools import distribution_function as df

# Define the barycentre mu and variance sigma
mu = torch.Tensor([[0.7, 0.7]])
sigma = torch.Tensor([2.2])

# Precompute the normalisation factor
norm_factor = df.ZetaPhiStorage(torch.arange(5e-2, 2., 0.001), 2)

# Sample a number of points on the manifold
points = (torch.rand(200000, 2) - 0.5) * 2
points = points[points.norm(2, -1) < 0.9999]

#Compute the probability density of each point
probs = df.gaussianPDF(points, mu, sigma, norm_func=norm_factor.zeta).squeeze()

#Divide into two classes
points_high = points[probs > 0.001]
points_med = points[probs > 0.003]
points_low = points[probs <= 0.003]

plt.figure()
plt.gca().set_aspect('equal', adjustable='box')
plt.axis('off')
plt.scatter(points_low[:, 0].numpy(), points_low[:, 1].numpy(), c='red')
plt.scatter(points_high[:, 0].numpy(), points_high[:, 1].numpy(), c='blue')
plt.scatter(points_med[:, 0].numpy(), points_med[:, 1].numpy(), c='green')
plt.scatter(mu[:, 0].numpy(), mu[:, 1].numpy(), c='orange', marker='*', s=150)
plt.savefig('gaussian.png')
plt.show()